[英]Get stack size of current thread in HP-UX 11
我正在嘗試獲取在 HP-UX 11.31 上運行的應用程序中當前線程的堆棧大小。
在 Linux 上我使用pthread_getattr_np
,在 Solaris 上我可以使用thr_stksegment
。
請幫助我找到一種方法來了解 C 上的線程堆棧大小。
我在webkit 資源中找到了解決這個問題的方法。 但是如果應用程序的高性能對您來說非常重要,則此解決方案不適合,因為創建和掛起線程是昂貴的操作。
我用size
替換base
word ,因為在 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.