I am currently working on a project where we have a C thread implementation for UNIX systems using pthreads. Now we want to be able to run this entire project on Windows, as well as translate all threads for WIN32. Now I am faced with a problem for which I could not come up with a decent solution.
I have a function thrd_create():
static inline int thrd_create(thrd_t *thr, thrd_start_t func, void *arg) {
Args* args = malloc(sizeof(Args));
args->arg = arg;
args->function = func;
*thr = CreateThread(NULL, 0, wrapper_function, (LPVOID) args, 0, NULL);
if (!*thr) {
free (args);
return thrd_error;
}
return thrd_success;
}
This function should create a new thread, and the user provides a start function. For convenience, I would like to leave the implementation that calls thrd_create () intact, if possible. For this reason, I created wrapper_function:
static inline DWORD wrapper_function(LPVOID arg) {
Args * args;
args = (Args*) arg;
DWORD res = args->function(args->arg);
return res;
}
: DWORD ? , pthread, void, . ?
Args :
struct Args {
void (*function)(void * aArg);
void* arg;
};
typedef struct Args Args;