Skip to content

Instantly share code, notes, and snippets.

@vallahor
Created August 8, 2023 01:43
Show Gist options
  • Save vallahor/ced1cfadfea6da5edb57453d2a2f864d to your computer and use it in GitHub Desktop.
Save vallahor/ced1cfadfea6da5edb57453d2a2f864d to your computer and use it in GitHub Desktop.
Simple Thread Windows
#include <stdio.h.>
#include <windows.h>
#include <processthreadsapi.h>
DWORD WINAPI ThreadFn(PVOID param);
struct Inner {
const char* innerName;
int result;
void (*print)(const Inner*);
};
void PrintInner(const Inner* inner) {
printf("Name: %s Result: %d\n", inner->innerName, inner->result);
}
int main(void) {
printf("Main TID: (%lu)\n", GetCurrentThreadId());
Inner inner;
inner.innerName = "It's a inner BOI";
inner.print = PrintInner;
DWORD tid;
HANDLE thread = CreateThread(NULL, 0, ThreadFn, &inner, CREATE_SUSPENDED, &tid);
printf("Thread %lu was created in a suspended state\n", tid);
Sleep(500);
ResumeThread(thread);
WaitForSingleObject(thread, INFINITE);
DWORD exitCode;
if (!GetExitCodeThread(thread, &exitCode)) {
printf("GetExitCodeThread Error: %lu\n", GetLastError());
}
printf("Thread TID: (%lu) exited with code: (%lu)\n", tid, exitCode);
CloseHandle(thread);
inner.print(&inner);
return EXIT_SUCCESS;
}
DWORD WINAPI ThreadFn(PVOID param) {
Sleep(1000);
Inner* inner = (Inner*)param;
inner->result = 42;
inner->print(inner);
Sleep(1000);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment