Sign in to follow this  
EmpireProductions

MultiThreading

Recommended Posts

I have a question about making a application Multithreaded. What is NEEDED to make a app. multithreaded? I have this: boost::thread netupdate(boost::bind(&WorldNetTranslator::update, netTranslator)); netupdate.join(); Is that all that is needed or is there more that I am missing?

Share this post


Link to post
Share on other sites
Quote:
Original post by EmpireProductions
I have a question about making a application Multithreaded. What is NEEDED to make a app. multithreaded?

I have this:

boost::thread netupdate(boost::bind(&WorldNetTranslator::update, netTranslator));
netupdate.join();

Is that all that is needed or is there more that I am missing?


That is all you need to run netTranslator.update() in another thread yes. There's of course a lot more to multithreading in general, but if that's all you need then there's nothing else to it.

Share this post


Link to post
Share on other sites
Quote:
Original post by EmpireProductions
Ok thanks. Is there any special ways you should program your classes and methods you want run in other threads and if so what should I be taking into consideration and doing in order to get the best results?


Yes :)

Learn about the different uses for concurrency (throughput vs responsiveness) and which idioms are used for each. Think in terms of tasks rather than threads. Use various kinds of queues, pools and parallel patterns to perform those tasks. Reduce shared state.

Read all of this. I found this to be quite good, too.

If you're more interested in getting stuff done and don't really care to know what's going on under the hood, then I suggest using established frameworks such as Intel's Threaded Building Blocks.

Share this post


Link to post
Share on other sites
Quote:
Original post by EmpireProductions
Is there any special ways you should program your classes and methods you want run in other threads and if so what should I be taking into consideration and doing in order to get the best results?
It requires a slightly different way of thinking.

The code does not follow one single thing at a time, but it is instead at any number of places at the same time. When debugging you aren't at a single location, but at one location in every thread. You might be in two, or five, or a hundred different places at once.

The added dimension of being in multiple places at once introduces an extra dimension of bugs such as race conditions and deadlock behaviors.

It is not difficult or labor intensive to code correctly, but it certainly takes some effort.


The world of parallel processing has many algorithms and patterns available.

The most simple and trivial example is distribution of work through tasks and transactions. This is a worker pool with tasks spread out to the first available processor. It is easy to implement and therefore one of the most common and popular algorithms.

That pattern doesn't offer much in terms of algorithmic benefit. It doesn't offer much benefit in terms of optimization. It causes slowdowns when processing is not your bottleneck. It can mean unnecessary data dependencies and locking. ... But it is the first (and frequently only) pattern that many people use, and it is important to learn.


More efficient applications of parallel processing require more intellectual work, and are therefore shunned. [grin] The steps are basically to subdivide the work into the component tasks, determine the dependencies and communications needs, group them to minimize those costs, and then map them to the appropriate processes. Then you implement it. The results can be substantially better than simply running serial algorithms in a master/worker pattern.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this