GoldFish Engine
Quite simple and lightweight game engine
Loading...
Searching...
No Matches
gf_thread.c
1#define GF_EXPOSE_THREAD
2
3#include <gf_pre.h>
4
5/* External library */
6#include <pthread.h>
7
8/* Interface */
9#include <gf_thread.h>
10
11/* Engine */
12
13/* Standard */
14#include <stdlib.h>
15
16void* gf_wrap_thread(void* arg) {
18 ctx->func(ctx->data);
19 return NULL;
20}
21
22gf_thread_t* gf_thread_create(void (*func)(void*), void* userdata) {
23 gf_thread_t* thread = malloc(sizeof(*thread));
24 thread->context.func = func;
25 thread->context.data = userdata;
26 if(pthread_create(&thread->thread, NULL, gf_wrap_thread, &thread->context) == 0) return thread;
27 free(thread);
28 return NULL;
29}
30
32 void* value;
33 pthread_join(thread->thread, &value);
34}
35
36void gf_thread_destroy(gf_thread_t* thread) { free(thread); }
Required headers before anything.
Thread interface.
void gf_thread_join(gf_thread_t *thread)
Join thread.
Definition gf_thread.c:31
gf_thread_t * gf_thread_create(void(*func)(void *), void *userdata)
Create thread.
Definition gf_thread.c:22
void gf_thread_destroy(gf_thread_t *thread)
Destroy thread.
Definition gf_thread.c:36
Thread context.
Definition thread.h:42
void * data
Data to be passed to thread.
Definition thread.h:42
void(* func)(void *)
Function to be called for thread.
Definition thread.h:42
Platform-dependent thread.
Definition thread.h:61