多线程(2)
来源:互联网 发布:软件系统集成技术方案 编辑:程序博客网 时间:2024/05/03 18:03
pthread_mutex_init
函数作用:初始化互斥锁
函数原型:init pthreaad_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutex *attr_t)
函数参数:mutex:互斥锁
attr_t:快速互斥锁
返回值:成功为0,失败<0
pthread_mutex_lock
函数作用:对互斥锁上锁
函数原型:init pthreaad_mutex_lock(pthread_mutex_t *mutex)
函数参数:mutex:互斥锁
返回值:成功为0,失败<0
//判断是否上锁init pthreaad_mutex_trylock(pthread_mutex_t *mutex);//解锁init pthreaad_mutex_unlock(pthread_mutex_t *mutex);//销毁锁init pthreaad_mutex_destroy(pthread_mutex_t *muteex);
程序
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#define THREAD_NUM 3#define REPEAT_NUM 3#define DELAY_TIME_LEVELS 6.0 pthread_mutex_t mutex;void *thrd_func(void *arg){ int thrd_num = (int)arg; int delay_time =0, count=0; int res; res = pthread_mutex_lock(&mutex); printf("Thread %d is starting\n", thrd_num); for(count=0; count<REPEAT_NUM; count++) { printf("\tThread %d:job %d delay=%d\n", thrd_num,count); } printf("Thread %d finish\n", thrd_num); pthread_mutex_unlock(&mutex); pthread_exit(NULL);}int main(void){ pthread_t thread[THREAD_NUM]; int no=0, res; void *thrd_ret; pthread_mutex_init(&mutex, NULL); for(no=0; no<THREAD_NUM; no++) { res=pthread_create(&thread[no], NULL, thrd_func, (void *)no); } printf("creatr threads success\nwaiting for threads to finish....\n"); for(no=0; no<THREAD_NUM; no++) { res=pthread_join(thread[no], &thrd_ret); }thread_mutex_destroy(&mutex); return 0;}
0 0
- Java Tread多线程(2)多线程安全问题
- 多线程(2/2)
- delphi多线程(2)
- 多线程(1/2)
- java 多线程(2)
- 多线程(2)
- linux多线程(2)
- 多线程(2)
- 多线程(2)锁
- 多线程(2)
- 多线程浅析(2)
- 多线程(2)
- JAVA多线程(2)
- JavaSE 多线程(2)
- 多线程(2)
- 多线程(2)-- yield( )
- 多线程面试(2)
- Java多线程(2)
- spark-框架执行流程概况standalone模式
- C++常指针与指针常量
- 有趣的指针(一)
- 新手作业android三步走
- Linux中vim编辑器常用命令(持续更新)
- 多线程(2)
- 雷涛的小猫
- html5新增元素
- sqlite3笔记(二)
- 基于肤色的人脸检测与分割
- 11.1 HTML5 HTML 4.01 和XHTML的区别
- 常用图像数据集大全(分类,跟踪,分割,检测等)
- android在滑动控件过程中动态设置view背景透明的问题
- 树的同构