多线程编程代码如下:
#include <windows.h>
#include <iostream.h>DWORD WINAPI Func1Proc( //线程1
LPVOID LpParameter
);
DWORD WINAPI Func2Proc( //线程2
LPVOID LpParameter
);
int tickets=100;
HANDLE hMutex;
void main() //主线程
{
HANDLE hThread1,hThread2;
DWORD ThreadID1,ThreadID2;
hThread1=CreateThread(NULL,0,Func1Proc,NULL,0,&ThreadID1);
hThread2=CreateThread(NULL,0,Func2Proc,NULL,0,&ThreadID2);
CloseHandle(hThread1);
CloseHandle(hThread2);
cout<<"main Thread is running!"<<endl;
hMutex=CreateMutex(NULL,false,NULL);
Sleep(4000);
//Sleep(10);
} DWORD WINAPI Func1Proc(
LPVOID LpParameter
)
{
while(TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
cout<<"Thread1 sells tickets:"<<tickets--<<endl;
else
break;
ReleaseMutex(hMutex);
} return 0;
}DWORD WINAPI Func2Proc(
LPVOID LpParameter
)
{
while(TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
cout<<"Thread2 sells tickets:"<<tickets--<<endl;
else
break;
ReleaseMutex(hMutex);
}
return 0;
}调试出错,不知何因,请高手赐教!
-------------------Configuration: MultiThread - Win32 Debug--------------------
Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/MultiThread.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.MultiThread.exe - 2 error(s), 0 warning(s)
#include <windows.h>
#include <iostream.h>DWORD WINAPI Func1Proc( //线程1
LPVOID LpParameter
);
DWORD WINAPI Func2Proc( //线程2
LPVOID LpParameter
);
int tickets=100;
HANDLE hMutex;
void main() //主线程
{
HANDLE hThread1,hThread2;
DWORD ThreadID1,ThreadID2;
hThread1=CreateThread(NULL,0,Func1Proc,NULL,0,&ThreadID1);
hThread2=CreateThread(NULL,0,Func2Proc,NULL,0,&ThreadID2);
CloseHandle(hThread1);
CloseHandle(hThread2);
cout<<"main Thread is running!"<<endl;
hMutex=CreateMutex(NULL,false,NULL);
Sleep(4000);
//Sleep(10);
} DWORD WINAPI Func1Proc(
LPVOID LpParameter
)
{
while(TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
cout<<"Thread1 sells tickets:"<<tickets--<<endl;
else
break;
ReleaseMutex(hMutex);
} return 0;
}DWORD WINAPI Func2Proc(
LPVOID LpParameter
)
{
while(TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if(tickets>0)
cout<<"Thread2 sells tickets:"<<tickets--<<endl;
else
break;
ReleaseMutex(hMutex);
}
return 0;
}调试出错,不知何因,请高手赐教!
-------------------Configuration: MultiThread - Win32 Debug--------------------
Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/MultiThread.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.MultiThread.exe - 2 error(s), 0 warning(s)
/subsystem:windows
=>
/subsystem:console
setting中option中把subsystem:windows改为console
使用控制台console