0

How can I estimate the minimum amount of work it is worth spawning a new thread? I'm principally interested in C++11 std::thread.

Long explanation. I was trying to speed up our application so I went on parallelizing a low level function, that perform very few and simple operations (such as inequalities and assignments) in a loop. This function is the most time consuming in the application, though. So I tried to parallelize the loop spawning two threads for two consecutive indices of the loop, but this killed performance (I can't say exactly the slow down factor, but it could be around 100x).

Paolo M
  • 12,403
  • 6
  • 52
  • 73
  • 1
    Have you looked at openmp? http://bisqwit.iki.fi/story/howto/openmp/ I find it's often much easier to use than spawning in a whole new thread, especially if you just need to speed up loops here and there. – Bas in het Veld Apr 17 '15 at 07:43
  • 5
    *two consecutive indices* sounds very much like *false sharing* or cache thrashing. Your processors cache wont like it at all, there may never be any speedup. Better split the loop into continuous regions and assign the threads to these regions. E.g. first half to the first thread, second half to the second thread. – Markus Kull Apr 17 '15 at 07:49
  • Don't spawn a new thread, assign a task to an existing thread. Re-use your threads if you care about performance. – David Schwartz Apr 17 '15 at 07:59
  • @BasinhetVeld This will be my next try, thanks. – Paolo M Apr 17 '15 at 09:30
  • @MarkusKull The problem is that my loop got an exit condition, so it's very probable that the second half of the loop should always wait for the first half to finish. I'll deepen *false sharing* and cache issues anyway, it sounds quite interesting. – Paolo M Apr 17 '15 at 09:32
  • @DavidSchwartz How can it be achieved with C++11 std::thread? By the way, I've read here http://stackoverflow.com/a/14351457/2508150 that there's no much convenience in thread pooling under linux. – Paolo M Apr 17 '15 at 09:40
  • 1
    @PaoloM You can implement a thread-safe, waitable queue of `std::function` objects and have threads pull from that queue and execute the object. – David Schwartz Apr 17 '15 at 15:50
  • @DavidSchwartz Ok, but as far as I can see, C++11 std::threads are not re-usable. I can't see a way of re-assign a task to an existing std::thread. – Paolo M Apr 17 '15 at 16:45
  • @PaoloM Did you read the comment you are responding to?! It explained precisely how to do that! – David Schwartz Apr 17 '15 at 22:44
  • @DavidSchwartz I'm not trying to polemicize, really. I simply can't see how I can "Re-use your threads if you care about performance" using std::thread. I still don't get it, sorry. – Paolo M Apr 18 '15 at 06:30
  • @PaoloM Why don't you ask a separate question? Any competent programmer who has used threads should understand what "implement a thread-safe, waitable queue of std::function objects and have threads pull from that queue and execute the object" means. That's the basic way you re-use threads. – David Schwartz Apr 20 '15 at 02:46
  • Listen @DavidSchwartz, my English is not very good so I think I can't explain what I can't get. By the way, never told of being a "competent programmer who has used threads". Cheers. – Paolo M Apr 20 '15 at 06:49
  • @PaoloM Ask a question about how to re-use C++11 std::threads and I'll explain it. – David Schwartz Apr 20 '15 at 07:12
  • @DavidSchwartz Last week I tried, but it was marked as a duplicate of http://stackoverflow.com/questions/26516683/reusing-thread-in-loop-c. There is also http://stackoverflow.com/questions/26417369/in-c-how-can-i-reuse-a-standard-thread-that-has-finished-execution. If you can, answer to one of that questions and please let me know. Thanks. – Paolo M Apr 20 '15 at 07:31
  • @PaoloM [Here](http://stackoverflow.com/a/29742586/721269) you go. – David Schwartz Apr 20 '15 at 08:20
  • @DavidSchwartz Thank you! I'll try that out and see if I get some speed-up. – Paolo M Apr 20 '15 at 08:37

1 Answers1

1

Spawning a new thread is often an expensive operation (probably more in Windows, in Linux it is cheaper). One alternative is to use a thread pool design pattern. This make it possible to reuse existing threads without creating/destroying them all the time.

The minimum amount work when using multithreading is profitable is difficult to assess without additional knowledge about used architecture and environment which the application run in. Some experience or experimentation would be useful.

rubix_addict
  • 1,811
  • 13
  • 27