Created
November 22, 2015 20:26
-
-
Save neuro-sys/fd1540c781a0a07b8973 to your computer and use it in GitHub Desktop.
POSIX/Windows Threads Wrapper
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 "thread.h" | |
#include <stdio.h> | |
#ifdef __WIN32__ | |
#include <windows.h> | |
#else | |
#include <pthread.h> | |
#include <stdlib.h> | |
#endif // __WIN32__ | |
int thread_create(thread_t * thread, void *(*start_routine)(void *), void *arg) | |
{ | |
#ifdef __WIN32__ | |
void * addr = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) start_routine, arg, 0, NULL); | |
*thread = addr; | |
return 0; | |
#else | |
*thread = malloc(sizeof (pthread_t)); | |
return pthread_create(*thread, NULL, start_routine, arg); | |
#endif // __WIN32__ | |
} | |
int thread_kill(thread_t * thread) | |
{ | |
#ifdef __WIN32__ | |
return TerminateThread(*thread, 9); | |
#else | |
pthread_kill(*((pthread_t *) *thread), 9); | |
free(*thread); | |
return 0; | |
#endif // __WIN32__ | |
} |
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
#ifndef __THREAD_H_ | |
#define __THREAD_H_ | |
typedef void * thread_t ; | |
int thread_create(thread_t * thread, void *(*start_routine)(void *), void *arg); | |
int thread_kill(thread_t * thread); | |
#endif // __THREAD_H_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment