As a part of an assignment, I am trying to create a user level thread library like pthreads.
For handling the context switching between the threads, I am using ‘swapcontext’ function. Before using it I have to make a context using ‘makecontext’ function. ‘makecontext’ expects a function pointer with return type void and argument type (void).
Whereas, the thread function has to be of the type void* thread_func (void*)
Is there a way to do a typecasting? Or is there some other way to do context switching at the user level?
It is illegal to invoke a function with an incompatible prototype by casting the address of the function to a different prototype and invoking it through the resulting pointer:
What you can do is pass to
makecontextyour own callback which will callthread_funcand ignore its return value. A small function that only serves to call another function is sometimes called a trampoline.For bonus points, you can modify the trampoline to store the
void *value returned by the callback function on the stack and have your equivalent ofpthread_join()retrieve it.