Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么makecontext/swapcontext不能与pthread_mutex_lock/pthread_mutex_unlock一起使用_C_Linux_Memory_Gcc_Stack - Fatal编程技术网

为什么makecontext/swapcontext不能与pthread_mutex_lock/pthread_mutex_unlock一起使用

为什么makecontext/swapcontext不能与pthread_mutex_lock/pthread_mutex_unlock一起使用,c,linux,memory,gcc,stack,C,Linux,Memory,Gcc,Stack,我已经成功地使用了makecontext/swapcontext来移动堆栈。但是,当我尝试将其与pthread\u mutex\u lock或pthread\u mutex\u unlock一起使用时,我总是收到分段错误。你知道为什么吗。代码如下所示 编辑 现在我读了swapcontext手册 由于当前pthread实现中的限制,makecontext不应用于链接pthread(3)库的程序中(无论是否使用线程) 有什么解决办法吗 static const unsigned int SWAP_S

我已经成功地使用了makecontext/swapcontext来移动堆栈。但是,当我尝试将其与pthread\u mutex\u lockpthread\u mutex\u unlock一起使用时,我总是收到分段错误。你知道为什么吗。代码如下所示

编辑 现在我读了swapcontext手册

由于当前pthread实现中的限制,makecontext不应用于链接pthread(3)库的程序中(无论是否使用线程)

有什么解决办法吗

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
将参数作为type
int
传递。如果您使用的是64位Linux,那么指针将是64位,而
int
将是32位,奇怪的事情将开始发生。

好的,如果是这样的话,我不能为pthread\u mutex\u lock创建一个包装函数,并传递两个整数(组合起来表示互斥体的地址),然后将它们打包到一个长变量中,以便将互斥体的地址从包装器函数中传递给pthread_mutex_lock函数?你觉得怎么样?@metallicparter这可能有用,但有点复杂。:)可能不是适合您的解决方案,但您可以使用Solaris。两年前,我在一个64位程序(Solaris 10,x64)中成功地使用了上下文函数和pthread。