多线程程序自动停止
本帖最后由 mingjiaoo415 于 2012-12-05 15:41:58 编辑 我开了两个子线程,都是循环的 为什么就运行了不到半分钟 就自动退出了呢 我用模拟的售票程序运行了一下 只能进行到ticket从10000 到8000多 线程就自动退出了 求解 vs2008中弄的
[最优解释]
你的代码怎么写的?
[其他解释]
发代码看看呀。
多线程知识点很多,楼主可以参考我写的《秒杀多线程面试题》,有十五篇文章。
博客地址:
http://blog.csdn.net/MoreWindows
专栏地址:
http://blog.csdn.net/column/details/killthreadseries.html
[其他解释]
最简单的代码了:
#include "stdafx.h"
#include <windows.h>
#include<iostream>
using namespace std;
void camera_1();
void camera_2();
HANDLE hevent;
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE handle1;
HANDLE handle2;
handle1 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)camera_1,NULL,0 ,NULL);
handle2 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)camera_2,NULL,0 ,NULL);
CloseHandle(handle1);
CloseHandle(handle2);
hevent = CreateEvent(NULL,false,true,NULL);
if(hevent)
{
if(GetLastError() == ERROR_ALIAS_EXISTS)
{
cout << "only one instance can run...." << endl;
printf("only one instance can run...\n");
return -1;
}
}
Sleep(1000);
return 0;
}
void camera_1()
{
WaitForSingleObject(hevent,INFINITE);
while(1)
{
printf("a\n");
SetEvent(hevent);
}
}
void camera_2()
{
WaitForSingleObject(hevent,INFINITE);
while(1)
{
printf("b\n");
SetEvent(hevent);
}
}
[其他解释]
知道了 主线程没有一直被挂起 主线程结束了