Proper way to code multithreaded applications?

Jason House jason.james.house at gmail.com
Fri Jun 1 22:04:14 PDT 2007


Sean Kelly wrote:
> Jason House wrote:
>> * derr is not synchronized.  Debug output from different threads can 
>> get multiplexed together.
> 
> In my opinion, this is the correct choice from a performance standpoint.

I may not appreciate the full impact on performance, but I can pretty 
much guarantee that jumbling the output of two different threads 
together is rarely what the user wants to see...


> 
>> * msleep isn't available on all platforms (problem inherited from C)
>> * usleep isn't thread safe (problem inherited from C)
> 
> This is only an issue because Phobos threads lack a sleep() routine. 
> Tango threads do not.  And to reply to Regan as well, nanosleep isn't 
> available everywhere either :-)  The most commonly implemented methods 
> are sleep() and usleep() in <unistd.h>.  In fact, I think these may 
> actually be required (nanosleep is a part of the realtime extensions IIRC).
> 
>> My big issue at the moment is how to cause a thread to delay 
>> (approximately) for a period of time that I specify.  I guess I might 
>> be ok with yield when the application is in full swing, but I really 
>> hate for nearly idle threads to peg the CPU when there's nothing to do.
> 
> You want sleep(), usleep(), or nanosleep() for Posix, and Sleep() or 
> SleepEx() for Win32.

It's annoying to have to worry about reentrant guarantees in addition to 
which are available on which platform.  I notice tango's thread library 
has a sleep method that (theoretically) would solve the problem.

I should add to my list that I don't see any mutexes in phobos.


More information about the Digitalmars-d-learn mailing list