多线程编程实验
来源:互联网 发布:什么软件基于位图软件 编辑:程序博客网 时间:2024/05/16 17:46
/*thread.c*/#include <stdio.h>#include <stdlib.h>#include <pthread.h>#define THREAD_NUMBER 3 /*线程数*/#define REPEAY_NUMBER 5 /*每个线程中的小任务*/#define DELAY_TIME_LEVELS 10.0 /*小任务之间的最大时间间隔*/void *thrd_func(void *arg){/*线程函数例程*/ int thrd_num = (int)arg; int delay_time = 0; int count = 0; printf("Thread %d is starting \n",thrd_num); for(count = 0;count< REPEAY_NUMBER;count++) { delay_time = (int) (rand() * DELAY_TIME_LEVELS/(RAND_MAX))+1; sleep(delay_time); printf("\t Thread %d: job %d delay = %d\n",thrd_num,count,delay_time); } printf("Thread %d finished \n",thrd_num); pthread_exit(NULL);}int main(void){ pthread_t thread[THREAD_NUMBER]; int no = 0,res; void * thrd_ret; srand(time(NULL)); for(no =0;no < THREAD_NUMBER; no ++) { /*创建多线程*/ res = pthread_create(&thread[no],NULL,thrd_func,(void*)no); if(res != 0) { printf("Create thread %d failed \n",no); } } printf("Create tread success\n Waiting for threads to finish...\n"); for(no = 0;no <THREAD_NUMBER;no++) { /*等待线程结束*/ res = pthread_join(thread[no],&thrd_ret); if(!res) { printf("Thread %d jioned \n",no); } else { printf("Thread %d jion failed \n",no); } } return 0;}
0 0
- OpenMP多线程编程实验
- 多线程编程实验
- java实验7-多线程编程
- python多线程编程例子实验
- Windows CE下的多线程编程实验
- Linux 多线程编程实验知识整理
- Windows CE下的多线程编程实验
- 哈工大计算机网络实验一 :多线程服务器编程
- 【VS2013】windows多线程编程_初次实验
- 多线程实验
- 实验--多线程
- 第4周实验——多线程编程
- 网络编程实验3-并发多线程服务器设计
- python 多线程编程总结(实验多线程判断网址是否在线)
- java 多线程实验
- 实验题目:多线程
- java多线程简单实验
- C#多线程实验
- Java引用类型
- 康托展开
- For input string: "null"
- 在Ubuntu 16.04系统上安装WikkaWiki
- android—sdk遇到的问题- Support Libraries删除后找不到
- 多线程编程实验
- 将HTML特殊转义为实体字符的两种实现方式
- java内存管理
- shell常用符号和逻辑判断
- shell常用命令
- Android通用流行框架大全
- CSS伪类、伪元素
- 【GDOI2016】互补约数 题解
- Android之事件分发机制