Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Created November 22, 2015 20:26
Show Gist options
  • Select an option

  • Save neuro-sys/fd1540c781a0a07b8973 to your computer and use it in GitHub Desktop.

Select an option

Save neuro-sys/fd1540c781a0a07b8973 to your computer and use it in GitHub Desktop.

Revisions

  1. neuro-sys created this gist Nov 22, 2015.
    33 changes: 33 additions & 0 deletions thread.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #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__
    }
    9 changes: 9 additions & 0 deletions thread.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    #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_