Skip to content

Instantly share code, notes, and snippets.

@X547
Created July 31, 2020 01:52
Show Gist options
  • Save X547/8561111d89657ed39df7f2d79cdea97e to your computer and use it in GitHub Desktop.
Save X547/8561111d89657ed39df7f2d79cdea97e to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <stdio.h>
enum {
threadCnt = 3,
};
HINSTANCE gInst;
HANDLE gThreads[threadCnt];
LRESULT CALLBACK WndHandler(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg) {
case WM_CREATE: {
char str[256];
sprintf(str, "Thread: %d", GetCurrentThreadId());
SetWindowText(wnd, str);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(wnd, msg, wp, lp);
}
return 0;
}
DWORD WINAPI ThreadEntry(void *arg)
{
MSG msg;
CreateWindow("Window", "Minimal Application", WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 320, 240, NULL, NULL, gInst, NULL);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInst, LPSTR cmdLine, int showCmd)
{
gInst = inst;
DWORD threadId;
WNDCLASS wc = {0, WndHandler, 0, 0, inst, LoadIcon(NULL, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_BTNFACE+1), 0, "Window"};
RegisterClass(&wc);
for (int i = 0; i < threadCnt; i++) {
gThreads[i] = CreateThread(NULL, 0, ThreadEntry, NULL, 0, &threadId);
}
WaitForMultipleObjects(threadCnt, gThreads, TRUE, INFINITE);
for (int i = 0; i < threadCnt; i++) {
CloseHandle(gThreads[i]); gThreads[i] = 0;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment