Skip to content

Instantly share code, notes, and snippets.

@viperscape viperscape/thread.h
Last active Mar 6, 2018

Embed
What would you like to do?
cross os threading example
void* thread (void* (callback)(void* data), void* data);
void join (void* p);
#ifdef _WIN32
#include <windows.h>
void* thread (void* (callback)(void* data), void* data) {
HANDLE p = CreateThread(NULL, 0, callback, data, 0, NULL);
return p;
}
void join (void* p) {
if (p) {
WaitForSingleObject(p, INFINITE);
CloseHandle((HANDLE) p);
}
}
#else
#include <pthread.h>
#include <unistd.h>
void* thread (void* (callback)(void* data), void* data) {
pthread_t p;
pthread_create(&p, NULL, callback, data);
return (void*)p;
}
void join (void* p) {
pthread_join((pthread_t) p, NULL);
}
#endif
#include <stdio.h>
#include "thread.h"
void* hello_world(void* data) {
printf("hello %s\n", data);
}
int main () {
char* world = "world";
void* server = thread(hello_world, (void*) world);
join(server);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.