24,854
社区成员
发帖
与我相关
我的任务
分享
VC6
i586-mingw32msvc-g++ 3.4.5
MinGw g++ 3.4.2
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
HANDLE hMutex, hWriteDone, hReadDone;
int num, state;
void Writer()
{
for(int x=10; x>=0; x--)
{
while (true)
{
if (WaitForSingleObject(hMutex, INFINITE) == WAIT_FAILED)
{
std::cout<<"In writing loop, no mutex!\n";
ExitThread(0);
}
if (state == 0)
{
ReleaseMutex(hMutex);
WaitForSingleObject(hReadDone, INFINITE);
continue;
}
break;
}
std::cout<<"Write done\n";
num= x;
state= 0;
ReleaseMutex(hMutex);
PulseEvent(hWriteDone);
}
}
void Reader()
{
while(true)
{
if (WaitForSingleObject(hMutex, INFINITE) == WAIT_FAILED)
{
std::cout<<"In reader, no mutex!\n";
ExitThread(0);
}
if (state == 1)
{
ReleaseMutex(hMutex);
WaitForSingleObject(hWriteDone, INFINITE);
continue;
}
if (num == 0)
{
std::cout<<"End of data\n";
ReleaseMutex(hMutex);
ExitThread(0);
}
else {
std::cout<<"Read done\n";
state = 1;
ReleaseMutex(hMutex);
PulseEvent(hReadDone);
}
}
}
int main()
{
HANDLE TName[2];
DWORD ThreadID;
state = 1;
hMutex = CreateMutex(NULL, FALSE, NULL);
hWriteDone = CreateEvent(NULL, TRUE, FALSE, NULL);
hReadDone = CreateEvent(NULL, TRUE, FALSE, NULL);
TName[0] = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)Writer,
NULL,
0,
&ThreadID);
TName[1] = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)Reader,
NULL,
0,
&ThreadID);
WaitForMultipleObjects(2, TName, TRUE, INFINITE);
CloseHandle(TName);
return 0;
}
#include <windows.h>
void hello()
{
MessageBox(NULL, "Hello MinGW!", "INFO", MB_OK|MB_ICONINFORMATION);
}
typedef void (*VOIDFUNC)(LPVOID);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE,
LPSTR lpszCmdLine,
int nCmdShow)
{
VOIDFUNC pfun = (VOIDFUNC)hello;
pfun(NULL);
return 0;
}
#include <windows.h>
void hello(LPVOID lpParam)
{
MessageBox(NULL, "Hello MinGW!", "INFO", MB_OK|MB_ICONINFORMATION);
}
typedef void (*VOIDFUNC)();
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE,
LPSTR lpszCmdLine,
int nCmdShow)
{
VOIDFUNC pfun = (VOIDFUNC)hello;
pfun();
return 0;
}
std::cout<<"Read done\n"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include<process.h>
#include <iostream>
HANDLE hMutex, hWriteDone, hReadDone;
int num, state;
unsigned int __stdcall Writer(void*)
{
for(int x=10; x>=0; x--)
{
while (true)
{
if (WaitForSingleObject(hMutex, INFINITE) == WAIT_FAILED)
{
std::cout<<"In writing loop, no mutex!\n";
ExitThread(0);
}
if (state == 0)
{
ReleaseMutex(hMutex);
WaitForSingleObject(hReadDone, INFINITE);
continue;
}
break;
}
std::cout<<"Write done\n";
num= x;
state= 0;
ReleaseMutex(hMutex);
PulseEvent(hWriteDone);
}
return 0;
}
unsigned int __stdcall Reader(void*)
{
while(true)
{
if (WaitForSingleObject(hMutex, INFINITE) == WAIT_FAILED)
{
std::cout<<"In reader, no mutex!\n";
ExitThread(0);
}
if (state == 1)
{
ReleaseMutex(hMutex);
WaitForSingleObject(hWriteDone, INFINITE);
continue;
}
if (num == 0)
{
std::cout<<"End of data\n";
ReleaseMutex(hMutex);
ExitThread(0);
}
else {
std::cout<<"Read done\n";
state = 1;
ReleaseMutex(hMutex);
PulseEvent(hReadDone);
}
}
return 0;
}
int main()
{
HANDLE TName[2];
DWORD ThreadID;
state = 1;
hMutex = CreateMutex(NULL, FALSE, NULL);
hWriteDone = CreateEvent(NULL, TRUE, FALSE, NULL);
hReadDone = CreateEvent(NULL, TRUE, FALSE, NULL);
TName[0] = (HANDLE)_beginthreadex(NULL,
0,
Writer,
NULL,
0,
0);
TName[1] = (HANDLE)_beginthreadex(NULL,
0,
Reader,
NULL,
0,
0);
WaitForMultipleObjects(2, TName, TRUE, INFINITE);
CloseHandle(TName);
return 0;
}