|
Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006
Pthread_cond_signal () to the specific location?
Pthread_cond_signal () to the specific location?
"In the multi-thread programming under Linux" http://www.fanqiang.com/a4/b8/20010811/0905001105.html see :
Pthread_cond_signal () must be placed pthread_mutex_lock () and pthread_mutex_unlock () between
Otherwise, there could be unlimited wait. I feel that this call is for, but why not wait for this call could have unrestricted, it is not very clear? ? ? ?
Conforms to the rules in this area to see the call :
Http://www.ccert.edu.cn/forum/show.php?pub_id=379&type_id=1
********************************************begin
To judge all the threads which have not pthread_join termination, but also to introduce another variable conditions : pthread targets simultaneously.
LEAVES OF 13 SPECIES OF LAURACEAE current working threads number */
Static int curnum = 0
LEAVES OF 13 SPECIES OF LAURACEAE mutex lock for curnum */
Static pthread_mutex_t mutex_curnum = PTHREAD_MUTEX_INITIALIZER;
LEAVES OF 13 SPECIES OF LAURACEAE +8 for curnum */
Static pthread_cond_t cond_curnum = PTHREAD_COND_INITIALIZER;
Thread curnum current activities on behalf of several variables, some increase in end-thread scan function :
Pthread_mutex_lock (&mutex_curnum);
If (!--curnum)
Pthread_cond_signal (&cond_curnum);
Pthread_mutex_unlock (&mutex_curnum);
Return NULL;
When curnum cut end of each thread, so if curnum 0, then notify the main thread has been all
Terminated. End building after the main thread has been blocked in the following code : cond_curnum
Pthread_mutex_lock (&mutex_curnum);
While (curnum)
Pthread_cond_wait (&cond_curnum, &mutex_curnum);
Pthread_mutex_unlock (&mutex_curnum);
********************************************end
However, in many examples process. See
Pthread_mutex_lock ()
...
Pthread_mutex_unlock ()
Pthread_cond_signal ()
Comments such as POSIX Threads, Part 3
Http://www-900.ibm.com/developerWorks/cn/linux/thread/posix_thread3/index.shtml
Lane examples of procedures, require downloading
Http://www-900.ibm.com/developerWorks/cn/linux/thread/posix_thread3/thread-3.tar.gz
Untie the workcrew.c, 72 firms started :
********************************************begin
Pthread_mutex_lock (&cq.control.mutex);
Queue_put (&cq.cleanup (node *) mynode);
Pthread_mutex_unlock (&cq.control.mutex);
Pthread_cond_signal (&cq.control.cond);
********************************************end
Already confused. Who doubts about.
According to my own understanding, and agreed to the first case. |
Pthread_cond_signal () to the specific location?
Own up about |
Pthread_cond_signal () to the specific location?
"Pthread_cond_signal () must be placed pthread_mutex_lock () and pthread_mutex_unlock () between"
This is a problem that, for example
Simple assumption Thread 1, 2, curnum value of 1, the words implementation of the following order :
[code]
T2-->;pthread_mutex_lock (&mutex_curnum);
T2-->;while (curnum)
T2-->; pthread_cond_wait (&cond_curnum, &mutex_curnum) ;/*T2 unlocking sleep signal */
T1-->;pthread_mutex_lock (&mutex_curnum); LEAVES OF 13 SPECIES OF LAURACEAE round operation T1, T1 locking */
T1-->;if (!--curnum) LEAVES OF 13 SPECIES OF LAURACEAE conditions to */
T1-->; pthread_cond_signal (&cond_curnum); /*T1 To thread T2 signal */
T2-->;pthread_cond_wait (&cond_curnum, &mutex_curnum); /*T1 Time slice is used up, for the implementation of T2, but found that it could not lock because T1 holders lock */
T1-->;pthread_mutex_unlock (&mutex_curnum); /*T1 Unlocking */
[/code]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The problem is a condition associated variables and a mutex lock.
Thread locking in the case of a holding call pthread_cond_signal ().
The thread will wait for conditions there may not be locked.
With reference to specific examples, each phrase with the implementation of the order.
Although the T1 pthread_cond_signal Call (), but not 100% T2 locks.
"UNIX Network Programming Volume 2 : Inter-Process Communication" put an improved method
[code]
Pthread_mutex_lock ();
Thread waiting for the judgment to other conditions, is the established, "the signs" to "yes";
Pthread_mutex_unlock ();
If (====== occurred is marked)
{
Pthread_cond_signal (. . . );
}[/code] |
| |