Created
August 8, 2023 01:43
-
-
Save vallahor/ced1cfadfea6da5edb57453d2a2f864d to your computer and use it in GitHub Desktop.
Simple Thread Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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