开发者

Which boost multithreading design pattern should I use?

开发者 https://www.devze.com 2023-02-02 02:38 出处:网络
I\'m getting started with boost for multi-threading to port my program to window ( from pthread of linux ) . Is there anyone familiar with it ? Any suggestion on which pattern should I use ?

I'm getting started with boost for multi-threading to port my program to window ( from pthread of linux ) . Is there anyone familiar with it ? Any suggestion on which pattern should I use ?

Here are the requirements:

  • I have many threads most of the time running the same thing with different parameters,
  • All threads shared a memory location called "critical memory" (an array)
  • Synchronization has to be done with a "barrier" at certain iteration
  • requires highest parall开发者_如何学Pythonelization if possible i.e good scheduling with same priority for all threads ( currently I let CPU does the job, but I find out that boost has threadpool with thread.schedule() not sure if i should use )

For pthread, every thing is function, so I'm not sure if I should convert it to object, what's the advantage then ?. A little bit of confusion after reading this tutorial http://antonym.org/2009/05/threading-with-boost---part-i-creating-threads.html so many options to use...

Thanks in advance


porting should be quite straightforward:

  • I have many threads most of the time running the same thing with different parameters,

create required number of threads with functor that binds your different parameters, like:

boost::thread thr1(boost::bind(your_thread_func, arg1, arg2));
  • All threads shared a memory location called "critical memory" (an array)

nothing special here, just use boost::mutex to synchronize access (or another mutex type if you have special requirements)

  • Synchronization has to be done with a "barrier" at certain iteration

use boost::barrier: http://www.boost.org/doc/libs/1_45_0/doc/html/thread/synchronization.html#thread.synchronization.barriers

  • requires highest parallelization if possible i.e good scheduling with same priority for all threads ( currently I let CPU does the job, but I find out that boost has threadpool with thread.schedule() not sure if i should use )

CPU? probably you meant OS scheduler. it's the simplest possible solution and in most cases satisfactory one. threadpool is not a part of boost and tbh I'm not familiar with it. boost thread doesn't have scheduler.

I don't know anything about your task and its parallelization potential, so I'll suppose it can be parallelized to more threads than you have cores. theoretically, to receive highest performance you need to smoothly distribute your work among thread number = number of your cores (including virtual ones). it's not the easiest task and you can use ready-for-use solutions. e.g. Intel Threading Building Blocks (GPL license) or even Boost Asio. Despite its main purpose is network communication, Asio has its dispatcher and you can use it as a thread pool. Just create optimal number of threads (number of cores?), boost::asio::io_service object and run it from all threads. post work to thread pool by io_service::post()


In my opinion, to use win32 port of pthread is more straightforward way to accomplish such tasks.

EDITED:

Last month I converted 1 year old project source code to pthread-w32 from Boost.Thread. Boost.Thread provides lots of good thing but if you are working on the old fashion thread routines Boost.Thread could be too mush hassle.

0

精彩评论

暂无评论...
验证码 换一张
取 消