#include <stdio.h>
#include <stdlib.h>
-#include <pthread.h>
#include <unistd.h>
#include <assert.h>
+#ifdef _WIN32
+#include <windows.h>
+#include <errno.h>
+#define ALLOC_THREADS(size) HANDLE *threads = malloc(sizeof(HANDLE) * times)
+#define spawn_thread(thread, attr, start_routine, arg) thread = CreateThread(attr, 0, start_routine, arg, 0, NULL)
+#define join_thread(thread, retval) WaitForSingleObject(thread, INFINITE)
+#else
+#include <pthread.h>
+#define ALLOC_THREADS(size) pthread_t *threads = malloc(sizeof(pthread_t) * times)
+#define spawn_thread(thread, attr, start_routine, arg) pthread_create(&thread, attr, start_routine, arg)
+#define join_thread(thread, retval) pthread_join(thread, retval)
+#endif
+#define FREE_THREADS free(threads)
+
#include <ykpers.h>
void *start_thread(void *arg)
{
int times = 5;
int i;
- pthread_t *threads = malloc(sizeof(pthread_t) * times);
+ ALLOC_THREADS(times);
for(i = 0; i < times; i++) {
- pthread_create(&threads[i], NULL, start_thread, NULL);
- pthread_join(threads[i], NULL);
+ spawn_thread(threads[i], NULL, start_thread, NULL);
+ join_thread(threads[i], NULL);
}
- free(threads);
+ FREE_THREADS;
}
int main(void)