Pthread_cond_wait () wake up two threads simultaneously

I am trying to better understand how to use it pthread_cond_wait()and how it works. I'm just looking for clarifications on the answer I saw on this site.

Answer last answer on this page

understanding pthread_cond_wait () and pthread_cond_signal ()

I am wondering what it will look like with three threads. Imagine that thread 1 wants to tell thread 2 and thread 3 to wake up

for instance

pthread_mutex_t mutex;
pthread_cond_t condition;

Theme 1 :

pthread_mutex_lock(&mutex);

/*Initialize things*/

pthread_mutex_unlock(&mutex);
pthread_cond_signal(&condition); //wake up thread 2 & 3

/*Do other things*/

Theme 2 :

pthread_mutex_lock(&mutex); //mutex lock
while(!condition){
    pthread_cond_wait(&condition, &mutex); //wait for the condition
}
pthread_mutex_unlock(&mutex);

/*Do work*/

Theme 3 :

pthread_mutex_lock(&mutex); //mutex lock
while(!condition){
    pthread_cond_wait(&condition, &mutex); //wait for the condition
}
pthread_mutex_unlock(&mutex);

/*Do work*/

I am wondering if such an installation is valid. Assume that threads 2 and 3 relied on some initialization parameters that thread 1 should process.

+5
source share
4 answers

: , #1 #2 #3, pthread_cond_broadcast.

: ( ). #2 #3 , . , , . , #2 #3 ( ).

+6

, , thr # 2 thr # 3 ( "" ) , thr # 1 ( "" ) .

, , , . ( , , .) :

pthread_mutex_t mtx;
pthread_cond_t  cv;
int             initialized = 0;  // our predicate of interest, signaled via cv

...

// boss thread
  initialize_things();
  pthread_mutex_lock(&mtx);
  initialized = 1;
  pthread_cond_broadcast(&cv);
  pthread_mutex_unlock(&mtx);
  ...

// worker threads
  pthread_mutex_lock(&mtx);
  while (! initialized) {
    pthread_cond_wait(&cv, &mtx);
  }
  pthread_mutex_unlock(&mtx);
  do_things();

, mutex/cv/ . ( . Python .) POSIX : , "". pthread_once - , , , .

+4

pthread_cond_signal () , cond. , cond pthread_cond_broadcast.

+1

, , , , .

, thread1 (. thread1 -), , thread2 thread3 . pthread_cond_wait , .

You can use a mutex read-write type pthread_rwlock_t. Essentially, it thread1does write lock, so other threads will be blocked when trying to get a read lock.

The functions for this lock are self-explanatory:

//They return: 0 if OK, error number on failure
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,
    const pthread_rwlockattr_t *restrict attr);
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);

int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);

When thread1completed its initialization, it unlocks. Other threads will perform read locks, and since more read locks can coexist, they can execute simultaneously. Again: this is valid if you are not writing to thread2 & 3 on shared resources.

0
source

Source: https://habr.com/ru/post/1598126/


All Articles