标签:
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); }
pthread_cond_wait和pthread_cond_signal分析
标签:
原文地址:http://www.cnblogs.com/aHuner/p/4229362.html