簡體   English   中英

為什么makecontext / swapcontext無法與pthread_mutex_lock / pthread_mutex_unlock一起使用

[英]Why makecontext/swapcontext does not work with pthread_mutex_lock/pthread_mutex_unlock

我已經成功使用了makecontext / swapcontext來移動堆棧。 但是,當我嘗試將其與pthread_mutex_lockpthread_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM