Hello,
I have trouble using nanosleep() in an app where main starts 4 threads
which should run periodically:
most of the time, some of the threads remain in NANOSLEEP state
and main can’t JOIN them
– equivalent code:
f1 … f4() {
while(!termination) {
…
nanosleep(20ms);
}
}
main() {
pthread_t t1 … t4;
pthread_create(&t1, f1,…);
…
pthread_create(&t4, f4,…);
nanosleep(10s);
//signal threads for termination
pthread_join(t1,…);
…
pthread_join(t4,…);
}
– end
The application was first designed on linux (2.4.21 - Mandrake 9.1) and
was correctly running back then…
What am I doing wrong ?
Thanks,
Yvain Thonnart
I use QNX RTP 6.1, because I need mkifs, and my budget is not that big… 
Yvain Thonnart wrote:
main() {
pthread_t t1 … t4;
pthread_create(&t1, f1,…);
…
pthread_create(&t4, f4,…);
nanosleep(10s);
Are you checking the return value of nanosleep and any error codes?
According to the doc, it’s an error to ask for more than 1000 million
nanoseconds (1 second). My theory is that this nanosleep is failing and
the threads are getting termination signals before they’re completely
created. Just an idea.
//signal threads for termination
pthread_join(t1,…);
…
pthread_join(t4,…);
}
Cheers,
Kris
Thanks for the idea,
Actually, I just found what the problem was,
obviously my mistake and not a nanosleep bug
The thing was nanosleep didn’t return at all, so I checked which
timestruct I was passing as request just before the crash:
‘time_t tv_sec’ is coded as an unsigned int, while it was signed under
linux. My arithmetic on timestructs was simple and I could get negative
timestructs in certain cases : nanosleep was therefore called for a
MAXINT seconds sleep !!
Mea Culpa, Mea Maxima Culpa,
Yvain
Kris Warkentin wrote:
Yvain Thonnart wrote:
main() {
pthread_t t1 … t4;
pthread_create(&t1, f1,…);
…
pthread_create(&t4, f4,…);
nanosleep(10s);
Are you checking the return value of nanosleep and any error codes?
According to the doc, it’s an error to ask for more than 1000 million
nanoseconds (1 second). My theory is that this nanosleep is failing and
the threads are getting termination signals before they’re completely
created. Just an idea.
//signal threads for termination
pthread_join(t1,…);
…
pthread_join(t4,…);
}
Cheers,
Kris