boost asio 类似信号量的解决方案

发布于 2024-12-10 01:07:42 字数 4535 浏览 1 评论 0原文

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

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

我创建了两个 int 变量,错误地命名为 semaphore_**,并在上述函数中递减它们。等待作业组完成的代码是简单的 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;
} 

This question is follow up of this question. Anyhow, this is the motivation - a bit of c/p from original question's comment:

I'd like to be able to post one group of jobs on multiple threads (CalcFib functions), and then when the jobs are over, another group of jobs (CalcFib2 functions), also on multiple threads. This cycle loops many times(here is two), so I thought the best thing to do is to create boost::asio::io_service and create threads at the beginning of the loop, so I don't have to create & destroy threads every time the loop starts/ends.

I had created two int vars wrongly named semaphore_**, and decrement them in above mentioned functions. The code that waits for the job group to finish is plain while as seen below. The solution works, at least as I can see.

Is the wait using while really a way to go? What am I missing? Is there a better way to do this?

My code looks like this:

#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;
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城已无爱 2024-12-17 01:07:43

这就是我解决 main()while 循环中过多 if 语句的方法。

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

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

This is how I solved too many if statements in while loop in main().

  • Added the boost::condition_variable cv; and boost::mutex mx; as global variables
  • Added boost::lock_guard<boost::mutex> lk(mx);cv.notify_all();
    at the end of CalcFib and CalcFib2 functions
  • Added boost::unique_lock<boost::mutex> lk(mx); cv.wait(lk);
    in every while loop

That way I had accomplished that the if statement in while loop gets executed only when cv.notify_all() executes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文