views:

107

answers:

5

I have 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?

+1  A: 

Unless I'm missing something, just debug it using a single thread. Forget about multi-threading unless you get the algorithm right.

Justin Ardini
+1  A: 

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.

prelic
He wants the threads running in series to make debugging easier. Hence the "but i need to debug it and want to run them in series" part of his first sentence. It's safe to assume he'll want re-enable parallel threads later after fixing the bugs.
Emile Cormier
Exactly what I meant. Thank you, Emile.
gt6989b
+1  A: 

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.

Gianni
I was wondering if you could use a simple system call forcing it to run in 1 thread, like MSalters proposed below.
gt6989b
Well, in that case, just set breakpoints all over. It'll give you that kind of experience.
Gianni
+1  A: 

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.

Rob Kennedy
I was wondering if you could use a simple system call forcing it to run in 1 thread, like MSalters proposed below.
gt6989b
+1  A: 

Assign only a single processor core to your process. On Windows, you can do so with SetProcessAffinityMask

MSalters
This would not ensure that his threads will be serialized.
tibur
Exactly what I needed. I wanted to get the correct boost call to force the library to run 1 thread at a time, if there was one, to do this on Red Hat Linux.
gt6989b
@Gt6989b, although this will indeed make only one thread run at a time, all the threads will still be active, meaning the OS scheduler may choose to interrupt a thread at any time and let a different one run instead. Rather than running in serial or in parallel, your thread will run *interleaved*. It's hard for me to tell whether that's really what you want.
Rob Kennedy