#include <pthread.h>
#include <iostream>
#include <unistd.h>
using namespace std;
//volatile bool g_brun = true;
bool g_brun = true;
void* TestThread1(void* arg)
{
cout << "TestThread1 进入" << endl;
long long ll = 0;
while(g_brun)
ll ++;
cout << "TestThread1 退出 ll:" << ll << endl;
}
void* TestThread2(void* arg)
{
cout << "TestThread2 进入" << endl;
g_brun = false;
cout << "TestThread2 退出 设置 g_brun = false" << endl;
}
int main()
{
pthread_t threadId1;
pthread_create(&threadId1, NULL, TestThread1, NULL);
usleep(1000000); // 保证TestThread1先执行
pthread_t threadId2;
pthread_create(&threadId2, NULL, TestThread2, NULL);
pthread_join(threadId1,NULL);
pthread_join(threadId2,NULL);
return 0;
}
原文地址:http://blog.51cto.com/13611395/2088655