Subj : Re: I write a multithread program with pthread, and meet a program To : comp.programming.threads From : loic-dev Date : Mon Apr 11 2005 01:56 am Hello, > Yes. If commenting the point 1 as you said: > pthread_mutex_unlock(&mutex); > // cout<<"------------thread start"< while(true){ > It can work well. > But I do not know why it works well after I comment the line. You didn't say which OS/Pthreads implementation you are using. Is it Linux/NPTL, by chance? Notice that nothing is said about cout being a cancellation point or not. A correct implementation should get this right. But you're surely on the safe side if you explicitely disable cancellation where you are writting on cout. [ as a matter of fact, with Linux/NPTL v0.61 I got the following uncaught exception: FATAL: exception not rethrown. The number of threads created depends from run to run. ] > You said it should have a predicate associated to the condition > variable. Yes, correct. Because you might be subject to spurious wake-up. > But the reason that I use a condition variable here is that I want to > the new thread runs before the main thread call pthread_cancel() to > stop it. > If I do not use a condition variable, it is possible that the main > thread start the new thread and call pthread_cancel() to stop it before > the new thread start to run. Yes, yes I understand that already. But theoritically, you might be woken up by a spurious signal, even if your thread doesn't run yet. Cheers, Loic. .