Search code examples
cpthreadsucontextpthread-exit

Exit thread with return


I have a small problem, and I hope finding someone who can help. I am trying to develop a thread library in c using ucontext, and i have developped the basic functions for that. so now my problem is that I want to consider the case when the user finishes his function with return instead of my exit function.

Is it possible to kown if the thread context is finished and to get the return value when the user uses retutn in his function ??


Solution

  • Is it possible to kown if the thread context is finished and to get the return value when the user uses retutn in his function ??

    Yes: you provide your own function, and initialize the context with it. You pass a pointer to the user function, and argument (if any) into your function. The function would look something like:

    void *thread_top(void *(*user_fn)(void*), void *arg)
    {
      void *ret = (*user_fn)(arg);
      // Do whatever is desirable for "ret" and terminate the context here.
    }