39 lines
870 B
C
39 lines
870 B
C
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#else
|
|
#include <pthread.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
#include "time.h"
|
|
|
|
void start_thread(void** thread_handle, void* (*task)(void*)) {
|
|
#ifdef _WIN32
|
|
HANDLE thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)task, NULL, 0, NULL);
|
|
if (thread == NULL) {
|
|
fprintf(stderr, "Failed to create thread\n");
|
|
}
|
|
*thread_handle = thread;
|
|
#else
|
|
pthread_t thread;
|
|
if (pthread_create(&thread, NULL, task, NULL) != 0) {
|
|
fprintf(stderr, "Failed to create thread\n");
|
|
}
|
|
pthread_detach(thread);
|
|
*thread_handle = (void*)thread;
|
|
#endif
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
void kill_thread(void* thread_handle) {
|
|
HANDLE thread = (HANDLE)thread_handle;
|
|
TerminateThread(thread, 0);
|
|
}
|
|
#else
|
|
void kill_thread(void* thread_handle) {
|
|
pthread_t thread = (pthread_t)thread_handle;
|
|
pthread_cancel(thread);
|
|
}
|
|
#endif
|