`
东边日出西边雨
  • 浏览: 259035 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

带条件变量的多线程

阅读更多

带条件变量的多线程.

Condition Variables:

A condition variable is a variable of type pthread_cond_t and is used with the appropriate functions for waiting and later, process continuation. The condition variable mechanism allows threads to suspend execution and relinquish the processor until some condition is true. A condition variable must always be associated with a mutex to avoid a race condition created by one thread preparing to wait and another thread which may signal the condition before the first thread actually waits on it resulting in a deadlock. The thread will be perpetually waiting for a signal that is never sent. Any mutex can be used, there is no explicit link between the mutex and the condition variable.

Functions used in conjunction with the condition variable:

 

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

pthread_mutex_t count_mutex     = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t condition_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  condition_cond  = PTHREAD_COND_INITIALIZER;

void *functionCount1();
void *functionCount2();
int  count = 0;
#define COUNT_DONE  10
#define COUNT_HALT1  3
#define COUNT_HALT2  6

main()
{
   pthread_t thread1, thread2;

   pthread_create( &thread1, NULL, &functionCount1, NULL);
   pthread_create( &thread2, NULL, &functionCount2, NULL);
   pthread_join( thread1, NULL);
   pthread_join( thread2, NULL);

   exit(0);
}

void *functionCount1()
{
   for(;;)
   {
      pthread_mutex_lock( &condition_mutex );
      while( count >= COUNT_HALT1 && count <= COUNT_HALT2 )
      {
         pthread_cond_wait( &condition_cond, &condition_mutex );
      }
      pthread_mutex_unlock( &condition_mutex );

      pthread_mutex_lock( &count_mutex );
      count++;
      printf("Counter value functionCount1: %d\n",count);
      pthread_mutex_unlock( &count_mutex );

      if(count >= COUNT_DONE) return(NULL);
    }
}

void *functionCount2()
{
    for(;;)
    {
       pthread_mutex_lock( &condition_mutex );
       if( count < COUNT_HALT1 || count > COUNT_HALT2 )
       {
          pthread_cond_signal( &condition_cond );
       }
       pthread_mutex_unlock( &condition_mutex );

       pthread_mutex_lock( &count_mutex );
       count++;
       printf("Counter value functionCount2: %d\n",count);
       pthread_mutex_unlock( &count_mutex );

       if(count >= COUNT_DONE) return(NULL);
    }

}

 

Results:

Counter value functionCount1: 1
Counter value functionCount1: 2
Counter value functionCount1: 3
Counter value functionCount2: 4
Counter value functionCount2: 5
Counter value functionCount2: 6
Counter value functionCount2: 7
Counter value functionCount1: 8
Counter value functionCount1: 9
Counter value functionCount1: 10
Counter value functionCount2: 11

Note that functionCount1() was halted while count was between the values COUNT_HALT1 and COUNT_HALT2. The only thing that has been ensures is that functionCount2 will increment the count between the values COUNT_HALT1 and COUNT_HALT2. Everything else is random.

The logic conditions (the "if" and "while" statements) must be chosen to insure that the "signal" is executed if the "wait" is ever processed. Poor software logic can also lead to a deadlock condition.

Note: Race conditions abound with this example because count is used as the condition and can't be locked in the while statement without causing deadlock. I'll work on a cleaner example but it is an example of a condition variable.

分享到:
评论

相关推荐

    条件变量交替控制多线程共享资源

    适合linux-c网络编程初学者学习的多线程控制,linux下编译通过,通过互斥锁和条件变量,最终线程的运行结果输出到txt文件中。

    多线程之条件变量

    多线程之条件变量。 http://blog.csdn.net/tornodo

    多线程编程——条件变量使用(Jack_pthread_cond_test.rar)

    多线程编程:条件变量使用。 打包文件包含两个文件:c文件源代码、Makefile文件,运行环境在Ubuntu14.04下,使用自带的gcc编译器,同学们只需将文件夹复制到某一目录下之后在终端执行:1.“make”生成“test”可执行...

    多线程条件变量列子 c++

    多线程条件变量列子 c++

    c语言跨平台条件变量封装

    条件变量所为一种线程安全对象,在多线程开发中,是有一些使用场景的,比如多个线程协作执行任务,或者生产者消费者模式的实现,都可以使用条件变量来进行线程控制。c语言做多线程开发,实现一个跨平台条件变量量...

    多线程互斥锁和条件变量demo

    基于多线程,学习互斥锁和pthread_cond_wait条件变量实现的demo, 初学者学习。

    互斥锁、条件变量、信号量总结

    互斥锁、条件变量、信号量是系统为实现多线程(多进程)访问共享资源或共同协作的同步机制

    Linux C 多线程编程之互斥锁与条件变量实例详解

    在Linux下, 线程的互斥量数据类型是pthread_mutex_t. 在使用前, 要对它进行初始化: 对于静态分配的互斥量, 可以把它设置为PTHREAD_MUTEX_INITIALIZER, 或者调用pthread_mutex_init. 对于动态分配的互斥量, 在申请...

    Java多线程编程总结

    Java 线程系列博文总结word化,编目如下,欢迎互相学习交流: Java线程:概念与原理 Java线程:创建与启动 Java线程:线程栈模型与...Java线程:新特征-条件变量 Java线程:新特征-原子量 Java线程:新特征-障碍器

    java多线程编程总结

    详细的讲述了多线程的各种用法 Java线程:概念与原理 Java线程:创建与启动 Java线程:线程栈模型与线程的变量 Java线程:线程状态的转换 Java线程:线程的同步与锁 Java线程:线程的交互 Java线程:线程的调度-休眠...

    linux多线程编程

    linux多线程编程 声明:本文是网上整理的资料,版权属其作者本人所有。 1 第一章 线程基础知识 2 一.什么是线程 2 二.线程的优点 2 三.线程的缺点 2 四.线程的结构 2 五.线程标识 2 六.线程的创建 3 七..线程...

    Linux线程同步之条件变量

    条件变量给多个线程提供了一个会合的场所。条件本身是由互斥量保护的。线程在改变 条件状态前必须首先锁住互斥量。  条件变量的初始化 pthread_cond_init  去除初始化 pthread_cond_destroy  等待 pthread_...

    C++多线程中的锁和条件变量使用教程

    主要介绍了C++多线程中的锁和条件变量使用,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

    python 多线程的同步机制 以python2例程的方式讲解了python 多线程的同步 常用的方法,主要是锁、条件同步、队列

    python 多线程的同步机制 以python2例程的方式讲解了python 多线程的同步 常用的方法,主要是锁、条件同步、队列 多线程的同步  多线程情况下最常见的问题之一:数据共享;  当多个线程都要去修改某一个共享数据...

    多线程不同步读写共享资源代码

    多线程不同步读写共享资源 文章配套代码 我在很早的时候就听说多线程不同步是可以读写共享资源的。这听起来感觉挺好,因为一旦同步线程,将在同步线程上花去一定的CPU时间片. 这一切都是真的,但是,不同步线程的...

    多线程程序时序分析的隐Markov模型

    用随机变量不确定性刻画不同线程之间时序上的交互关系,分析数据竞争条件下程序不确定结果的概率分布情况;建立多线程程序时序分析的隐Markov模型,使用Baum-Welch和前向算法仿真上下文对程序实际运行状态的影响.实验...

    abb输出,使用多线程配合互斥锁及条件变量来实现,C++源码演示

    abb abb输出10组,使用多线程配合互斥锁及条件变量来实现,C++源码演示

    象棋条件变量-采用python的条件变量

    利用条件变量实现两个线程之间的轮流顺序执行模型。实现多线程之间的互斥访问

    详细描述了Delphi多线程编程,超级简单易懂

    在Delphi中,多线程可以通过多种方式实现,包括使用TThread类、动态线程变量、线程同步机制等。 TThread类是Delphi中进行多线程编程的核心,它封装了创建和控制线程所需的方法和属性。开发者可以通过继承TThread类并...

Global site tag (gtag.js) - Google Analytics