#include <iostream>
#include "test.h"
#include <pthread.h>
using namespace std;
test::test(){}
test::~test(){}
void *threadFunction(){
cout << "this is a thread"<<endl;
for(;;);
}
void *thread(void*){
int i;
for(i=0;i<3;i++){
printf("this is a pthread.\n");
}
}
int main(){
//cout << "123" << endl;
pthread_t id;
int i,ret;
ret = pthread_create(&id,NULL,thread,NULL);
if(ret!=0){
printf("create pthread error!\n");
exit(1);
}
for(i=0;i<3;i++){
printf("This is the main process.\n");
pthread_join(id,NULL);
return 0;
}
}
编译的时候的命令是:
g++ test.h test.cpp -lpthread -o test
c++ 多线程
最新推荐文章于 2025-09-19 09:00:00 发布
本文通过一个C++程序示例介绍了如何使用pthread库创建和管理线程。该示例展示了两个线程的创建过程及运行效果:一个线程无限循环输出特定信息,另一个线程则循环三次输出信息。
7758

被折叠的 条评论
为什么被折叠?



