linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > article > unix > unix提高 >
栏目导栏
资料搜索
热门文章
·20%的SOLARIS知识解决80%的问题
·Solaris启动过程详解 zt
·查看Solaris系统硬件配置的命令
·STRUTS 源码学习笔记
·Unix系列shell程序编写(中)
·SOLARIS技巧篇
·snort源码分析
·Unix系列shell程序编写(下)
·在solaris 10/x86下安装oracle
·Apache源代码分析——关于模块
·solaris 10 硬盘安装
·Solaris安全配置手册
·HP-UX基本指令快速参考
· Tomcat Server源码启动分析
·Solaris8 双网卡配置(路由器用
最新文章
·solaris系统安全配置
·RHEL5.0操作系统下NFS服务的配
·Solaris Linux 9下Vsftpd的配置
·在HP-UX下建立只归属于某个目录
·SolarWinds2002使用说明
·从外部存储划盘并加入vg,为fs扩
·因带库问题导致系统光纤卡报错
·Unix下多线程中条件变量的使用
·UNIX和Linux中信号的个数
·不同的类UNIX操作系统密码破解
·AIX系统下Domino邮件服务器安装
·限制ROOT用户远程登陆UNIX系统
·Unix操作系统入侵追踪反击战
·AIX CDE不能启动的故障一般性解
·CentOS4.4用VSFTPD架设FTP服务
Google
 
Unix下多线程中条件变量的使用
[ 作者:  加入时间:2007-11-18 17:34:13  来自:Linux联盟收集整理 ]
条件变量是线程编程中的一种同步机制,它可以和互斥量一起使用,达到无竞争的使用临界资源SYzLinux联盟
下面是一个用条件变量实现的多线程consumer-procuder例子。SYzLinux联盟

#include <iostream>SYzLinux联盟
#include <pthread.h>SYzLinux联盟
#include <sys/time.h>SYzLinux联盟
using namespace std;SYzLinux联盟
SYzLinux联盟
int shareVal = 0;SYzLinux联盟
SYzLinux联盟
pthread_cond_t qready = PTHREAD_COND_INITIALIZER;SYzLinux联盟
pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;SYzLinux联盟
SYzLinux联盟
void *SYzLinux联盟
consumer(void* arg)SYzLinux联盟
{SYzLinux联盟
    for (;;) {SYzLinux联盟
        pthread_mutex_lock(&qlock);SYzLinux联盟
SYzLinux联盟
        while(0 == shareVal) { /* No resource available, then wait */SYzLinux联盟
            pthread_cond_wait(&qready, &qlock);SYzLinux联盟
        }SYzLinux联盟
SYzLinux联盟
        cout << "current shareVal = " << shareVal << endl;SYzLinux联盟
        shareVal--;SYzLinux联盟
SYzLinux联盟
        pthread_mutex_unlock(&qlock);SYzLinux联盟
    }SYzLinux联盟
    return ((void *)1);SYzLinux联盟
}SYzLinux联盟
SYzLinux联盟
void SYzLinux联盟
producer(void)SYzLinux联盟
{SYzLinux联盟
    pthread_mutex_lock(&qlock);SYzLinux联盟
    shareVal++;SYzLinux联盟
    pthread_mutex_unlock(&qlock);SYzLinux联盟
    pthread_cond_signal(&qready); /* resource is available, awake the consumer thread */SYzLinux联盟
}SYzLinux联盟
SYzLinux联盟
int main()SYzLinux联盟
{SYzLinux联盟
    int err;SYzLinux联盟
    pthread_t tid1;SYzLinux联盟
    void *tret;SYzLinux联盟
SYzLinux联盟
    err = pthread_create(&tid1, NULL, consumer, NULL);SYzLinux联盟
    if (err != 0) {SYzLinux联盟
        cout << "cant create thread" << endl;SYzLinux联盟
        exit(1);SYzLinux联盟
    }SYzLinux联盟
SYzLinux联盟
    
/*SYzLinux联盟
    err = pthread_join(tid1, &tret);SYzLinux联盟
    if (err != 0) {SYzLinux联盟
        cout << "can't join" << endl;SYzLinux联盟
        exit(1);SYzLinux联盟
    }SYzLinux联盟
    */
SYzLinux联盟
SYzLinux联盟
    for (int i = 0; i < 5; i++) {SYzLinux联盟
        cout << "i = " << i << endl;SYzLinux联盟
        producer();SYzLinux联盟
        sleep(1);SYzLinux联盟
    }SYzLinux联盟
SYzLinux联盟
    pthread_mutex_destroy(&qlock);SYzLinux联盟
    return 0;SYzLinux联盟
}
SYzLinux联盟

Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·Unix、Linux下常用监控和管理命令工具  (2007-11-14 17:22:09)
 ·UNIX和Linux系统下SUID、SGID的解析  (2007-10-31 18:07:41)
 ·UNIX和Linux中信号的个数  (2007-10-30 13:47:09)
 ·Linux/Unix操作系统处于内网的桌面控制  (2007-10-30 12:28:59)
 ·如何使用Java编写多线程程序  (2007-10-25 11:26:17)
 ·不同的类UNIX操作系统密码破解方法介绍  (2007-10-25 10:22:32)
 ·用Visual Basic创建多线程应用程序  (2007-10-19 13:13:59)
 ·限制ROOT用户远程登陆UNIX系统  (2007-10-19 11:12:19)
 ·类unix系统下常用工具及命令  (2007-10-16 11:51:09)
 ·基本UNIX命令集介绍  (2007-10-16 11:50:05)