Topics trying to acquire pthread_mutex_lock (& ​​mutex) What happens if they don't get a lock?

C Programming:

What happens when a thread tries to obtain a mutex lock and does not receive it?

Does he fall asleep?

Will the thread wake up when pthread_mutex_unlock (& ​​mutex); is called?

Then try to get the lock again?

+3
source share
4 answers

On the page :

The function blocks the mutex. If the mutex is already locked, the calling thread will block until the mutex is available. pthread_mutex_lock()

So, yes - your thread is blocked until the lock is available and it can receive it.

+5
source

, , .

pthread_mutex_trylock(pthread_mutex_t *mutex) EBUSY, - , 0, . (, , - )

+2

pthread_mutex_lock , , , (). :

  • EAGAIN, .
  • EDEADLK, , .
  • EOWNERDEAD, , () . , , , , pthread_mutex_consistent.
  • , pthread_mutex_unlock pthread_mutex_consistent, ENOTRECOVERABLE.

, . : (PTHREAD_MUTEX_NORMAL type) , , , , .

+1

POSIX:

, , .

(...)

mutex, mutex, pthread_mutex_unlock(), , , .

"result in" ,

( PTHREAD_MUTEX_RECURSIVE , , .)

0

All Articles