Linux 下的多线程编程(1)
#include<stdio.h>
#include<pthread.h>
#include<string.h>
#include<sys/time.h>
#define MAX 20
//定义三个线程共同完成计数任务
pthread_t thread[];
pthread_mutex_t mut;//定义互斥锁变量
int num=;
int i; void *thread1(){
printf("thread1:im thread 1");
for(i=;i<MAX;i++){
printf("thread1:num=%d \n",num);
pthread_mutex_lock(&mut);
num++;
pthread_mutex_unlock(&mut);
sleep();
}
pthread_exit(NULL);
}
void *thread2(){
printf("thread1:im thread 1");
for(i=;i<MAX;i++){
printf("thread1:num=%d \n",num);
pthread_mutex_lock(&mut);
num++;
pthread_mutex_unlock(&mut);
sleep();
}
pthread_exit(NULL);
}
void *thread3(){
printf("thread1:im thread 1");
for(i=;i<MAX;i++){
printf("thread1:num=%d \n",num);
pthread_mutex_lock(&mut);
num++;
pthread_mutex_unlock(&mut);
sleep();
}
pthread_exit(NULL);
}
void main(){
pthread_mutex_init(&mut,NULL);//使用默认属性来初始化互斥锁
pthread_create(&thread[],NULL,thread1,NULL);//创建线程并没有运行
pthread_create(&thread[],NULL,thread1,NULL);
pthread_create(&thread[],NULL,thread1,NULL);
pthread_join(thread[],NULL);//把线程1加入等待开始执行
pthread_join(thread[],NULL);
pthread_join(thread[],NULL);
}
Linux 下的多线程编程(1)的更多相关文章
- 【转】 Linux下的多线程编程
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/原文链接:http://www.cnblogs.com/gnuhpc/archive/2012/12/07/280 ...
- Linux下的多线程编程
1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,但是在一个进程(proces ...
- 【转】Linux下的多线程编程
1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,但是在一个进程(proces ...
- 《转》Linux下的多线程编程
原地址:http://linux.chinaunix.net/doc/program/2001-08-11/642.shtml 1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程 ...
- 【转】Linux下的多线程编程背景知识
1. 进程和线程 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,但是在一个进程(pr ...
- Linux 下的多线程编程
随着你对编程的深入,多线程是一个免不了的话题,在这里就对多线程做一个比较详细的总结. 首先摆在我们面前的就是什么是线程,以及为么会有这个东西.记得之前学习的时候自己会画一张很大的图,在图中可以详细的写 ...
- Linux下的C编程实战
Linux下的C编程实战(一) ――开发平台搭建 1.引言 Linux操作系统在服务器领域的应用和普及已经有较长的历史,这源于它的开源特点以及其超越Windows的安全性和稳定性.而近年来, Linu ...
- Linux C++的多线程编程(转)
1. 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的Unix也支持线程的概念,但是在一个进程(proces ...
- Linux C语言多线程编程实例解析
Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...
随机推荐
- this,super,和继承
this是指当前对象的引用,super是指直接父类的引用 比如 我建造一个类 public class Person(){ private String name; private int age; ...
- Problem E: 积木积水 ——————【模拟】
Problem E: 积木积水 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元 ...
- Python sh模块--------替换subprocess的利器
官方文档有句话"allows you to call any program",并且: helps you write shell scripts in Python by giv ...
- git打补丁、还原补丁
打补丁.还原补丁 1.两个commit间的修改(包含两个commit,<r1>.<r2>表示两个提交的版本号,<r1>是最近提交) git format-patch ...
- oracle学习篇九:同义词
Oracle数据库中提供了同义词管理的功能.Oracle同义词是数据库方案对象的一个别名,经常用于简化对象访问和提高对象访问的安全性. 在Oracle中对用户的管理是使用权限的方式来管理的,也就是说, ...
- axios 发 post 请求,后端接收不到参数的解决方案
问题场景 场景很简单,就是一个正常 axios post 请求: axios({ headers: { 'deviceCode': 'A95ZEF1-47B5-AC90BF3' }, method: ...
- [PAMI 2018] Differential Geometry in Edge Detection: accurate estimation of position, orientation and curvature
铛铛铛,我的第一篇文章终于上线了,过程曲折,太不容易了--欢迎访问--- https://ieeexplore.ieee.org/document/8382271/ 后面有需要的话可以更新一下介绍,毕 ...
- C++基础--指针,&的用法
#include "stdafx.h" #include <stdio.h> #include <string.h> int main() { ] = {, ...
- Win8 Pro 64 Install .net3.5 在线升级会遇到错误0x800F0906。
很多人安装Win8后都遇到了无法升级.NET Framework 3.5.1的问题,在线升级会遇到错误0x800F0906. Uninstall Windows update kB: KB296682 ...
- SpringMVC方法接收参数可以为空、默认值设置