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 ;
}

linux pthread多线程编程模板的更多相关文章

  1. [转]Linux 的多线程编程的高效开发经验

    Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多线程 API 有一些细微和隐晦的差别.不注意这些 Linux 上的一些开发陷阱,常常会导致程序问题不穷,死锁不断.本文中我们 ...

  2. 转载自~浮云比翼:Step by Step:Linux C多线程编程入门(基本API及多线程的同步与互斥)

    Step by Step:Linux C多线程编程入门(基本API及多线程的同步与互斥)   介绍:什么是线程,线程的优点是什么 线程在Unix系统下,通常被称为轻量级的进程,线程虽然不是进程,但却可 ...

  3. pthread多线程编程的学习小结

    pthread多线程编程的学习小结  pthread 同步3种方法: 1 mutex 2 条件变量 3 读写锁:支持多个线程同时读,或者一个线程写     程序员必上的开发者服务平台 —— DevSt ...

  4. Linux 的多线程编程的高效开发经验(转)

    http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/ 背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多 ...

  5. Linux 的多线程编程的高效开发经验

    http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/ 背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多 ...

  6. Linux下多线程编程遇到的一些问题

    今天在学习了Linux的多线程编程的基础的知识点.于是就试着做了一个简单的Demo.本以为会得到预期的结果.不成想却遇到了意想不到的问题. 代码展示 我的C 代码很简单,就是一个简单的示例程序,如下: ...

  7. clone的fork与pthread_create创建线程有何不同&pthread多线程编程的学习小结(转)

    进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合,这些资源在Linux中被抽 象成各种数据对象:进程控制块.虚存空间.文件系统,文件I/O.信号处理函数.所以创建一个进程的 过程就是这 ...

  8. VC++6.0 下配置 pthread库2010年12月12日 星期日 13:14VC下的pthread多线程编程 转载

    VC++6.0 下配置 pthread库2010年12月12日 星期日 13:14VC下的pthread多线程编程     转载 #include <stdio.h>#include &l ...

  9. Linux下多线程编程

    一.为什么要引入线程? 使用多线程的理由之一是和进程相比,它是一种非常"节俭"的多任务操作方式.在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维 ...

随机推荐

  1. git clone速度太慢解决方案

    原文地址:https://blog.csdn.net/hzwwpgmwy/article/details/79043251 适用各种操作系统,本次测试于ubuntu,下载速度从二十几k提高到二百多k ...

  2. arcgis python 随机取部分数据

    # -*- coding: cp936 -*- import arcpy import os import ylpy import random def main(): num=ylpy.getCou ...

  3. (六)爬虫之使用selenium

    selenium是使用javascript编写,主要用来进行web应用程序测试,在python爬虫中可以用来进行动态网页爬取,解决爬虫中的javascript渲染(执行js语句).总结记录下,以备后面 ...

  4. linux设置脚本开机自启

    由于在centos7中/etc/rc.d/rc.local的权限被降低了,所以需要赋予其可执行权 chmod +x /etc/rc.d/rc.local 赋予脚本可执行权限假设/opt/script/ ...

  5. JS创建类和对象,看完了,头就不大了

    JavaScript 创建类/对象的几种方式 在JS中,创建对象(Create Object)并不完全是我们时常说的创建类对象,JS中的对象强调的是一种复合类型,JS中创建对象及对对象的访问是极其灵活 ...

  6. swift 第九课 用tableview 做一个下拉菜单Menu

    写到这里的时候,自己这个项目已经完成了一半左右,项目进度自己还是挺满意.今天又有一个新的布局,要实现个下拉菜单,刚开始写的时候,觉得会很容易,后来发现也是小错不断, 我想自己限制的自己属于写博客的初期 ...

  7. Flutter中通过循环渲染组件

    class ContactsState extends State<Contacts>{ List formList; initState() { super.initState(); f ...

  8. C语言字符串之无重复字符的最长子串

    题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 输入: "abcabcbb" 输出: 解释: 因为无重复字符的最长子串是 . 输入: " ...

  9. [mysql] C++操作mysql方法

    下载:http://mirrors.sohu.com/mysql/MySQL-5.5/ From: http://www.cnblogs.com/magicsoar/p/3817518.html C+ ...

  10. shell 监控

    #!/bin/shsource /etc/profileserverName=$1dingDingName=$2 #获取内存情况memory=(`free | awk 'NR==2{print $2, ...