![](/img/trans.png)
[英]What is the behaviour of calling pthread_mutex_lock and pthread_mutex_unlock with NULL?
[英]Why makecontext/swapcontext does not work with pthread_mutex_lock/pthread_mutex_unlock
我已经成功使用了makecontext / swapcontext来移动堆栈。 但是,当我尝试将其与pthread_mutex_lock或pthread_mutex_unlock结合使用时 ,总是会遇到分段错误。 任何想法,为什么会这样。 代码如下所示。
现在我从swapcontext手册中读到,
由于当前pthread实现的限制,在链接到pthread(3)库的程序中(无论是否使用线程)都不应使用makecontext。
有解决方法吗?
static const unsigned int SWAP_STACK_SIZE = 8192;
// These are globally defined variables.
// Since each thread will have its own stack, they are defined as arrays.
static ucontext_t uctx_main[8], uctx_func[8];
static char func_stack[8][SWAP_STACK_SIZE];
// tid is thread ID here, values are 0, 1, 2, 3, etc...
if (getcontext(&uctx_func[tid]) == -1)
handle_error("getcontext");
uctx_func[tid].uc_stack.ss_sp = func_stack[tid];
uctx_func[tid].uc_stack.ss_size = SWAP_STACK_SIZE;
uctx_func[tid].uc_link = &uctx_main[tid];
makecontext(&uctx_func[tid], (void(*)())pthread_mutex_unlock, 1, &mutex);
if (swapcontext(&uctx_main[tid], &uctx_func[tid]) == -1)
handle_error("swapcontext");
手册页指出, makecontext
将参数传递为int
类型。 如果您使用的是64位Linux,则指针将是64位,而int
将是32位,并且奇怪的事情将开始发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.