NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: pthreads condition variables

On Thu, Nov 19, 2009 at 02:21:02PM +0000, 
> I was looking for a way to minimise the number of times you need to call
> pthread_cond_signal(), or pthread_mutex_lock(). It doesn't make much
> difference on a 4-core CPU, but as the number of cores increases, the
> overheads of each function call will quickly add up.

Not if the amount of work that each thread will do once started
is usefully large.  The amount of work involved in hitting a
pthread_cond_signal() is vastly tinier than that involved in
psycho-acoustically compressing 1/256th of a WAV file.

In the broader context, where this sort of branch/join problem
*is* significant, then the usual approach is to make the
funnelling time log(N) by using a branching tree structure of
branchers/joiners.  I doubt that that level of complexity is
necessary for this sort of embarrasingly-parallel task, though.

Or is it?  Your setup seems a bit artificial, I think.
Ultimately meda-file encoding *is* a streaming exercise, with a
single input and output channel.  The amount of embarrasing
parallelism that you can expose depends, then, on how much
latency from input to output (and consequently how much memory)
you can give to the task.  You've described one extreme, but
there is a continuum, usually.

> The best workaround I came up with, was to divide all threads into
> groups, so 256 parallel threads would be arranged into something like
> 32 groups of 8 threads per group. Then all 8 threads in a group would
> sleep on the same condition variable, minimising the 'thundering herd'
> problem. You would have to step through 32 groups and broadcast to all
> 8 threads in a group.

That's kind of a two-tier approach the the full tree structure
of activation.  Should work fine, but I still don't think that
the one-tier linear approach (a condition var per thread) will
be a problem either.

> Does this make sense, or I'm doing something completely wrong?

I think that you're over-stating or mis-stating the problem.



Home | Main Index | Thread Index | Old Index