c/c++中主线程退出,子线程也会退出

 1 #include <windows.h>
 2 #include <process.h> /* _beginthread, _endthread */
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 void show(void *ptr);
 8 
 9 int main(){
10     _beginthread(show, 0, NULL);  //创建一个子线程
11     Sleep(10000);
12     return 0;
13 }
14 
15 void show(void *ptr){
16     int i = 0;
17     cout<<"i am in"<<endl;
18     while(true){
19         cout<<"i = "<<i++<<endl;
20         Sleep(1000);
21     }
22     
23 }

主线程睡眠10s中,子线程每隔1s向控制台输出I。可以看到10s过后子线程不在输出信息,子线程随主线程的退出而退出

posted on 2015-01-13 17:17  luckygxf  阅读(2594)  评论(0编辑  收藏  举报

导航