pthread_cond_wait和pthread_cond_signal分析

1.pthread线程控制

int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*stat_rtn)(void*), void *restrict arg);
int pthread_exit(void *status);
int pthread_cancel(pthread_t thread);
int pthread_join(pthread_t tid, void **tret);
int pthread_cleanup_push(void (*rtn)(void*), void *arg);
int pthread_clean_pop(int execute);
int pthread_equal(pthread_t tid1, pthread_t tid2);
pthread_t pthread_self(void);

2.Pthread提供多种同步机制:

1) Mutex(互斥量):

int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);

2) Condition Variable(条件变量):

int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t *cond);
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
// wait:必须与pthread_mutex_lock和pthread_mutex_unlock配套使用,一进入wait状态就会自动release mutex
int pthread_cond_timedwait(pthread_cond_t *restict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime);
int pthread_cond_signal(pthread_cond_t *restrict cond);  // signal:只对正处于wait状态下的其他线程具备唤醒作用
int pthread_cond_broadcast(pthread_cond_t *restrict cond);

3) Read/Write lock(读写锁):

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_tryrdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

4) Spin lock(自旋锁):

int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
int pthread_spin_destroy(pthread_spinlock_t *lock);
int pthread_spin_lock(pthread_spinlock_t *lock);
int pthread_spin_trylock(pthread_spinlock_t *lock);

3.代码分析

线程A:

pthread_mutex_lock(&mtx); +1

pthread_cond_wait(&cond, &mtx); +2 // 内部有解锁和加锁步骤

pthread_mutex_unlock(&mtx); +3

线程B:

pthread_mutex_lock(&mtx); +4

pthread_cond_signal(&cond); +5 // 只对正处于wait状态下的其他线程具备唤醒作用

pthread_mutex_unlock(&mtx); +6

如果线程A先被调用, pthread_cond_wait内部先解锁,再进入wait状态,此时线程A处于等待状态;
线程B先加锁,pthread_cond_signal调用时,唤醒了线程A,但pthread_cond_wait内被唤醒但又得加锁才能返回,所以线程A再次进入等待锁状态,当线程B解锁后pthread_cond_wait才能加锁返回成功,然后线程A外部解锁。

pthread的源码在glibc/nptl下

int
__pthread_cond_wait (cond, mutex)
     pthread_cond_t *cond;
     pthread_mutex_t *mutex;
{
  struct _pthread_cleanup_buffer buffer;
  struct _condvar_cleanup_buffer cbuffer;
  int err;
  int pshared = (cond->__data.__mutex == (void *) ~0l)
        ? LLL_SHARED : LLL_PRIVATE;

  LIBC_PROBE (cond_wait, 2, cond, mutex);

  /* Make sure we are alone.  */
  lll_lock (cond->__data.__lock, pshared);

/* Now we can release the mutex. */

  err 

= __pthread_mutex_unlock_usercnt (mutex, 0

);
  if (__builtin_expect (err, 0))
    {
      lll_unlock (cond->__data.__lock, pshared);
      return err;
    }

  /* We have one new user of the condvar.  */
  ++cond->__data.__total_seq;
  ++cond->__data.__futex;
  cond->__data.__nwaiters += 1 << COND_NWAITERS_SHIFT;

  /* Remember the mutex we are using here.  If there is already a
     different address store this is a bad user bug.  Do not store
     anything for pshared condvars.  */
  if (cond->__data.__mutex != (void *) ~0l)
    cond->__data.__mutex = mutex;

  /* Prepare structure passed to cancellation handler.  */
  cbuffer.cond = cond;
  cbuffer.mutex = mutex;

  /* Before we block we enable cancellation.  Therefore we have to
     install a cancellation handler.  */
  __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);

  /* The current values of the wakeup counter.  The "woken" counter
     must exceed this value.  */
  unsigned long long int val;
  unsigned long long int seq;
  val = seq = cond->__data.__wakeup_seq;
  /* Remember the broadcast counter.  */
  cbuffer.bc_seq = cond->__data.__broadcast_seq;

  do
    {
      unsigned int futex_val = cond->__data.__futex;

      /* Prepare to wait.  Release the condvar futex.  */
      lll_unlock (cond->__data.__lock, pshared);

      /* Enable asynchronous cancellation.  Required by the standard.  */
      cbuffer.oldtype = __pthread_enable_asynccancel ();

      /* Wait until woken by signal or broadcast.  */
      lll_futex_wait (

&cond->

__data.__futex, futex_val, pshared);

      /* Disable asynchronous cancellation.  */
      __pthread_disable_asynccancel (cbuffer.oldtype);

      /* We are going to look at shared data again, so get the lock.  */
      lll_lock (cond->__data.__lock, pshared);

      /* If a broadcast happened, we are done.  */
      if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
    goto bc_out;

      /* Check whether we are eligible for wakeup.  */
      val = cond->__data.__wakeup_seq;
    }
  while (val == seq || cond->__data.__woken_seq == val);

  /* Another thread woken up.  */
  ++cond->__data.__woken_seq;

 bc_out:

  cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;

  /* If pthread_cond_destroy was called on this varaible already,
     notify the pthread_cond_destroy caller all waiters have left
     and it can be successfully destroyed.  */
  if (cond->__data.__total_seq == -1ULL
      && cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
    lll_futex_wake (&cond->__data.__nwaiters, 1, pshared);

  /* We are done with the condvar.  */
  lll_unlock (cond->__data.__lock, pshared);

  /* The cancellation handling is back to normal, remove the handler.  */
  __pthread_cleanup_pop (&buffer, 0);

  /* Get the mutex before returning.  */

return

 __pthread_mutex_cond_lock (mutex);
}

int
__pthread_cond_signal (cond)
     pthread_cond_t *cond;
{
  int pshared = (cond->__data.__mutex == (void *) ~0l)
        ? LLL_SHARED : LLL_PRIVATE;

  LIBC_PROBE (cond_signal, 1, cond);

  /* Make sure we are alone.  */
  lll_lock (cond->__data.__lock, pshared);

  /* Are there any waiters to be woken?  */
  if (cond->__data.__total_seq > cond->__data.__wakeup_seq)
    {
      /* Yes.  Mark one of them as woken.  */
      ++cond->__data.__wakeup_seq;
      ++cond->__data.__futex;

      /* Wake one.  */

if (! __builtin_expect (lll_futex_wake_unlock (&cond->__data.__futex, 1, 1, &cond->

__data.__lock,
                             pshared), 0))
    return 0;

      lll_futex_wake (&cond->__data.__futex, 1, pshared);
    }

  /* We are done.  */
  lll_unlock (cond->__data.__lock, pshared);

  return 0;
}

4.使用方案

4.1.如果只有线程A和线程B时,可以这样:

线程A:

pthread_mutex_lock(&mtx);           +1

pthread_cond_wait(&cond, &mtx);  +2 // 内部有解锁和加锁步骤

pthread_mutex_unlock(&mtx);        +3

线程B:

pthread_cond_signal(&cond);         +1

4.2.如果有多个线程,则代码如下:

int init_count;

static void register_thread_initialized(void) {
    pthread_mutex_lock(&init_lock);                   // 对init_count的加锁
    init_count++;
    pthread_cond_signal(&init_cond);
    pthread_mutex_unlock(&init_lock);
}

static void wait_for_thread_registration(int nthreads) {
    pthread_mutex_lock(&init_lock);
    init_count = 0;
    while (init_count < nthreads) {
        pthread_cond_wait(&init_cond, &init_lock);
    }
    pthread_mutex_unlock(&init_lock);
}
时间: 2024-10-08 15:09:59

pthread_cond_wait和pthread_cond_signal分析的相关文章

深入理解pthread_cond_wait、pthread_cond_signal

===============================man pthread_cond_wait的解释========================== LINUX环境下多线程编程肯定会遇到需要条件变量的情况,此时必然要使用pthread_cond_wait()函数.但这个函数的执行过程比较难于理解. pthread_cond_wait()的工作流程如下(以MAN中的EXAMPLE为例): Consider two shared variables x and y, protected

pthread_cond_wait()与pthread_cond_signal()

pthread_cond_wait在把线程放进阻塞队列后,自动对mutex进行解锁(它内部有自己维护一个队列),使得其他线程可以获得加锁的权利.这样其它线程才能对临界资源进行访问并在适当的时候唤醒这个阻塞的进程.当pthread_cond_wait返回的时候又会自动给mutex加锁,所以最后需要一个pthread_mutex_unlock()函数来解锁. 假设另一个线程(线程2)锁定了mutex并对已链接列表添加了一项.在对互斥对象解锁后,线程2会立即调用函数pthread_cond_broad

关于 pthread_cond_wait 和 pthread_cond_signal , signal 无效的问题

关于一个消费者模式,,,引起的问题.. 我在io线程里不断的把一个函数调用放到队列里 然后ruby线程就不断的从这个队列里取出函数之争并执行. 典型的 消费者模式. 我以前以为是这样... 这是work线程 pthread_mutex_lock(&mutex2) while(( invoke = get_invoke() ) != NULL){ do_invoke(invoke); } pthread_mutex_unlock(&mutex2) pthread_cond_wait(&

pthread_cond_wait和pthread_cond_signal以及互斥变量的使用情况

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define BUFFER_SIZE 21 //初始化存储空间的大小 #define END_FLAG (-1)//用于退出循环 struct Products { int buffer[BUFFER_SIZE];//存储空间 pthread_mutex_t locker; //声明互斥变量 pth

pthread_cond_wait、pthread_cond_signal 不深入的思考

编程中,当涉及到多个线程需要访问同一个全局对象时,往往需要进行线程同步,而线程同步,往往是一个约定俗成的东西.比如说: 1 //*.cpp 2 int g_var ; pthread_mutex_t g_mtx ; 3 static void * threadFunc(void * Par) { 4 pthread_mutex_lock(&g_mtx) ; 5 6 g_var = //some value ; 7 8 return NULL ; 9 } 为了保持g_var的一致性,理应在所有线程

pthread_cond_signal与pthread_cond_wait详解

转:http://blog.chinaunix.net/uid-11572501-id-3456343.html //pthread_cond_signal 只发信号,内部不会解锁,在Linux 线程中,有两个队列,分别是cond_wait队列和mutex_lock队列, cond_signal只是让线程从cond_wait队列移到mutex_lock队列,而不用返回到用户空间,不会有性能的损耗.(pthread_cond_signal unlock后pthread_cond_wait才能上锁)

深入理解 pthread_cond_wait()

在多线程同步互斥的应用场景下,通常会用到pthread_cond_wait()和pthread_cond_signal()函数.那么这两个函数到底是如何保证互斥同步的呢? 为了对上面的问题有个直观的了解,可以从下面的问题着手.下面两段这些程序有什么bug么? a. 等待条件的线程执行的代码: Thread 1: { --------.. pthread_cond_wait(cv, mutex); do_worker_task(); --------.. } b.唤醒等待线程的代码: { pthr

条件变量的陷阱与思考

一.前言 在多线程编程中,互斥锁与条件变量恐怕是最常用也是最实用的线程同步原语. 关于条件变量一共也就pthread_cond_init.pthread_cond_destroy.pthread_cond_wait.pthread_cond_timedwait.pthread_cond_signal.pthread_cond_broadcast这么几个函数,但是在实际使用中却是很容易用错,后文将来分析几种常见使用情况的正确性. 二.分析 下面是一个辅助基类.便于减少篇幅(由于简单起见,后文中的所

linux c++ 多线程 【三】

今天得空继续扫了一下(https://computing.llnl.gov/tutorials/pthreads/,这次没有用c++,直接参考的tutorial中的c语言实现)pthread中提供的另一种线程同步的方法:condition variables 既然已经有了mutex,为什么还要有condition variables这样的技术手段呢? 原文的阐述是:“While mutexes implement synchronization by controlling thread acc