#include "thread/Thread.h" #include #include #include #include typedef struct ThreadPthreadImpl { pthread_t thread; } ThreadPthreadImpl; Thread* createThread(ThreadFn threadFn, void* arg) { ThreadPthreadImpl* pthreadImpl = NULL; Thread* thread = calloc(1, sizeof(Thread)); thread->impl = calloc(1, sizeof(ThreadPthreadImpl)); pthreadImpl = (ThreadPthreadImpl*)thread->impl; if (pthread_create(&(pthreadImpl->thread), NULL, threadFn, arg) != 0) { printf("Thread: Failed to create thread.\n"); } return thread; } void joinThread(Thread* thread) { ThreadPthreadImpl* pthreadImpl = (ThreadPthreadImpl*)thread->impl; pthread_join(pthreadImpl->thread, NULL); } void killThread(Thread* thread) { ThreadPthreadImpl* pthreadImpl = (ThreadPthreadImpl*)thread->impl; pthread_kill(pthreadImpl->thread, SIGUSR1); // thread needs to handle or SIG_IGN this } void freeThread(Thread** thread) { free((ThreadPthreadImpl*)((*thread)->impl)); free(*thread); *thread = NULL; }