linux定时器cond格式,Linux 条件变量 pthread_cond_signal及pthread_cond_wait

#include

#include

#include

#include

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*静态初始化*/

pthread_cond_t  cond = PTHREAD_COND_INITIALIZER;  //init cond

void *thread1(void*);

void *thread2(void*);

int i = 1; //global

int main(int argc,char*  argv[])

{

pthread_t t_a;

pthread_t t_b;//two thread

pthread_create(&t_b,NULL,thread2,(void*)NULL);//Create thread

pthread_create(&t_a,NULL,thread1,(void*)NULL);

pthread_join(t_b,NULL);//wait a_b thread end

pthread_mutex_destroy(&mutex);

pthread_cond_destroy(&cond);

exit(0);

}

//t_a  实现线程t_b打印9之内3的倍数

void *thread1(void *junk){

for(i = 1;i<= 9; i++){

pthread_mutex_lock(&mutex); //互斥锁

printf("call thread1 \n");

if(i%3 == 0)

pthread_cond_signal(&cond); //send sianal to t_b

else

printf("thread1: %d\n",i);

pthread_mutex_unlock(&mutex);

printf("1  [%d]\n",i);

sleep(1);

}

}

//t-b  打印其余的数

void *thread2(void*junk){

while(i < 9)

{

pthread_mutex_lock(&mutex);//开始进入临界区

printf("call thread2 \n");

if(i%3 != 0)//操做有2步,是原子操做。第一解锁,先解除以前的pthread_mutex_lock锁定的mutex;第二 挂起,阻塞并在等待队列里休眠,即所在线程挂起,直到再次被再次唤醒,唤醒的条件是由pthread_cond_signal(&cond);发出的cond信号来唤醒。

pthread_cond_wait(&cond,&mutex); //wait 必须和互斥锁同时用在一个线程里,它同时起到对资源的加锁和解锁

printf("thread2: %d\n",i);

pthread_mutex_unlock(&mutex);//离开临界区

printf("2 ....\n"  );

sleep(1);

}

ide

}this

*********************************************************************spa

int __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)操作系统

pthread_cond_wait 源码线程

{

volatile pthread_descr self = thread_self();

pthread_extricate_if extr;

int already_canceled = 0;

int spurious_wakeup_count;

/* Check whether the mutex is locked and owned by this thread.  */

if (mutex->__m_kind != PTHREAD_MUTEX_TIMED_NP

&& mutex->__m_kind != PTHREAD_MUTEX_ADAPTIVE_NP

&& mutex->__m_owner != self)

return EINVAL;

/* Set up extrication interface */

extr.pu_object = cond;

extr.pu_extricate_func = cond_extricate_func;

/* Register extrication interface */

THREAD_SETMEM(self, p_condvar_avail, 0);

__pthread_set_own_extricate_if(self, &extr);

/* Atomically enqueue thread for waiting, but only if it is not

canceled. If the thread is canceled, then it will fall through the

suspend call below, and then call pthread_exit without

having to worry about whether it is still on the condition variable queue.

This depends on pthread_cancel setting p_canceled before calling the

extricate function. */

__pthread_lock(&cond->__c_lock, self);

if (!(THREAD_GETMEM(self, p_canceled)

&& THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))

enqueue(&cond->__c_waiting, self);

else

already_canceled = 1;

__pthread_unlock(&cond->__c_lock);

if (already_canceled) {

__pthread_set_own_extricate_if(self, 0);

__pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);

}

pthread_mutex_unlock(mutex);

spurious_wakeup_count = 0;

while (1)

{

suspend(self);

if (THREAD_GETMEM(self, p_condvar_avail) == 0

&& (THREAD_GETMEM(self, p_woken_by_cancel) == 0

|| THREAD_GETMEM(self, p_cancelstate) != PTHREAD_CANCEL_ENABLE))

{

/* Count resumes that don't belong to us. */

spurious_wakeup_count++;

continue;

}

break;

}

__pthread_set_own_extricate_if(self, 0);

/* Check for cancellation again, to provide correct cancellation

point behavior */

if (THREAD_GETMEM(self, p_woken_by_cancel)

&& THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {

THREAD_SETMEM(self, p_woken_by_cancel, 0);

pthread_mutex_lock(mutex);

__pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);

}

/* Put back any resumes we caught that don't belong to us. */

while (spurious_wakeup_count--)

restart(self);

pthread_mutex_lock(mutex);

return 0;

}rest

示例的解释:

call thread2:是线程2即t_b首先上锁,即 pthread_mutex_lock(&mutex);锁住了mutex使得此进程执行线程2中的临界区的代码,当执行到45行:if(i%3 != 0),此时i=1,知足此条件,则执行46行: pthread_cond_wait(&cond,&mutex); 这句是关键,pthread_cond_wait(&cond,&mutex)操做有两步,是原子操做:第一 解锁,先解除以前的pthread_mutex_lock锁定的mutex;第二 挂起,阻塞并在等待对列里休眠,即线程2挂起,直到再次被唤醒,唤醒的条件是由pthread_cond_signal(&cond);发出的cond信号来唤醒。

call thread1:因为pthread_cond_wait已经对线程2解锁,此时另外的线程只有线程1,那么线程1对mutex上锁,若这时有多个线程,那么线程间上锁的顺序和操做系统有关。

thread1: 1:线程1上锁后执行临界区的代码,当执行到if(i%3 == 0)此时i=1,不知足条件,则pthread_cond_signal(&cond);不被执行,那么线程2仍处于挂起状态,输出thread1: 1后线程1由pthread_mutex_unlock(&mutex);解锁。

thread1: 2:这时此进程中只有2个线程,线程2处于挂起状态,那么只有线程1,则线程1又对mutex上锁,此时一样执行临界区的代码,并且i=2,不知足条件,pthread_cond_signal(&cond);不被执行,那么线程2仍处于挂起状态,输出thread1: 1后线程1由pthread_mutex_unlock(&mutex);解锁。

call thread1:一样由线程1上锁,但此时i=3,知足条件pthread_cond_signal(&cond)被执行,那么pthread_cond_signal(&cond)会发出信号,来唤醒处于挂起的线程2。

thread2: 3:因为pthread_cond_signal唤醒了线程2,即i=3知足条件,pthread_cond_wait(&cond,&mutex);被执行,那么pthread_cond_wait(&cond,&mutex)此时也有一步操做:上锁;即对线程2上锁,此时的pthread_cond_wait(&cond,&mutex)的操做至关与pthread_mutex_lock(&mutex);那么线程2继续执行上锁后的临界区的代码,并由pthread_mutex_unlock(&mutex);对线程2进行解锁。

队列

.......进程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值