提升asio信号量解决方案

时间:2011-10-17 12:43:25

标签: c++ multithreading boost boost-asio boost-thread

这个问题是this问题的后续问题。无论如何,这是动机 - 来自原始问题评论的一些c / p:

我希望能够在多个线程上发布一组作业( CalcFib 功能),然后在作业结束时发布另一组作业( CalcFib2 函数),也适用于多个线程。这个循环循环多次(这里是两个),所以我认为最好的办法是创建boost::asio::io_service并在循环开始时创建线程,所以我不必创建&每次循环开始/结束时销毁线程。

我创建了两个错误地命名为信号量_ **的int变量,并在上述函数中递减它们。等待作业组完成的代码为while,如下所示。解决方案起作用,至少我可以看到。

使用while的等待真的是一种方法吗?我错过了什么?有更好的方法吗?

我的代码如下所示:

#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <iostream>

boost::mutex global_stream_lock;
int semaphore_fib = 0;
int semaphore_fib2 = 0;

void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service)
{
    global_stream_lock.lock();
    std::cout << "[" << boost::this_thread::get_id()
        << "] Thread Start" << std::endl;
    global_stream_lock.unlock();

    io_service->run();

    global_stream_lock.lock();
    std::cout << "[" << boost::this_thread::get_id()
        << "] Thread Finish" << std::endl;
    global_stream_lock.unlock();
}

size_t fib( size_t n )
{
    if ( n <= 1 )
    {
        return n;
    }
    boost::this_thread::sleep( boost::posix_time::milliseconds( 1000 ) );
    return fib( n - 1 ) + fib( n - 2);
}

void CalcFib( size_t n )
{
    global_stream_lock.lock();
    std::cout << "[" << boost::this_thread::get_id()
        << "] Now calculating fib( " << n << " ) " << std::endl;
    global_stream_lock.unlock();

    size_t f = fib( n );

    global_stream_lock.lock();
    std::cout << "[" << boost::this_thread::get_id()
        << "] fib( " << n << " ) = " << f << std::endl;
    semaphore_fib = semaphore_fib-1;
    global_stream_lock.unlock();
}

void CalcFib2( size_t n )
{
    global_stream_lock.lock();
    std::cout << "\t\t[" << boost::this_thread::get_id()
        << "] Now calculating fib2( " << n << " ) " << std::endl;
    global_stream_lock.unlock();

    size_t f = fib( n );

    global_stream_lock.lock();
    std::cout << "\t\t[" << boost::this_thread::get_id()
        << "] fib2( " << n << " ) = " << f << std::endl;
    semaphore_fib2=semaphore_fib2-1;
    global_stream_lock.unlock();
}

int main( int argc, char * argv[] )
{
    boost::shared_ptr< boost::asio::io_service > io_service(
        new boost::asio::io_service
        );
    boost::shared_ptr< boost::asio::io_service::work > work(
        new boost::asio::io_service::work( *io_service )
        );
    boost::asio::io_service::strand strand( *io_service );

    global_stream_lock.lock();
    std::cout << "[" << boost::this_thread::get_id()
        << "] The program will exit when all work has finished."
        << std::endl;
    global_stream_lock.unlock();

    boost::thread_group worker_threads;
    for( int x = 0; x < 2; ++x )
    {
        worker_threads.create_thread( 
            boost::bind( &WorkerThread, io_service)
            );
    }
    for(int loop_no=0; loop_no<2; ++loop_no)
    {
        semaphore_fib=3;
        io_service->post( boost::bind( CalcFib, 5 ) );
        io_service->post( boost::bind( CalcFib, 4 ) );
        io_service->post( boost::bind( CalcFib, 3 ) );
        while(semaphore_fib>0)
        {
            // waiting
        }

        global_stream_lock.lock();
        std::cout << "[" << boost::this_thread::get_id()
            << "] ******* CalcFib group finished ********" << std::endl;
        global_stream_lock.unlock();

        semaphore_fib2=3;
        io_service->post( boost::bind( CalcFib2, 2 ) );
        io_service->post( boost::bind( CalcFib2, 1 ) );
        io_service->post( boost::bind( CalcFib2, 1 ) );
        while(semaphore_fib2>0)
        {
            // waiting
        }
        global_stream_lock.lock();
        std::cout << "[" << boost::this_thread::get_id()
            << "] ******* CalcFib2 group finished  ********" << std::endl;
        global_stream_lock.unlock();
    }
    work.reset();
    worker_threads.join_all();

    return 0;
} 

1 个答案:

答案 0 :(得分:0)

这就是我在if while循环中解决了太多main()个语句的问题。

  • 添加了boost::condition_variable cv;boost::mutex mx;作为全局变量
  • 已添加boost::lock_guard<boost::mutex> lk(mx);cv.notify_all();CalcFibCalcFib2函数
  • 的末尾
  • 已添加boost::unique_lock<boost::mutex> lk(mx); cv.wait(lk); 在每个while循环中

这样我就完成了if循环中的while语句仅在cv.notify_all()执行时执行。