boost asio 类似信号量的解决方案
这个问题是 的后续问题这个问题。无论如何,这就是动机 - 来自原始问题评论的一点 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我解决
main()
中while
循环中过多if
语句的方法。boost::condition_variable cv;
和boost::mutex mx;
作为全局变量boost::lock_guard; lk(mx);cv.notify_all();
在
CalcFib
和CalcFib2
函数末尾boost::unique_locklk(mx); cv.wait(lk);
在每个
while
循环中这样我就完成了
while
循环中的if
语句仅在cv.notify_all()< 时执行/code> 执行。
This is how I solved too many
if
statements inwhile
loop inmain()
.boost::condition_variable cv;
andboost::mutex mx;
as global variablesboost::lock_guard<boost::mutex> lk(mx);cv.notify_all();
at the end of
CalcFib
andCalcFib2
functionsboost::unique_lock<boost::mutex> lk(mx); cv.wait(lk);
in every
while
loopThat way I had accomplished that the
if
statement inwhile
loop gets executed only whencv.notify_all()
executes.