Linux 多线程编程 实例 1
子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码。
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
static pthread_mutex_t mtx=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static void *thread_func(void *arg)
{
//sleep(10);
int j=3;
while(j--)
{
pthread_mutex_lock(&mtx);
pthread_cond_wait(&cond,&mtx);
//pthread
printf("10 times complete.\n");
//pthread
pthread_mutex_unlock(&mtx);
}
}
int main(void *arg)
{
pthread_t tid;
pthread_create(&tid,NULL,thread_func,NULL);
//sleep(5);
int i=50;
while(i--)
{
sleep(2);
pthread_mutex_lock(&mtx);
printf("100 times complete!.\n");
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx);
}
//pthread_join(tid,NULL);
//pthread_cancel(tid);
pthread_join(tid,NULL);
}
最新文章
- NTP校时设置
- ORACLE迁移votedisk,spfile以及OCRfile的方法
- linux分享六:字符串处理
- Github 安全类Repo收集整理
- #pragma预处理实例
- BC#32 1002 hash
- C++_auto
- kingso_sort - Taocode
- struts2防止重复提交的标签
- innodb关键特性之double write
- Ubuntu搭建Gitlab服务器
- Koa 中的错误处理
- 在CentOs7上部署Gunicorn
- 下单快发货慢:一个 JOIN SQL 引起 SqlClient 读取数据慢的奇特问题
- C-Lodop回调函数的触发
- Myeclipse如何使用自带git工具向远程仓库提交代码
- POJ 2696
- 【CF884F】Anti-Palindromize 费用流
- shell 脚本中所有循环语法
- linux下查看doc在线帮助文件