首页 > 代码库 > 线程初级基础(一)
线程初级基础(一)
一,单线程例子
#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 processRunning in main processRunning in threadRunning in threadRunning in main processRunning in thread[root@localhost thread]# ./a.out Running in main processRunning in threadRunning in main processRunning in threadRunning in main processRunning in thread[root@localhost thread]# ./a.out Running in main processRunning in threadRunning in main processRunning in main processRunning in threadRunning in thread
线程初级基础(一)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。