c - `pthread_mutex_trylock` and `pthread_mutex_lock` behaviour -
this follow this question.
in code, when not using fflush(stdout)
output not flushed screen when kept sleep(1)
.
#define s sleep(0)
void* xthread_fn(void* arg) { while(1) { s; pthread_mutex_lock(&read_c_mutex); if(!read_c) { pthread_mutex_unlock(&read_c_mutex); printf(" x"); } else { pthread_mutex_unlock(&read_c_mutex); pthread_exit(null); } fflush(stdout); <---this 1 here } }
but when keep sleep(0)
there no need fflush(stdout)
, output updated on stdout
. why so?
q1. why should presence of sleep(0)
cause change in way output flushed?
if modify code follows (to track execution),
#define s sleep(1) int read_c = 0; pthread_mutex_t read_c_mutex = pthread_mutex_initializer; void* inputthread_fn(void* arg) { printf("%p input\n",pthread_self()); char inputchar; int = 0; while(1) { s; printf("\nchecking input"); scanf("%c",&inputchar); if(inputchar=='c' || inputchar == 'c') { pthread_mutex_trylock(&read_c_mutex); printf("%p has lock %d\n",pthread_self(),i); read_c = 1; pthread_mutex_unlock(&read_c_mutex); printf("%p has unlocked %d\n",pthread_self(),i++); printf("%p gone!\n",pthread_self()); fflush(stdout); pthread_exit(null); } } } void* xthread_fn(void* arg) { int = 0; printf("%p x\n",pthread_self()); while(1) { s; printf("x trying lock\n"); pthread_mutex_trylock(&read_c_mutex); printf("%p has lock %d\n",pthread_self(),i); if(!read_c) { pthread_mutex_unlock(&read_c_mutex); printf("%p has unlocked %d\n",pthread_self(),i++); printf("x\n"); fflush(stdout); } else { printf("%p gone!\n",pthread_self()); pthread_mutex_unlock(&read_c_mutex); fflush(stdout); pthread_exit(null); } } } void* ythread_fn(void* arg) { printf("%p y\n",pthread_self()); int = 0; while(1) { s; printf("y trying lock\n"); pthread_mutex_trylock(&read_c_mutex); printf("%p has lock %d\n",pthread_self(),i); if(!read_c) { pthread_mutex_unlock(&read_c_mutex); printf("%p has unlocked %d\n",pthread_self(),i++); printf("z\n"); fflush(stdout); } else { printf("%p gone!\n",pthread_self()); pthread_mutex_unlock(&read_c_mutex); fflush(stdout); pthread_exit(null); } } }
a sample output
0xb6700b70 input 0xb6f01b70 y 0xb7702b70 x checking inputy trying lock 0xb6f01b70 has lock 0 0xb6f01b70 has unlocked 0 z x trying lock 0xb7702b70 has lock 0 0xb7702b70 has unlocked 0 x y trying lock 0xb6f01b70 has lock 1 0xb6f01b70 has unlocked 1 z x trying lock 0xb7702b70 has lock 1 0xb7702b70 has unlocked 1 x y trying lock 0xb6f01b70 has lock 2 0xb6f01b70 has unlocked 2 z x trying lock 0xb7702b70 has lock 2 0xb7702b70 has unlocked 2 x y trying lock 0xb6f01b70 has lock 3 0xb6f01b70 has unlocked 3 z x trying lock 0xb7702b70 has lock 3 0xb7702b70 has unlocked 3 x y trying lock 0xb6f01b70 has lock 4 0xb6f01b70 has unlocked 4 z x trying lock 0xb7702b70 has lock 4 0xb7702b70 has unlocked 4 x c y trying lock 0xb6f01b70 has lock 5 0xb6f01b70 has unlocked 5 z x trying lock 0xb7702b70 has lock 5 0xb7702b70 has unlocked 5 x 0xb6700b70 has lock 0 0xb6700b70 has unlocked 0 0xb6700b70 gone! y trying lock 0xb6f01b70 has lock 6 0xb6f01b70 gone! x trying lock 0xb7702b70 has lock 6 0xb7702b70 gone!
q2. have made use of pthread_mutex_trylock()
because wanted code proceed in while loop till acquires lock check value of read_c
. same appears achieved pthread_mutex_lock();
. confuses me more. pthread_mutex_trylock();
too, output this? 1 x
followed 1 z
. won't there situation might x
x
z
x
(provided thread switched os , ythread tried lock , failed)?
answering "trylock" question.
pthread_mutex_trylock
attempts lock mutex. if else has mutex locked, return error , keep running. since you're not checking return value might touch data protected mutex without holding mutex.
your code equivalent not having mutexes @ all.
pthread_mutex_trylock
should used in situations special reason can't wait lock , fall different behavior when acquiring mutex has failed. calling without checking return value bug.
to correct, should check return value of pthread_mutex_lock
too. can away not doing that. can never away not checking return value of trylock.
Comments
Post a Comment