Why makecontext does not work with pthreads

From the makecontext manual ...

Due to limitations in the current implementation of pthread, makecontext should not be used in programs that are associated with the pthread (3) library (regardless of whether threads are used).

Now my question is: why does it not work and what are the alternative methods. Actually, I'm interested in switching stacks in a user-level stream at some points, but I see that when I do swapcontext, I get segmentation errors from time to time. What should I do?

I want to achieve something like this ...

void thread_func(void * thread_args) { a(); b(); getcontext/makecontext/swapcontext to call c(); d(); .... } 

So, in this case, I want to use a separate stack when executing the c function.

+7
source share
1 answer

Due to limitations in the current implementation of pthread, makecontext should not be used in programs that are associated with the pthread (3) library

This section of the manual applies to LinuxThreads, which was used to round the %esp value until the current thread descriptor is found. This (obviously) will not create a valid thread descriptor if you are executing an alternate stack.

LinuxThreads is no longer used by any Linux distribution in the last 5+ years, and {get,make,swap}context works fine with NPTL threads.

EDIT: Actually, I only see "due to restrictions" in NetBSD documents , not Linux Documents .

when I execute swapcontext, I get segmentation errors from time to time

You have an error that appears as a segmentation error now. You did not provide enough information to guess where this error might be.

+9
source

All Articles