![](/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.