2013-01-17 96 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; 
}