RE: [pthreads-devel] Re: program gets stuck

"Perez-Gonzalez, Inaky" <[email protected]> Tue, 1 Oct 2002 11:34:10 -0700
Newsgroups gmane.linux.ngpt.user,gmane.linux.ngpt.devel
Message-ID <[email protected]>
>  Then I run ps and grep for the name of the program and see that four
> threads are running, two spawned by the main thread, and a fourth one
> spawned by the third one. So I attach gdb to each one of them, and
> printing the stack of unction calls I see the last two 
> threads are calling
> nanosleep(), that we use before calling sched_yield(); to 
> yield cpu time
> to the other threads.

Well, you are not doing the right thing. Never rely on sleep, sched_yield or
pthreads specific yield for sharing CPU time. The system will behave
randomly and will depend on the implementation - not to mention that if you
use different priorities, probably some of your threads will always starve.

Use mutexes or conditional variables or barriers if you need to make sure
that all the threads do some work. They are there for that.

Inaky Perez-Gonzalez -- Not speaking for Intel - opinions are my own [or my
fault]