Log in

View Full Version : question about multithreading


codeguru
3rd November 2007, 13:36
Hi folks,
I modified a vdub filter to support up to 16 CPUs, using the CreateThread() function.

Now my question - is it better to create threads dynamically (e.g. 4 threads per filtered picture) which will calc immediately (and exit) after being created or having threads created once with the flag CREATE_SUSPEND (parameter 5) in the background? A repeated call of CreateThread() should be cached, and the second way will need to implement somme callbacks for message handling, that will eat up the performance gain of creating threads only once in a filter usage.

There are no information about the latencies due to the system message passing, and the timertick is too short for precise measurements in microseconds.

At least a Q6600 has enough cache to hold 1 kb of code so my choice was this:

void dodnrMMX_schedule(Pixel32 *dst, Pixel32 *src, PixDim w, PixDim h, PixOffset modulo, __int64 thresh1, __int64 thresh2, void *p, unsigned long nT)
{
dnrparams_t dnrp[MAXTHREADS];
HANDLE threadHandles[MAXTHREADS];
int n;
int delta;
void *pparams;
for(n=0;n<nT;n++)
{
....
some code for dividing src and dst in smaller pieces
...
threadHandles[n]=CreateThread(
NULL,
0,
dodnrMT,
&dnrp[n],
0,
&dnrp[n].threadid);
}
WaitForMultipleObjects(
nT,
threadHandles,
TRUE,
INFINITE);
}

foxyshadis
3rd November 2007, 20:15
If you can't measure it, there really is no practical difference. Stick with ease of use, maintainability, and scalability in that case, whatever they happen to be here.

If you have access to it, have you considered OpenMP? It uses a threadpool, in case you'd like to test a mature implementation of the idea.

JohnnyMalaria
3rd November 2007, 21:50
It's always recommended to use a thread pool and reuse threads. There is a considerable overhead for creating and destroying them.

Additionally, you may want to consider using the SetThreadAffinityMask() function to ensure that each thread does in fact run on its own processor. If you do, make sure you determine how many cores are actually present (I assume you are doing that anyway since you pass the variable nT to the function).

Both the Windows API and OpenMP provide thread pools. The WIN32 API for threads has been around longer than OpenMP and Microsoft's Visual C/C++ compiler supports both. Personally, I favor WIN32 for my particular needs (I have more control over it but it requires more effort to code). You can freely mix both in your code. Visual Studio has extensive documentation about OpenMP.

There are no information about the latencies due to the system message passing, and the timertick is too short for precise measurements in microseconds.

Do you mean the standard Windows timer? It has a resolution of about 10ms at best. You can use QueryPerformanceCounter() to get much higher resolution. The counter is on the processor itself and increments every clock cycle.