Sunday 15 June 2014

pthread and condition variable -


I wonder why the following code returns unexpected output: a 110 can get ...! pthread_t th [nbt]; Zero * Funk (Zero * D) {While (a & lt; 100) {pthread_mutex_lock (& ​​amp; l); Cout & lt; & Lt; A & lt; & Lt; "In thread" & lt; & Lt; Pthread_self () & lt; & Lt; "\ N"; A + = 1; Pthread_mutex_unlock (& ​​amp; nbsp;); } Return tap; } Int main (int argc, const char * argv []) {for (int i = 0; i & lt; nbt; i ++) pthread_create (and (w [i]), faucet, funk, null); For (int i = 0; i / p> Em> is checking the status, so you do not know whether it is still correct or not, once you get locked you should simply double-check a simple:

  while (a & lt; 100) {pthread_mutex_lock (& ​​amp; l); Cout & lt; & Lt; A & lt; & Lt; "In thread" & lt; & Lt; Pthread_self () & lt; & Lt; "\ N"; If (a & lt; 100) a + = 1; // & lt; == Added condition is here! Pthread_mutex_unlock (& ​​amp; nbsp;); }    

No comments:

Post a Comment