1
votes

I am not knowledgeable enough in multi-thread programming. I my C++ program I make 10 pthreads which each are responsible to process a part of realtime data streamed to the system. Eventually inside the main(), the processed data from all threads are merged. To my knowledge the exact number of threads that can be run in parallel is determined by the Cores per socket x Threads per socket x sockets which in my case is 8 (4 x 2 x 1). So is it the case that in my application with 10 threads no palatalization is happening and swapping between different threads is what the scheduler does? if not is there any way to determine this? or at least can I know how many milliseconds does this swapping takes? Since my application is dealing with real time data, its crucial to make sure all the threads are running in parallel.

2
"Swapping" between different threads of execution have been happening since multitasking was invented 50 or 60 years ago. If your system supports 8 simultaneous threads then such swapping needs to happen. And that swapping also includes all the other programs and their processes and threads running on your system.Some programmer dude
std::thread::hardware_concurency may be of interest to you.François Andrieux

2 Answers

2
votes

"To my knowledge the exact number of threads ... "

of a sort - there's also hyper-threading that scews things; and there's also the fact that C++ can be run on non-PC's. (ie embedded devices) which will have a static number.

"is what the scheduler does"

Yes. But not just your threads; all the threads, of the OS, and other applications also get controlled by this.

or at least can I know how many milliseconds does this swapping takes

No; and nor does it matter since you can't do anything about it. The best you can do is try to track how much CPU time you've had on your thread, work out the total CPU time available to a single thread; and then you can work out what % of the time you've had; but again, you shouldn't need to do this ever because it likely means your program is worrying about things outside it's control. More meaningful would be to monitor the CPU usage of the machine.

Since my application is dealing with real time data, its crucial to make sure all the threads are running in parallel.

It's not up to you about how your threads run; it's up to the OS. There are other more important tasks on your computer than your program. For example the OS needs processing time to handle any requests for memory made by applications.

Some OS's are actually able to do do this in a more specialised way; Qnix for example is a real time OS; but this comes at other costs.

One thing you can be sure though; is that if you have a maximum number of possible threads running as 8; and you create 10; you will not have them all running in parallel. You may wish to look into some libraries that will help ensure that the threads you create are always busy; boost::asio for example

-1
votes

I can not add comments yet so part as answer.

or at least can I know how many milliseconds does this swapping takes

On Windows this is default 10 - 20 ms With a little trick you can change this to 1 - 2 ms

#include <Mmsystem.h>
#pragma comment(lib,"winmm.lib")
timeBeginPeriod(1); // set system clock to 1ms from default 10ms

If you want to add another dirty trick to give your process more power. You need to add the 2 lines below in the main() process and you also need to do this 2nd line for every thread.

SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_NORMAL);

Be warned !!!! You will make windows unresponsive if you do this with more threads than there are processing cores. So you need to give the system some time in the threads.

On Linux the default is already 1 ms (dynamic) but you can change between different scheduler.