linux多线程的创建基本知识
来源:互联网 发布:网络视频下载器 编辑:程序博客网 时间:2024/05/17 22:10
下列程序模拟两个线程同时执行,一个从1到100,一个从50到100:
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <string.h>typedef struct stc_arg{ int start; int end;}stc_arg;void *th_fun(void *arg){ stc_arg *data = (stc_arg*)arg; int i = data->start; for(i; i <= data->end; ++i) { //pthread_self():get thread id printf("pthread_t: %ld, i= %d\n", pthread_self(), i); //rand creat 0.0~1.0 No. double r = drand48(); int rt = (int)(r * 100000); usleep(rt); }}int main(void){ pthread_t pth1; pthread_t pth2; int err; stc_arg arg1 = {1, 100}; if ((err = pthread_create(&pth1, NULL, th_fun, (void*)&arg1)) != 0) { fprintf(stderr, "pthread_create:%s\n", strerror(err)); exit(1); } stc_arg arg2 = {50, 100}; if ((err = pthread_create(&pth2, NULL, th_fun, (void*)&arg2)) != 0) { fprintf(stderr, "pthread_create:%s\n", strerror(err)); exit(1); } sleep(10); printf("finish!\n"); return 0;}
注意编译多线程要使用-lpthread选项。
0 0
- linux多线程的创建基本知识
- 多线程的基本知识
- 多线程的基本知识
- Linux进程的基本知识
- Linux的一些基本知识
- 02Linux的基本知识
- linux互斥锁的基本知识
- Linux防火墙的基本知识
- 多线程基本知识
- 多线程 - 基本知识
- 多线程基本知识
- 多线程基本知识
- 多线程基本知识
- 多线程基本知识
- linux多线程编程1--线程的创建
- linux多线程编程1--线程的创建
- LINUX多线程pipeline架构的创建分析
- linux学习之十三---多线程的创建
- 河南第八届省赛(最小换乘)(最短路径,DP)
- Android Studio中Button ,Textview中字体自动变成大写的问题
- android自定义view实现"偷窥"图片
- Java注解Annotation学习(例子实践篇)
- 进阶篇,第一章:矿物的生成
- linux多线程的创建基本知识
- ServletConfig实现简单的登录操作
- 非root权限安装软件
- codevs1080线段树练习(线段树)
- 用Python写的hilbert曲线生成代码
- JAVA快捷键使用
- 树状数组。。
- HDU 1358 Period (kmp求周期)
- iOS、Mac代码规范