2013-01-17 98 views
0

我正試圖在運行在HP-UX 11.31上的應用程序中獲取當前線程的堆棧大小。獲取HP-UX中當前線程的堆棧大小11

在Linux上,我使用pthread_getattr_np,在Solaris上我可以使用thr_stksegment

請幫我找到一個方法來知道線程的堆棧大小,請在C.

+0

您可能會發現一些問題的答案,特別是一個關係到** ** libunwind從[這個問題](http://stackoverflow.com/questions/975108/stack-unwinding- on-hp-ux-and-linux)有幫助。 – WhozCraig

回答

1

我發現這個問題的解決方案webkit sources。但是,如果應用程序的高性能對您非常重要,則此解決方案不適合,因爲創建和掛起線程是昂貴的操作。

我用size替換base字,因爲在webkit源代碼中,我們正在尋找堆棧基礎,而不是大小。示例代碼:

struct hpux_get_stack_size_data 
{ 
    pthread_t thread; 
    _pthread_stack_info info; 
}; 

static void *hpux_get_stack_size_internal(void *d) 
{ 
    hpux_get_stack_base_data *data = static_cast<hpux_get_stack_size_data *>(d); 

    // _pthread_stack_info_np requires the target thread to be suspended 
    // in order to get information about it 
    pthread_suspend(data->thread); 

    // _pthread_stack_info_np returns an errno code in case of failure 
    // or zero on success 
    if (_pthread_stack_info_np(data->thread, &data->info)) { 
    // failed 
    return 0; 
    } 

    pthread_continue(data->thread); 

    return data; 
} 

static void *hpux_get_stack_size() 
{ 
    hpux_get_stack_size_data data; 
    data.thread = pthread_self(); 
    // We cannot get the stack information for the current thread 
    // So we start a new thread to get that information and return it to us 
    pthread_t other; 
    pthread_create(&other, 0, hpux_get_stack_size_internal, &data); 
    void *result; 
    pthread_join(other, &result); 
    if (result) 
    return data.info.stk_stack_size; 

    return 0; 
}