子线程循环 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);
}

最新文章

  1. NTP校时设置
  2. ORACLE迁移votedisk,spfile以及OCRfile的方法
  3. linux分享六:字符串处理
  4. Github 安全类Repo收集整理
  5. #pragma预处理实例
  6. BC#32 1002 hash
  7. C++_auto
  8. kingso_sort - Taocode
  9. struts2防止重复提交的标签
  10. innodb关键特性之double write
  11. Ubuntu搭建Gitlab服务器
  12. Koa 中的错误处理
  13. 在CentOs7上部署Gunicorn
  14. 下单快发货慢:一个 JOIN SQL 引起 SqlClient 读取数据慢的奇特问题
  15. C-Lodop回调函数的触发
  16. Myeclipse如何使用自带git工具向远程仓库提交代码
  17. POJ 2696
  18. 【CF884F】Anti-Palindromize 费用流
  19. shell 脚本中所有循环语法
  20. linux下查看doc在线帮助文件

热门文章

  1. mysql安装及卸载
  2. html图片和文字的细节
  3. jQuery -&gt; 获取元素的各种过滤器
  4. hdu A Bug&#39;s Life
  5. windows信息
  6. 创建 PDO 实例并在构造函数中设置错误模式
  7. Window7 驱动编程环境配置
  8. xml案例(考生成绩管理系统)
  9. C#中的延迟加载
  10. JS的基础知识