编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。

使用条件变量来实现:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
static pthread_mutex_t mtx=PTHREAD_MUTEX_INITIALIZER;

static pthread_cond_t condA ;
static pthread_cond_t condB ;
static pthread_cond_t condC ;

void* threadA(void *arg)
{
int a =10;
while(a--)
{
//sleep(2);
//printf("A begin.\n");
pthread_mutex_lock(&mtx);
//printf("A wait.\n");
pthread_cond_wait(&condC,&mtx);
printf("A.\n");
pthread_mutex_unlock(&mtx);
pthread_cond_signal(&condA);
}
}

void* threadB(void *arg)
{
int b=10;
while(b--)
{
//sleep(2);
//printf("B begin.\n");
pthread_mutex_lock(&mtx);
//printf("B wait.\n");
pthread_cond_wait(&condA,&mtx);
printf("B.\n");
pthread_mutex_unlock(&mtx);
pthread_cond_signal(&condB);
}
}

void* threadC(void *arg)

{
int c=10;
while(c--)
{
//sleep(2);
//printf("C begin.\n");
pthread_mutex_lock(&mtx);
//printf("C wait.\n");
pthread_cond_wait(&condB,&mtx);
printf("C.\n");
pthread_mutex_unlock(&mtx);
pthread_cond_signal(&condC);
}
}
int main (void *arg)
{
pthread_t tidA;
pthread_t tidB;
pthread_t tidC;
pthread_cond_init(&condA,NULL);
pthread_cond_init(&condB,NULL);
pthread_cond_init(&condC,NULL);
pthread_create(&tidA,NULL,&threadA,NULL);
pthread_create(&tidB,NULL,&threadB,NULL);
pthread_create(&tidC,NULL,&threadC,NULL);

printf("main begin..\n");
sleep(4);
pthread_cond_signal(&condC);

pthread_join(tidA,NULL);
pthread_join(tidB,NULL);
pthread_join(tidC,NULL);

return 0;
}

Linux 多线程编程 实例 2的更多相关文章

  1. Linux多线程编程实例解析

    Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...

  2. Linux 多线程编程实例

    一.多线程 VS 多进程 和进程相比,线程有很多优势.在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维护代码段和数据.而运行于一个进程中的多个线程,他们之间使用相同 ...

  3. Linux 多线程编程 实例 1

    子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码. #include <pthread.h> ...

  4. linux下C语言多线程编程实例

    用一个实例.来学习linux下C语言多线程编程实例. 代码目的:通过创建两个线程来实现对一个数的递加.代码: //包含的头文件 #include <pthread.h> #include ...

  5. Linux C语言多线程编程实例解析

    Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...

  6. Linux多线程编程初探

    Linux线程介绍 进程与线程 典型的UNIX/Linux进程可以看成只有一个控制线程:一个进程在同一时刻只做一件事情.有了多个控制线程后,在程序设计时可以把进程设计成在同一时刻做不止一件事,每个线程 ...

  7. 【操作系统作业-lab4】 linux 多线程编程和调度器

    linux多线程编程 参考:https://blog.csdn.net/weibo1230123/article/details/81410241 https://blog.csdn.net/skyr ...

  8. Linux多线程编程之详细分析

    线程?为什么有了进程还需要线程呢,他们有什么区别?使用线程有什么优势呢?还有多线程编程的一些细节问题,如线程之间怎样同步.互斥,这些东西将在本文中介绍.我见到这样一道面试题: 是否熟悉POSIX多线程 ...

  9. Linux多线程编程阅读链接

    1. 进程与线程的一个简单解释(阮一峰) 2. linux 多线程编程 3. Linux 的多线程编程的高效开发经验 (IBM)

随机推荐

  1. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  2. 普通工程转为mvn工程

    不同类型的工程可以转为mvn工程, 只需要一个插件 You may need to install m2e-eclipse plugin in order to have this simple ut ...

  3. Linux_权限

    一.查看文件或文件夹权限 [root@hadoop09-linux etc]# ll -h /etc #ll 是ls -l 的缩写方式 -h文件大小单位k 截取其中三行说明 drwxr-xr-x. 2 ...

  4. hao123列表的实现

    <!DOCTYPE html><html><head>        <meta http-equiv="Content-Type" co ...

  5. Daily Scrum 11.4

    昨天会议结束后熬夜较晚,没有及时添加会议记录到博客上,今天补上. Member Today’task Tomorrow’task 张恿 挖掘程序潜力 根据今天的总结对程序进行改进 吴文会 明日返校.. ...

  6. chrome 优秀的插件推荐

    就本人使用过的chrome插件推荐下: 1:Adblock Plus 免费的广告拦截器,可阻止所有烦人的广告及恶意软件和跟踪. 2:ChaZD 英文翻译,妈妈再也不用担心我英文看不懂了,ChaZD 查 ...

  7. 【HDU 4747 Mex】线段数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意:有一组序列a[i](1<=i<=N), 让你求所有的mex(l,r), mex ...

  8. zju(5)LED控制实验

    1.实验目的 1.学习和掌握如何将一个驱动程序添加到Kconfig,编译到内核. 二.实验内容 1.编写EduKit-IV试验箱Linux操作系统下LED灯的驱动: 2.编写EduKit-IV试验箱L ...

  9. //sql过滤关键字

    //sql过滤关键字 public static bool CheckKeyWord(string sWord) { //过滤关键字 string StrKeyWord = @"select ...

  10. Objective-C 中类属性(修饰)

    Objective-C 中类属性(修饰) (2013-07-13 14:38:35) 转载▼ 标签: it 分类: IOS笔记 nonatomic: 非原子性访问,对属性赋值的时候不加锁,多线程并发访 ...