test1.cpp
来自「linux环境下 多线程的简单使用」· C++ 代码 · 共 52 行
CPP
52 行
#include <pthread.h>#include <iostream.h>#include <stdlib.h>void* thread_1(void *p){ while (1) { cout<<"1"<<endl; usleep(300000); } return NULL;}void* thread_2(void *p){ while (1) { cout<<"2"<<endl; usleep(300000); } return NULL;}int main(int argc,char **argv){ pthread_t ntid_send, ntid_rcv; int Err; Err=pthread_create(&ntid_send,NULL,thread_1, NULL); if(Err!=0) printf("Create thread failed,thread index: ntid_send \n"); Err=pthread_create(&ntid_rcv,NULL,thread_2, NULL); if(Err!=0) printf("Create thread failed,thread index: ntid_rcv.\n"); int err1=pthread_join(ntid_send,NULL); int err2=pthread_join(ntid_rcv,NULL); if(err1!=0 || err2!=0) { printf("The child thread: %d %d.\r\n",err1,err2); } return 0;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?