gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:20:53 24 4
gpt4 key购买 nike

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

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

我创建了两个错误命名为 semaphore_** 的 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;
}

最佳答案

我就是这样解决了太多if while 中的陈述循环 main() .

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

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

关于c++ - boost asio 类似信号量的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7793895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com