DungeonCrawl
Loading...
Searching...
No Matches
thread_handler.c
Go to the documentation of this file.
1
5#include "thread_handler.h"
6
7#include <stdlib.h>
8
9typedef struct {
10 void (*func)(void);
12
13#ifdef _WIN32
14 #include <windows.h>
15
21DWORD WINAPI thread_wrapper(LPVOID arg) {
22 thread_func_wrapper_t* wrapper_arg = (thread_func_wrapper_t*) arg;
23 wrapper_arg->func();
24 free(wrapper_arg);
25 return 0;
26}
27
28void start_simple_thread(void (*thread_func)(void)) {
29 thread_func_wrapper_t* arg = malloc(sizeof(thread_func_wrapper_t));
30 if (!arg) return;
31 arg->func = thread_func;
32
33 HANDLE thread = CreateThread(NULL, 0, thread_wrapper, arg, 0, NULL);
34 if (thread) {
35 CloseHandle(thread);// detach the thread
36 } else {
37 free(arg);
38 }
39}
40
41#else
42 #include <pthread.h>
43
49void* thread_wrapper(void* arg) {
50 thread_func_wrapper_t* wrapper_arg = (thread_func_wrapper_t*) arg;
51 wrapper_arg->func();
52 free(wrapper_arg);
53 return NULL;
54}
55
56void start_simple_thread(void (*thread_func)(void)) {
57 pthread_t thread;
58 thread_func_wrapper_t* arg = malloc(sizeof(thread_func_wrapper_t));
59 if (!arg) return;// Fehlerbehandlung
60 arg->func = thread_func;
61
62 if (pthread_create(&thread, NULL, thread_wrapper, arg) == 0) {
63 pthread_detach(thread);// Detach den Thread
64 } else {
65 free(arg);// Fehlerbehandlung
66 }
67}
68#endif
void * thread_wrapper(void *arg)
A wrapper function arround a thread for multi platform thread implementation.
void start_simple_thread(void(*thread_func)(void))
Starts a new thread with the given function.
Exposes functions for the thread_handler.