-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMsgQueue.cpp
More file actions
60 lines (49 loc) · 1.5 KB
/
Copy pathMsgQueue.cpp
File metadata and controls
60 lines (49 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "stdafx.h"
#include "Windows.h"
#include "list"
#include "iostream"
using namespace std;
// Define constants for command messages
#define TM_CMD_EXIT WM_USER + 1
#define TM_CMD_DATA WM_USER + 2
// Define function for the worker thread
DWORD WINAPI QueueWorkerProc(LPVOID pParam)
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (msg.message == TM_CMD_EXIT) // Exit the loop if the message is to quit
break;
char *pszData = (char *)msg.lParam;
printf(" => queued data : %s\n", pszData);
delete[] pszData; // Free up the memory used for the message data
}
printf("......QueueWorkerProc Thread Exit!!!\n");
return 0;
}
void _tmain()
{
cout << "======= Start MsgQueue Test ========" << endl;
// Create a worker thread to process messages
DWORD dwThreadId;
HANDLE hThread = CreateThread(NULL, 0, QueueWorkerProc, NULL, 0, &dwThreadId);
// Get input from the user and send it to the worker thread
char szIn[1024];
while (true)
{
cin >> szIn;
if (_stricmp(szIn, "quit") == 0) // Send exit command if the user enters "quit"
{
PostThreadMessage(dwThreadId, TM_CMD_EXIT, 0, 0);
break;
}
// Allocate memory for the message data and send it to the worker thread
char *pszData = new char[strlen(szIn) + 1];
strcpy(pszData, szIn);
PostThreadMessage(dwThreadId, TM_CMD_DATA, 0, (LPARAM)pszData);
}
// Wait for the worker thread to finish and close its handle
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
cout << "======= End MsgQueue Test ==========" << endl;
}