I h开发者_Go百科ave a piece of code which uses Boost threads to speed up the calculation, but I need to debug it and want to run them in series, not in parallel. How do I do that?
Unless I'm missing something, just debug it using a single thread. Forget about multi-threading unless you get the algorithm right.
Assuming you meant "to speed up the calculation", threads running in series will not help performance at all. Actually, it would cost you performance for the overhead on the threads, because you're not parallelizing any work.
If you're so inclined to run them in series, just make sure each one waits for the current thread to finish executing before allowing another to run? I'm probably missing something here.
You can create a semaphore for each thread, and then signal the 1st semaphore in the main thread, and each thread can signal the next semaphore at its end.
But, still, why do you need to debug your app this way? It is very useful to debug the app with all threads running so that you can see if any race conditions happen, or anything like that.
Put breakpoints in all your threads. Your debugger should have a command to step through or start just one thread. The rest of your threads will remain suspended, so they won't interfere with your single-threaded debugging. Once the one thread terminates, you can resume all the threads, or you can continue debugging in the next thread.
Assign only a single processor core to your process. On Windows, you can do so with SetProcessAffinityMask
精彩评论