pthread_create() 创建线程,pthread_join()让线程一直运行下去。

链接时要加上-lpthread选项。

pthread_create中, 第三个参数为线程函数,定义如下:

  void * heartbeat_thread()

  {

    ...

  }

下面是main.c :

#include <pthread.h>

pthread_t thread[MAX_THREAD_NUM];
pthread_mutex_t cache_mutex;
pthread_mutex_t variable_mutex;
pthread_mutex_t time_mutex;
pthread_mutex_t msg_mutex; void thread_create(void)
{
int temp;
memset(thread, , sizeof(pthread_t) * MAX_THREAD_NUM);
if((temp = pthread_create(&thread[], NULL, heartbeat_thread, NULL)) != )
printf("heartbeat thread create failed\n");
if((temp = pthread_create(&thread[], NULL, timesync_thread, NULL)) != )
printf("tiemsync thread create failed\n");
if((temp = pthread_create(&thread[], NULL, udpclient_thread, NULL)) != )
printf("udpclient thread create failed\n");
if((temp = pthread_create(&thread[], NULL, udpserver_thread, NULL)) != )
printf("udpserver thread create failed\n");
if((temp = pthread_create(&thread[], NULL, msgdisplay_thread, NULL)) != )
printf("msgdisplay thread create failed\n");
} void thread_wait(void)
{
if(thread[] !=)
{
  pthread_join(thread[],NULL);
  printf("heartbeat thread finish\n");
}
if(thread[] !=)
{
pthread_join(thread[],NULL);
printf("tiemsync thread finish\n");
}
if(thread[] !=)
{
pthread_join(thread[],NULL);
printf("udpclient thread finish\n");
}
if(thread[] !=)
{
pthread_join(thread[],NULL);
printf("udpserver thread finish\n");
}
if(thread[] !=)
{
pthread_join(thread[],NULL);
printf("msgdisplay thread finish\n");
}
} int main(void)
{
pthread_mutex_init(&cache_mutex,NULL);
pthread_mutex_init(&variable_mutex,NULL);
pthread_mutex_init(&time_mutex,NULL);
pthread_mutex_init(&msg_mutex,NULL); thread_create();
thread_wait(); pthread_mutex_destroy(&msg_mutex);
pthread_mutex_destroy(&time_mutex);
pthread_mutex_destroy(&variable_mutex);
pthread_mutex_destroy(&cache_mutex);
return ;
}

最新文章

  1. 树链剖分+线段树 BZOJ 1036 [ZJOI2008]树的统计Count
  2. About 滚存
  3. CSS3自动添加省略号
  4. 解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法
  5. Mac安装Brew
  6. 在winform中添加普通右键菜单
  7. 【读书笔记】读《JavaScript设计模式》之工厂模式
  8. [HDOJ1175]连连看
  9. Android WebRTC 音视频开发总结(四)-- webrtc传输模块
  10. Android 4.4环境搭建——Android SDK下载与安装
  11. RabbitMQ (两)工作队列
  12. 【软件project】生存期模型(含图)
  13. element-ui中上传文件upload
  14. scrapy使用MongoDB简单示例
  15. C#中byte[] 与string相互转化问题
  16. JS面试题(一)
  17. Boost 常用的库
  18. 网络基础之socket
  19. Python ————反射机制
  20. FFmpeg简易播放器的实现-最简版

热门文章

  1. Flutter移动电商实战 --(18)首页_火爆专区商品接口制作
  2. OpenCL多次循环执行内核的一个简单样例
  3. android popupwindow 位置显示
  4. Qt编写安防视频监控系统8-双击节点
  5. String字符串的截取
  6. 解决Python-OpenCV中cv2.rectangle报错
  7. 中国行政区划表,包括34个省、直辖市的所有数据 mysql数据
  8. Python3 Selenium自动化web测试 ==&gt; 第十一节 WebDriver高级应用 -- 显示等待 + 二次封装
  9. sed工具的基本用法
  10. Qt5.&quot;Clang Code Model&quot;一些设置