代码:
#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
void *threadRoutine(void *args)
{
int i=0;
while(true)
{
cout << "新线程:"<< (char*)args << " running ... " <<endl;
sleep(1);
if(i++ == 10)break;
}
cout << "new thread quit ..." << endl;
}
int main()
{
pthread_t tid;
pthread_create(&tid,nullptr,threadRoutine,(void *)"thread 1");
pthread_join(tid,nullptr);//
cout << "main thread wait done...main quit ...\n";
return 0;
}
运行结果: