多线程中的条件线程简单示例

1.原理:理解的条件线程最常见的用法就是两个线程同时对一个队列进行处理,比如一个线程负责在队列未满的时候插入item,另外一个线程负责在队列非空时取出item,条件线程涉及4个线程相关函数,pthread_mutex_lock、pthread_mutex_unlock、pthread_cond_wait、pthread_cond_signal,其中pthread_mutex_lock负责对线程加锁,以免其他线程同时对共享资源(比如本例中的int cond_num)进行修改,pthread_mutex_unlock对线程解锁,pthread_cond_wait用于阻塞自己这个线程,pthread_cond_signal用于唤醒其他线程,必须遵守pthread_mutex_lock、pthread_cond_wait、pthread_cond_signal、pthread_mutex_unlock的顺序。


2.代码示例

#define __USE_LARGEFILE64
#define _LARGEFILE64_SOURCE
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>


pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_num_even = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_num_odd = PTHREAD_COND_INITIALIZER;
int cond_num = 1;
void *thread_function_even( )
{
    long unsigned int thread_num = pthread_self();
    int status = pthread_detach( thread_num ); /*detach to be an independent thread*/
    while( 1 )
    {
        printf("enter thread_function_even,cond_num:%d\n",cond_num);
        pthread_mutex_lock( &mutex );
        while( 1 == cond_num % 2 )
            pthread_cond_wait( &cond_num_even, &mutex );
        printf("enter thread_function_even,before deal with cond_num,cond_num:%d\n",cond_num);
        cond_num ++; /*cond_num become even,operate it*/
        pthread_cond_signal( &cond_num_odd );/*wake odd function to see if it become odd*/
        pthread_mutex_unlock( &mutex );
    }
}
void * thread_function_odd( )
{
    long unsigned int thread_num = pthread_self();
    int status = pthread_detach( thread_num ); /*detach to be an independent thread*/
    while( 1 )
    {
        printf("enter thread_function_odd,cond_num:%d\n",cond_num);
        pthread_mutex_lock( &mutex );
        while( 0 == cond_num % 2 )
            pthread_cond_wait( &cond_num_odd, &mutex );
        printf("enter thread_function_odd,before deal with cond_num,cond_num:%d\n",cond_num);
        cond_num ++;/*cond_num become odd,operate it*/
        pthread_cond_signal( &cond_num_even);/*wake even function to see if it become even*/
        pthread_mutex_unlock( &mutex );
    }
}
int main( void )
{
    pthread_t thread;
    pthread_create( &thread, NULL, &thread_function_even, NULL );
    pthread_create( &thread, NULL, &thread_function_odd, NULL );
    while(1); /*avoid thread's quit after main thread quit*/
}


结果:
gcc main.c -lpthread -g
./a.out 后的前几行结果解析
enter thread_function_even,cond_num:1 /*条件数为奇数,直接pthread_cond_wait阻塞跳出该函数直至条件数为偶数为止*/
enter thread_function_odd,cond_num:1 /*条件数符合奇数,跳过阻塞的代码行*/
enter thread_function_odd,before deal with cond_num,cond_num:1 /*处理条件数,条件数变为偶数,将even线程唤醒,因为还未发pthread_cond_wait阻塞自己,继续在odd的while循环中进行*/
enter thread_function_odd,cond_num:2 /*条件数为偶数,直接pthread_cond_wait阻塞跳出该函数直至条件数为奇数为止,因为even已唤醒,跳到even函数处理*/
enter thread_function_even,before deal with cond_num,cond_num:2 /*条件数为偶数,处理条件数*/
...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这本教程花费了我足足两个月的时间写的,每个章节以例子为核心讲解知识点,最大的好处是要用的时候把代码粘贴一下,修改修改就可以上手用了,绝对原创,光是调试这些例子就够我折腾了。知识点覆盖比较全面,要个20分不过分吧,有几本书认真讲过那些常用模块: socket, 数据库操作,xml解析,多线程,最具体的做法多半是一带而过,我可是一个例子一个例子做出来的。 把目录发给大家看看: 2.1 第1课:简单文本输出 5 2.2 第2课:给变量赋值 5 2.3 第3课:命令的赋值与置换一 6 2.4 第4课:命令的赋值与置换二 7 2.5 第5课:命令的赋值与置换三 7 2.6 第6课:算数运算 8 2.7 第7课:文本比较-SWITCH应用 9 2.8 第8课:数值比较-IF应用 10 2.9 第9课:WHILE 循环 11 2.10 第10课:FOR循环和INCR 11 2.11 第11课:过程PROC 12 2.12 第12课:过程PROC的参数定义 13 2.13 第13课:变量的作用域 13 2.14 第14课:LIST结构 14 2.15 第15课:LIST项的增删改 15 2.16 第16课:更多LIST相关 16 2.17 第17课:字符串函数 17 2.18 第18课:更多字符串函数 17 2.19 第19课:修改字符串函数 20 2.20 第20课:正则表达式 21 2.21 第21课:更多正则表达式 22 2.22 第22课:数组 24 2.23 第23课:更多数组相关 25 2.24 第24课:文件存取 28 2.25 第25课:文件信息 30 2.26 第26课:TCL的子进程调用-OPEN & EXEC 33 2.27 第27课:命令或者变量是否存在-INFO 34 2.28 第28课:解释器状态-INFO 35 2.29 第29课:过程信息-INFO 36 2.30 第30课:模块化-SOURCE 37 2.31 第31课:建库-UNKNOWN & INFO LIBRARY 38 2.32 第32课:创建命令-EVAL 40 2.33 第33课:在EVAL应用FORMAT & LIST 40 2.34 第34课:不使用EVAL替换-FORMAT & SUBST 42 2.35 第35课:改变工作目录- CD & PWD 43 2.36 第36课:调试和错误-ERRORINFO & ERRORCODE & CATCH 44 2.37 第37课:调试-TRACE 45 2.38 第38课:命令行参数和环境串 46 2.39 第39课:TIME & UNSET 47 2.40 第40课:SOCKET & FILEEVENT & VWAIT 49 2.41 第41课:日期时间-CLOCK 51 2.42 第42课:I/O通道-FBLOCKED & FCONFIG 53 2.43 第43课:子解释器 56 2.44 第44课:数据库操作 57 2.45 第45课:函数或过程数组的输入和输出方法 59 2.46 第46课:INFO的用法 60 2.47 第47课:多线程 61 2.48 第48课:解析XML 72

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值