标签:style blog color io os ar for div sp
一,单线程例子
#include <stdio.h> #include <string.h> #include <iostream> #include <pthread.h> using namespace std; void *thread_func(void *arg) { for(int i=0; i<3; i++) { sleep(1); cout<<"Running in thread"<<endl; } return NULL; } int main() { pthread_t tid1; if(pthread_create(&tid1, NULL, thread_func, NULL) != 0) { cout<<"thread create error"<<endl; return -1; } for(int i=0; i<3; i++) { cout<<"Running in main process"<<endl; sleep(1); } pthread_join(tid1, NULL); return 0; }
输出结果:
[root@localhost thread]# ./a.out Running in main process Running in main process Running in thread Running in thread Running in main process Running in thread [root@localhost thread]# ./a.out Running in main process Running in thread Running in main process Running in thread Running in main process Running in thread [root@localhost thread]# ./a.out Running in main process Running in thread Running in main process Running in main process Running in thread Running in thread
标签:style blog color io os ar for div sp
原文地址:http://www.cnblogs.com/jacklikedogs/p/3974040.html