I've seen a few questions on user thread libraries, but none that seem to answer my question. I am able to create threads, run them, cancel them, and exit from them. What I cannot do for some reason is get a thread to return data.
When I initialize my thread library, I set my exit thread context as follows:
getcontext(&threadEnd);
threadEnd.uc_stack.ss_sp = (char *)malloc(SIGSTKSZ);
threadEnd.uc_stack.ss_size = SIGSTKSZ;
makecontext(&threadEnd, (void *) thread_exit, 1, &ReturnValue);
I create a thread and assign it as follows:
thread->myContext.uc_stack.ss_sp = (char *) malloc(SIGSTKSZ);
thread->myContext.uc_stack.ss_size = SIGSTKSZ;
thread->myContext.uc_link = &threadEnd;
When the function returns and thread_exit() is called:
void thread_exit(void* retval){
int* test;
if (retval != NULL)
{
test = (int*) retval;
printf("Returned value: %i\n", *test);
fflush(stdout);
}
The printout is always "Returned value: 0"
The called function is returning a pointer to an integer.
What am I doing wrong?