8 个解决方案
#1
贴下代码看看
#2
无论主线程是否sleep或者处于等待状态,其创建的线程只要是初期创建的时候没有指定挂起,那么这个线程 进入了执行状态,如果主线程要是退出了,这意味着进程结束了,那么它所创建的线程也就被强制退出。使主线程序处于运行状态可以使用消息循环,也可以让它空转,也可以使用WaitForXXXX系列函数使主线程进入等待状态。
#3
楼主给点代码,不是谁手中都有本孙鑫的VC书的啊
#4
那本书我看过,至少15章的代码没有错,你再往后看几页他的解释
#5
教程没有错 你把代码贴上看看
#6
应该是这个吧?
#include <windows.h>
#include <iostream.h>
DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
);
DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
);
int index=0;
int tickets=100;
HANDLE hMutex;互斥对象的句柄
void main()
{
HANDLE hThread1;
HANDLE hThread2;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);创建线程1
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);创建线程2
CloseHandle(hThread1);关闭线程的句柄,为什么要关闭?它将线程的使用计数减1
CloseHandle(hThread2);这样当线程结束时,线程内核对象被释放,否则只有当进程结束,才释放线程的内核对象
/*while(index++<1000)
cout<<"main thread is running"<<endl;*/
//hMutex=CreateMutex(NULL,TRUE,NULL);将第二个参数设为true后,互斥对象的计数加1
hMutex=CreateMutex(NULL,TRUE,"tickets");此段代码可以让系统只一份实例在运行!
if(hMutex)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only instance can run!"<<endl;
return;
}
}
WaitForSingleObject(hMutex,INFINITE);此代码也将コ舛韵蟮募剖?
ReleaseMutex(hMutex);所以要释放两次互斥对象
ReleaseMutex(hMutex);
Sleep(4000);睡眠4000毫秒
// Sleep(10);
}
DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
/*while(index++<1000)
cout<<"thread1 is running"<<endl;*/
/*while(TRUE)
{
//ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);等待互斥对象的到来,到来后将互斥对象的计数加1
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);释放互斥对象,将其计数减1,这样可以保证,这两句话之间 的代码!的执行连续性!
}*/
WaitForSingleObject(hMutex,INFINITE);
cout<<"thread1 is running"<<endl;
return 0;
}
DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
)
{
/*while(TRUE)
{
//ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread2 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}*/
WaitForSingleObject(hMutex,INFINITE);
cout<<"thread2 is running"<<endl;
return 0;
}
#include <windows.h>
#include <iostream.h>
DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
);
DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
);
int index=0;
int tickets=100;
HANDLE hMutex;互斥对象的句柄
void main()
{
HANDLE hThread1;
HANDLE hThread2;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);创建线程1
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);创建线程2
CloseHandle(hThread1);关闭线程的句柄,为什么要关闭?它将线程的使用计数减1
CloseHandle(hThread2);这样当线程结束时,线程内核对象被释放,否则只有当进程结束,才释放线程的内核对象
/*while(index++<1000)
cout<<"main thread is running"<<endl;*/
//hMutex=CreateMutex(NULL,TRUE,NULL);将第二个参数设为true后,互斥对象的计数加1
hMutex=CreateMutex(NULL,TRUE,"tickets");此段代码可以让系统只一份实例在运行!
if(hMutex)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only instance can run!"<<endl;
return;
}
}
WaitForSingleObject(hMutex,INFINITE);此代码也将コ舛韵蟮募剖?
ReleaseMutex(hMutex);所以要释放两次互斥对象
ReleaseMutex(hMutex);
Sleep(4000);睡眠4000毫秒
// Sleep(10);
}
DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
/*while(index++<1000)
cout<<"thread1 is running"<<endl;*/
/*while(TRUE)
{
//ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);等待互斥对象的到来,到来后将互斥对象的计数加1
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);释放互斥对象,将其计数减1,这样可以保证,这两句话之间 的代码!的执行连续性!
}*/
WaitForSingleObject(hMutex,INFINITE);
cout<<"thread1 is running"<<endl;
return 0;
}
DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
)
{
/*while(TRUE)
{
//ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread2 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}*/
WaitForSingleObject(hMutex,INFINITE);
cout<<"thread2 is running"<<endl;
return 0;
}
#7
为什么我们电脑上可以运行多个实例,而不是只有一个实例?我用的是vc++6.0,是否要设置哪里?
#8
#1
贴下代码看看
#2
无论主线程是否sleep或者处于等待状态,其创建的线程只要是初期创建的时候没有指定挂起,那么这个线程 进入了执行状态,如果主线程要是退出了,这意味着进程结束了,那么它所创建的线程也就被强制退出。使主线程序处于运行状态可以使用消息循环,也可以让它空转,也可以使用WaitForXXXX系列函数使主线程进入等待状态。
#3
楼主给点代码,不是谁手中都有本孙鑫的VC书的啊
#4
那本书我看过,至少15章的代码没有错,你再往后看几页他的解释
#5
教程没有错 你把代码贴上看看
#6
应该是这个吧?
#include <windows.h>
#include <iostream.h>
DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
);
DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
);
int index=0;
int tickets=100;
HANDLE hMutex;互斥对象的句柄
void main()
{
HANDLE hThread1;
HANDLE hThread2;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);创建线程1
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);创建线程2
CloseHandle(hThread1);关闭线程的句柄,为什么要关闭?它将线程的使用计数减1
CloseHandle(hThread2);这样当线程结束时,线程内核对象被释放,否则只有当进程结束,才释放线程的内核对象
/*while(index++<1000)
cout<<"main thread is running"<<endl;*/
//hMutex=CreateMutex(NULL,TRUE,NULL);将第二个参数设为true后,互斥对象的计数加1
hMutex=CreateMutex(NULL,TRUE,"tickets");此段代码可以让系统只一份实例在运行!
if(hMutex)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only instance can run!"<<endl;
return;
}
}
WaitForSingleObject(hMutex,INFINITE);此代码也将コ舛韵蟮募剖?
ReleaseMutex(hMutex);所以要释放两次互斥对象
ReleaseMutex(hMutex);
Sleep(4000);睡眠4000毫秒
// Sleep(10);
}
DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
/*while(index++<1000)
cout<<"thread1 is running"<<endl;*/
/*while(TRUE)
{
//ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);等待互斥对象的到来,到来后将互斥对象的计数加1
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);释放互斥对象,将其计数减1,这样可以保证,这两句话之间 的代码!的执行连续性!
}*/
WaitForSingleObject(hMutex,INFINITE);
cout<<"thread1 is running"<<endl;
return 0;
}
DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
)
{
/*while(TRUE)
{
//ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread2 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}*/
WaitForSingleObject(hMutex,INFINITE);
cout<<"thread2 is running"<<endl;
return 0;
}
#include <windows.h>
#include <iostream.h>
DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
);
DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
);
int index=0;
int tickets=100;
HANDLE hMutex;互斥对象的句柄
void main()
{
HANDLE hThread1;
HANDLE hThread2;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);创建线程1
hThread2=CreateThread(NULL,0,Fun2Proc,NULL,0,NULL);创建线程2
CloseHandle(hThread1);关闭线程的句柄,为什么要关闭?它将线程的使用计数减1
CloseHandle(hThread2);这样当线程结束时,线程内核对象被释放,否则只有当进程结束,才释放线程的内核对象
/*while(index++<1000)
cout<<"main thread is running"<<endl;*/
//hMutex=CreateMutex(NULL,TRUE,NULL);将第二个参数设为true后,互斥对象的计数加1
hMutex=CreateMutex(NULL,TRUE,"tickets");此段代码可以让系统只一份实例在运行!
if(hMutex)
{
if(ERROR_ALREADY_EXISTS==GetLastError())
{
cout<<"only instance can run!"<<endl;
return;
}
}
WaitForSingleObject(hMutex,INFINITE);此代码也将コ舛韵蟮募剖?
ReleaseMutex(hMutex);所以要释放两次互斥对象
ReleaseMutex(hMutex);
Sleep(4000);睡眠4000毫秒
// Sleep(10);
}
DWORD WINAPI Fun1Proc(
LPVOID lpParameter // thread data
)
{
/*while(index++<1000)
cout<<"thread1 is running"<<endl;*/
/*while(TRUE)
{
//ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);等待互斥对象的到来,到来后将互斥对象的计数加1
if(tickets>0)
{
Sleep(1);
cout<<"thread1 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);释放互斥对象,将其计数减1,这样可以保证,这两句话之间 的代码!的执行连续性!
}*/
WaitForSingleObject(hMutex,INFINITE);
cout<<"thread1 is running"<<endl;
return 0;
}
DWORD WINAPI Fun2Proc(
LPVOID lpParameter // thread data
)
{
/*while(TRUE)
{
//ReleaseMutex(hMutex);
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
{
Sleep(1);
cout<<"thread2 sell ticket : "<<tickets--<<endl;
}
else
break;
ReleaseMutex(hMutex);
}*/
WaitForSingleObject(hMutex,INFINITE);
cout<<"thread2 is running"<<endl;
return 0;
}
#7
为什么我们电脑上可以运行多个实例,而不是只有一个实例?我用的是vc++6.0,是否要设置哪里?