Reputation: 3332
I am trying to make a user level thread library like pthreads. I am using the makecontext()/swapcontext() to do the switching between the threads and while making a context for the thread, I am passing uctx_main to the uc_link field in the context.
context.uc_stack.ss_sp = thread->c_stack;
context.uc_stack.ss_size = sizeof(thread->c_stack);
context.uc_link = &uctx_main;
However, when my thread terminates, it gives me a segmentation fault. My main program is still running.
Also, can someone please explain what is uctx_main? Is it the default context for the main function? Do I need to have a uctx_main for every thread that I create? Sorry for asking such a silly question, but I did not find anything on googling it.
Upvotes: 1
Views: 1369
Reputation: 36412
I assume from your reference to uctx_main
that you are following the example in the Linux man page for makecontext()
.
In that example, uctx_main
is the context for the main thread, which is set when calling setcontext()
to switch to uctx_func2
.
uctx_main
isn't special in any way, other than it having been set from the main thread (and being set as the successor to uctx_func1
in the example).
In general, every thread you create needs to have a context.
Upvotes: 1