linux创建、取消多线程的简单实用的模板例子
来源:互联网 发布:图片转换成文字软件 编辑:程序博客网 时间:2024/05/21 09:20
/****************************************************************** * simple.c -- multithreaded "hello world" * * Author: Mark Hays <hays@math.arizona.edu> *//* Linux with glibc: * _REENTRANT to grab thread-safe libraries * _POSIX_SOURCE to get POSIX semantics */#ifdef __linux__# define _REENTRANT# define _POSIX_SOURCE#endif/* Hack for LinuxThreads */#ifdef __linux__# define _P __P#endif#include <pthread.h>#include <string.h>/* for strerror() */#include <stdio.h>#define NTHREADS 4#define errexit(code,str) \ fprintf(stderr,"%s: %s\n",(str),strerror(code)); \ exit(1);/******** this is the thread code */void *hola(void * arg){ int myid=*(int *) arg; printf("Hello, world, I'm %d\n",myid); return arg;}/******** this is the main thread's code */int main(int argc,char *argv[]){ int worker; pthread_t threads[NTHREADS]; /* holds thread info */ int ids[NTHREADS]; /* holds thread args */ int errcode; /* holds pthread error code */ int *status; /* holds return code */ /* create the threads */ for (worker=0; worker<NTHREADS; worker++) { ids[worker]=worker; if (errcode=pthread_create(&threads[worker],/* thread struct */ NULL, /* default thread attributes */ hola, /* start routine */ &ids[worker])) { /* arg to routine */ errexit(errcode,"pthread_create"); } } /* reap the threads as they exit */ for (worker=0; worker<NTHREADS; worker++) { /* wait for thread to terminate */ if (errcode=pthread_join(threads[worker],(void *) &status)) { errexit(errcode,"pthread_join"); } /* check thread's exit status and release its resources */ if (*status != worker) { fprintf(stderr,"thread %d terminated abnormally\n",worker); exit(1); } } return(0);}/* EOF simple.c */
更详细的来源:http://math.arizona.edu/~swig/documentation/pthreads/#createdestroy
- linux创建、取消多线程的简单实用的模板例子
- 多线程的创建例子
- autoconf简单实用的例子
- digester简单实用的例子
- 简单实用的socket例子
- linux 一个简单的进程创建例子
- 简单的多线程创建,执行,挂起,终止的例子
- delphi中用api创建多线程的简单例子
- 简单利用模板的例子
- Linux下一个简单的多线程互斥锁的例子
- 多线程编程的简单例子
- qt多线程的简单例子
- ios多线程的简单例子
- 一个简单实用的AJAX例子
- javascript中的简单.实用的例子
- 一个简单实用的AJAX例子
- Android中 AIDL 的简单实用例子
- linux C++ 多线程 pthread_cond_t 使用的简单例子
- 解决方案:Failed to install helloworld.apk on device 'emulator-5554'
- keil c 知识总结
- jmeter 技巧 参数 正则表达式
- 类似QQ表情的功能,包括动态绑定图片
- c++链表(随手写的)
- linux创建、取消多线程的简单实用的模板例子
- 构建大型网站架构服务器集群
- uC/OS-II在C8051F020单片机上的移植
- 空间点到平面的垂足坐标的计算方法
- 关于Qt Webkit release
- 用三张图片详解Asp.Net 全生命周期
- Jmeter-使用正则表达式提取器获取关联参数
- 重新发布【谷歌输入法用户词典整理工具】
- 表达式即编译器——C# lambda 表达式高级编程