信号丢失问题

当标志没有被设置的时候,线程会不断循环检测这个标志同时会不断锁定、解锁互斥体,浪费 CPU  时间你真正需要的是这样一种方法:当标志没有设置的时候让线程进入休眠状态;而当某种特定条件出现时,标志位被设置,线程被唤醒。

    如同信号量,线程可以对一个条件变量执行等待操作。如果如果线程 A 正在等待一个条件变量,它会被阻塞直到另外一个线程B,向同一个条件变量发送信号以改变其状态。同于信号量,条件变量没有计数值也不占据内存空间,线程 A 必须在 B 发送信号之前开始等待。如果 B 在 A 执行等待操作之前发送了信号,这个信号就丢失了,同时 A会一直阻塞直到其它线程再次发送信号到这个条件变量。

    条件变量将允许你实现这样的目的:在一种情况下令线程继续运行,而相反情况下令线程阻塞。只要每个可能涉及到改变状态的线程正确使用条件变量,Linux 将保证当条件改变的时候由于一个条件变量的状态被阻塞的线程均能够被激活。

     GNU/Linux 刚好提供了这个机制,每个条件变量都必须与一个互斥体共同使用,以防止这种竞争状态的发生。这种设计下,线程函数应遵循以下步骤: 

  1.  thread_function中的循环首先锁定互斥体并且读取标志变量的值。 
  2.   如果标志变量已经被设定,该线程将互斥体解锁然后执行工作函数
  3.   如果标志没有被设置,该线程自动锁定互斥体并开始等待条件变量的信号

     这里最关键的特点就在第三条。这里,GNU/Linux系统允许你用一个原子操作完成解除互斥体锁定和等待条件变量信号的过程而不会被其它线程在中途插入执行。这就避免了在thread_function中检测标志和等待条件变量的过程中其它线程修改标志变量并对条件变量发送信号的可能性。

   pthread_cond_t  pCond;

   pthread_cond_init(&pCond,NULL); 第一个参数是一个指向pthread_cond_t变量的指针。第二个参数是一个指向条件变量属性对象的指针;这个参数在 GNU/Linux 系统中是被忽略的。

    pthread_cond_signal(&pCond)如果没有线程正在等待这个信号,则这个信号会被忽略。该函数的
参数是一个指向 pthread_cond_t 类型变量的指针。

    pthread_cond_broadcast()函数会将所有等待该条件变量的线程解锁而不是仅仅解锁一个线程         

    pthread_cond_wait(&pCond,&mutex)会让调用线程阻塞直到条件变量收到信号。第一个参数是指向一个 pthread_cond_t 类型变量的指针,第二个参数是指向一个pthread_mutex_t类型变量的指针。当调用 pthread_cond_wait 的时候,互斥体对象必须已经被调用线程锁定。这个函数以一个原子操作解锁互斥体并锁定条件变量等待信号。当信号到达且调用线程被解锁之后,pthread_cond_wait自动申请锁定互斥体对象。

 

 
    pthread_mutex_lock(&qlock);  
    pthread_cond_wait(&qready, &qlock);  //其它线程pthread_cond_sigal发出信号才会对出阻塞
    pthread_mutex_unlock(&qlock);

The mutex passed to pthread_cond_wait protects the condition.The caller  passes it locked to the function, which then atomically places the calling thread on the list of threads waiting for thecondition and unlocks the mutex. This closes the window between the time that the  condition is checked and the time that the thread goes to sleep waiting for the condition to change, so that the thread doesn't miss a change in the condition. When pthread_cond_wait returns, the mutex is again locked.
上面是APUE的原话,就是说pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t*mutex)

数传入的参数mutex用于保护条件,因为我们在调用pthread_cond_wait时,如果条件不成立我们就进入阻塞,但是进入阻塞这个期间,如果条件变量改变了的话,那我们就漏掉了这个条件。因为这个线程还没有放到等待队列上,所以调用pthread_cond_wait前要先锁互斥量,即调用pthread_mutex_lock(),pthread_cond_wait在把线程放进阻塞队列后,自动对mutex进行解锁,使得其它线程可以获得加锁的权利。这样其它线程才能对临界资源进行访问并在适当的时候唤醒这个阻塞的进程。当pthread_cond_wait返回的时候又自动给mutex加
实际上边代码的加解锁过程如下:

pthread_mutex_lock(&qlock);   
pthread_cond_wait(&qready, &qlock);
pthread_mutex_unlock(&qlock);

 

 

一般修改标志变量,经过以下步骤:

1.  锁定与条件变量伴生的互斥体。
2.  执行可能改变程序状态的指令(在我们的例子中,修改标志)。
3.  向条件变量投递或广播信号。这取决于我们希望的行为。
4.  将与条件变量伴生的互斥体解锁。

pCond.c

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
int  flag;
pthread_cond_t pCond;
pthread_mutex_t mutex;
 

void initialize_flag()
{
 
 pthread_mutex_init (&mutex, NULL);
 
 pthread_cond_init (&pCond, NULL);
 

  flag = 0;
}

void set_thread_flag (int flag_value)

  flag = flag_value;
  pthread_cond_signal (&pCond);
  pthread_mutex_unlock (&mutex);
}
 

void* thread_fun1()
{
  while (1)
  {
     pthread_mutex_lock (&mutex);
     if (!flag)
     pthread_cond_wait (&pCond, &mutex);
     printf("thread 1 is running\n");
    pthread_mutex_unlock (&mutex);
  }
  return NULL;
}
void *thread_fun2()
{
  sleep(1);
  printf("thread 2 is running\n");
  set_thread_flag(1);

}
 

int main()
{
pthread_t id1,id2;
pthread_create(&id1,NULL,thread_fun1,NULL);
pthread_create(&id2,NULL,thread_fun2,NULL);

initialize_flag();
printf("main thread is running\n");
sleep(20);
return 0;
}

 

 运行结果应为:

main thread  is running
thread 2 is running
thread 1 is running
thread 1 is running
thread 1 is running
.......

因为线程1阻塞,至到线程2运行,给于信号后,线程1才开始运行

 

件变量也可以用于不涉及程序状态的情况,而仅用作一种让一个线程阻塞等待其它线程唤醒的机制。信号量也可用于这个目的。两者之前的主要区别是,当没有线程处于阻塞状态的时候信号量会“记住”唤醒下一个被阻塞的线程,而条件变量只是简单地丢弃这个信号。另外,信号量只能发送一个唤醒信息给一个线程,而 pthread_cond_broadcast 可以同时唤醒不限数量的可以被唤醒的线程。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值