You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-user@logging.apache.org by Anatoly Kournik <An...@enigma.com> on 2004/09/13 10:15:02 UTC

Thread::sleep() implementation bug on Solaris

Hi,
 
using usleep in Thread::sleep() implementation on Unix(Solaris) is
problematic in multithreaded environment
because  it's implemented by setting an alarm signal and pausing until it
occurs.
Since SIGALRM shouldn't be blocked or ignored during a call, if context
switch occurs when it's delivered, the programm simply quits (default action
on SIGALRM).
The proper way is to use nanosleep function. It has no effect on the action
or blockage of any signal.
 
Here is an example of nanosleep usage.
I've encapsulated call to nanosleep in signal blocking/unblocking routines
in order to prevent signals delivery. 
 
sigset_t newset, oldset;

sigfillset(&newset);

pthread_sigmask(SIG_BLOCK, &newset, &oldset);//block signals

struct timespec sleeptime;

sleeptime.tv_sec = ms / 1000; //argument is supplied in milliseconds

sleeptime.tv_nsec = 0; 

if(nanosleep(&sleeptime, NULL)) perror("nanosleep");

pthread_sigmask(SIG_SETMASK, &oldset, NULL);//restore signals

--
Toly Kournik
 
--------------------------------------------------------------------------------------------------------------------------------------------------------------
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom
they are addressed. If you have received this email in error please notify the originator of the message.
Scanning of this message is performed by SurfControl E-mail Filter software in conjunction with  virus detection software.