An easy question ..
I read before saying - " Being a mutex "
But I have written a program where THREAD1 locks the mute xvara and goes to sleep i THREAD2 directly Mute can unlock Xvar and can do some work.
==> I know everyone says that why am I doing this ?? But my question is - is this the correct behavior of mutex ??
== & gt; Adding sample code
void * functionC () {pthread_mutex_lock (and mutex1); Counter ++; Sleep (10); Printf ("Thread 01: Counter Value:% d \ n", Counter); Pthread_mutex_unlock (and mutex1); } Zero * functionD () {pthread_mutex_unlock (and mutex1); Pthread_mutex_lock (and mutex1); Counter = 10; Printf ("counter value:% d \ n", counter); } Int main () {int rc1, rc2; Pthread_t thread 1, thread 2; If (pthread_mutex_init (and mutex1, NULL)) printf ("Error while using pthread_mutex_init \ n"); If ((RC1 = pithread_create (and thread 1, faucet, and function C, tap)) {printf ("thread creation failed:% d \ n", rc1);} if ((RC2 = pathread_create (and thread 2 , Tap, and function D, tap)) {printf ("Thread creation failed:% d \ n", rc2);}
What you did is not only legal, and the behavior is undefined. Mutes only take out the thread according to the rules. If you tried to lock mutex1 from Thread 2, the thread Will be blocked, sure As, if it is necessary it is not anything device which tells you attempt to unlock a mute X that you do not own!
Comments
Post a Comment