linux - Pthread Mutex lock unlock by different threads -


a naive question ..

i read before saying - "a mutex has unlocked thread locked it."

but have written program thread1 locks mutexvar , goes sleep. thread2 can directly unlock mutexvar operations , return.

==> know why doing ?? question - right behaviour of mutex ??

==> adding sample code

void *functionc() {    pthread_mutex_lock( &mutex1 );    counter++;    sleep(10);    printf("thread01: counter value: %d\n",counter);    pthread_mutex_unlock( &mutex1 ); }  void *functiond() {    pthread_mutex_unlock( &mutex1 );    pthread_mutex_lock( &mutex1 );    counter=10;    printf("counter value: %d\n",counter); }  int main() {    int rc1, rc2;    pthread_t thread1, thread2;     if(pthread_mutex_init(&mutex1, null))    printf("error while using pthread_mutex_init\n");     if( (rc1=pthread_create( &thread1, null, &functionc, null)) )    {          printf("thread creation failed: %d\n", rc1);    }        if( (rc2=pthread_create( &thread2, null, &functiond, null)) )    {          printf("thread creation failed: %d\n", rc2);    }  

what you've done not legal, , behavior undefined. mutexes exclude threads play rules. if tried lock mutex1 thread 2, thread blocked, of course; that's required thing do. there's nothing in spec says happens if try unlock mutex don't own!


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -