Click to See Complete Forum and Search --> : Apis to support multithreading


Alphadan
September 13th, 2010, 07:16 PM
Hi, everyone

I'm looking for apis to support multithreading (something that might help me to debug or make the job lil easier).

my current problem is that i have a mulithreaded application (socket oriented: file transfer), made it especifically to support multi-filetransfer simultaneously.

The reciver part is working as expected but the sender(not 100% sure where is the trouble) is working as it was single threaded.

I think is executing the send procedure is made in a single connection thread or even in the main thread.

basicaly im looking for api or somethign that would allow me to know what threadID executes the current portion of the code. or at least a way to debug this problem.

Also how am i able to force a procedure to be executed in a specific thread, is there a way?


Thx in advance.

Lindley
September 21st, 2010, 08:35 AM
basicaly im looking for api or somethign that would allow me to know what threadID executes the current portion of the code. or at least a way to debug this problem.


Any debugger should include a means to view where each thread is at any given time. However, multithreaded debugging is still tricky. The valgrind tool for Linux comes with a helgrind mode which can assist with finding MT errors, although it does give a lot of false positives.


Also how am i able to force a procedure to be executed in a specific thread, is there a way?


You would need to queue up the function call (perhaps as a std::function if your compiler is new enough, or a boost::function if it's older) in a data structure which is periodically examined for things to run by the thread in question. This approach is commonly used in thread pool designs, or when dealing with a GUI to force rendering commands to be executed in the main GUI-controlling thread.

Alphadan
September 25th, 2010, 02:17 PM
Thx Lindley, very precise answer.

I have now a starting point to do my research.

I have heard of thread pool, sincerely have no idea what it is.

Also ima Learn how to use debuggers since, i need that.

Lindley
September 27th, 2010, 07:56 AM
I have heard of thread pool, sincerely have no idea what it is.


Consider the case where you have lots of small tasks you'd like to run in parallel. You could spawn a thread for each one, and then join all the threads to know they'd been completed. However, all that thread setup/teardown is expensive, which can reduce the benefit of multithreaded coding significantly.

In a thread pool design, you instead start a small number of threads, perhaps 1 or 2 more than you have cores available on your machine. Then you queue up tasks which one of the threads will acquire and execute as the threads become available. When all tasks are completed, the pool threads become idle but remain in existence, waiting for more tasks. This is often a more efficient design for this situation.