编译环境 Ubuntu18.04 G++
#include <pthread.h>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
using namespace std;
void * funct(void * arg)
{
static int i=0;
printf("hello %d\n",i);
i++;
sleep(4);
printf("wake UP%d\n",i);
i++;
}
int main()
{
pthread_t threadFunctID;
int returnvalue=pthread_create(&threadFunctID,NULL,&funct,NULL);
if (returnvalue)
{
//成功为0 失败有值
printf("error");
}
else
{
printf("success\n");
cout<<"thread1 ID is "<<threadFunctID<<endl;
}
cout<<pthread_self<<"!!!!!"<<endl;
int returnvalue2=pthread_create(&threadFunctID,NULL,&funct,NULL);
if (returnvalue2)
{
//成功为0 失败有值
printf("error");
}
else
{
printf("success\n");
cout<<"thread2 ID is"<<threadFunctID<<endl;
}
cout<<pthread_self<<"!!!!!"<<endl;
//way2 use detach
int err=pthread_detach(threadFunctID);
if(err)
{
cout<<"error"<<endl;
}
sleep(8); //the main function wait for thread to exit
// you can test it for change the
//way1 use join
// cout<< "wait for thread to exit\n";
// returnvalue=pthread_join(threadFunctID,NULL);
// if(returnvalue)
// {
// printf("error");
// }
cout<<"Exit main\n";
return 0;
}