#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <iostream>
using namespace std;
void* threadFun(void* p)
{
char buf[ 9 * 1024 * 1024] = {0};
printf("in thread\n");
return 0;
}
int main(int argc, char *argv[])
{
pthread_t pThread ;
int ret = 0;
char buf[ 1 * 1024 * 1024] = {0};
//创建线程
ret = pthread_create(&pThread, NULL, threadFun, NULL );
if( ret != 0 )
{
cout << "pthread_create error:error_code=" << ret << endl;
}
//让主线程等待pThread线程结束
ret = pthread_join(pThread, NULL);
return 0;
}
编译命令:
g++ thread1.cpp -o thread1 -lpthread -g
因为使用到pthread_create()、pthread_join()等线程相关函数,所以我们编译时需要连接编程库"pthread"。