编写一个程序,开启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. winform对话框控件

    (1)ColorDialog     用户自定义颜色控件 点击颜色按键,改变richTextBox1中字体的颜色 private void button1_Click(object sender, E ...

  2. 把应用程序exe 注册成为windows 服务的方法

    由于在Windows 服务器上必须要启动一个软件,提供外网访问内网的客户端软件,但是由于每次远程服务器之后会注销当前用户,所以客户端软件就会自动退出,那么我在外网的系统就不能支持访问了. 解决方案:将 ...

  3. PHP脱mysql脚本

    <?php $SQL_Server="xxxxxx:3306"; $SQL_User="xxxx"; $SQL_Name="xxxx" ...

  4. Aspose Cells 添加数据验证(动态下拉列表验证)

    参考 :http://www.componentcn.com/kongjianjishu/kongjianjishu/2015-06-04/2781.html Aspose Cells是一款操作和处理 ...

  5. JPA入门例子(采用JPA的hibernate实现版本)

    (1).JPA介绍: JPA全称为Java Persistence API ,Java持久化API是Sun公司在Java EE 5规范中提出的Java持久化接口.JPA吸取了目前Java持久化技术的优 ...

  6. repeater单双行颜色不同,gridview repeater DataList 鼠标经过改变背景颜色

    1.gridview 双击GridView的OnRowDataBound事件: 在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示: protected void  ...

  7. VS 调试

    Vs 单步调试 在vs中的单步调试: 调试重要的几个键: F9在当前光标所在的行下断点,如果当前行已经有断点,则取消断点. F5调试状态运行程序,程序执行到有断点的地方会停下来. F10单步执行程序. ...

  8. IOS第二天多线程-03对列组合并图片

    ********* // 2D绘图 Quartz2D // 合并图片 -- 水印 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) ...

  9. Cannot create a server using the selected type.

    1.退出 eclipse 2.到[工程目录下]/.metadata/.plugins/org.eclipse.core.runtime 3.把org.eclipse.wst.server.core.p ...

  10. 【转】arcgis server site 快速恢复与重建

    作者:suwenjiang 出处:http://www.cnblogs.com/myyouthlife/ 具体链接:http://www.cnblogs.com/myyouthlife/p/48985 ...