Code snippet
#include "Windows.h" #include using namespace std; HANDLE thread1; HANDLE thread2; HANDLE mEvent; char x; // Test thread routine. DWORD WINAPI F1(LPVOID lpParameter) { bool *bRunning = reinterpret_cast while(true) { if(x=='q') { *bRunning = false; break; } WaitForSingleObject (mEvent,INFINITE);//nonsignaled = wait cout <<"Start F1\n"; cout <<"Working F1 \n"; cout <<"any service from me if so press any key [q to quit]\n"; cin>>x; cout <<"Thank you\nEnd F1\n\n"; SetEvent(mEvent); //signaled = finished work } return 0; } // Test thread routine. DWORD WINAPI F2(LPVOID lpParameter) { bool *bRunning = reinterpret_cast while(true) { if(x=='q') { *bRunning = false; break; } WaitForSingleObject (mEvent,INFINITE);//nonsignaled = wait cout <<"Start F2\n"; cout <<"Working F2 \n"; cout <<"any service from me if so press any key [q to quit]\n"; cin>>x; cout <<"Thank you\nEnd F2\n\n"; SetEvent(mEvent); //signaled = finished work } return 0; } void main() { bool bRunning = true; mEvent = CreateEvent(NULL, FALSE, TRUE, NULL); thread1 = CreateThread(NULL, 0, F1, static_cast thread2 = CreateThread(NULL, 0, F2, static_cast while(bRunning) { } CloseHandle(thread1); CloseHandle(thread2); CloseHandle(mEvent); } |
MFC code snippet
#include "stdafx.h" #include "CEvent.h" #include "conio.h" #include "afxmt.h" using namespace std; HANDLE thread1; HANDLE thread2; CEvent mCEvent(TRUE,FALSE); char x; // Test thread routine. DWORD WINAPI F1(LPVOID lpParameter) { bool *bRunning = reinterpret_cast while(true) { if(x=='q') { *bRunning = false; break; } WaitForSingleObject (mCEvent,INFINITE);//nonsignaled = wait cout <<"Start F1\n"; cout <<"Working F1 \n"; cout <<"any service from me if so press any key [q to quit]\n"; cin>>x; cout <<"Thank you\nEnd F1\n\n"; SetEvent(mCEvent); //signaled = finished work } return 0; } // Test thread routine. DWORD WINAPI F2(LPVOID lpParameter) { bool *bRunning = reinterpret_cast while(true) { if(x=='q') { *bRunning = false; break; } WaitForSingleObject (mCEvent,INFINITE);//nonsignaled = wait cout <<"Start F2\n"; cout <<"Working F2 \n"; cout <<"any service from me if so press any key [q to quit]\n"; cin>>x; cout <<"Thank you\nEnd F2\n\n"; SetEvent(mCEvent); //signaled = finished work } return 0; } int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed\n")); nRetCode = 1; } else { bool bRunning = true; thread1 = CreateThread(NULL, 0, F1, static_cast thread2 = CreateThread(NULL, 0, F2, static_cast while(bRunning) { } CloseHandle(thread1); CloseHandle(thread2); } return nRetCode; } |
Download source without using MFC
Download source using MFC
No comments:
Post a Comment