1

我正在尝试获取在 HP-UX 11.31 上运行的应用程序中当前线程的堆栈大小。

在我使用的 Linuxpthread_getattr_np上,在 Solaris 上我可以使用thr_stksegment

请帮助我,请在 C 上找到一种了解线程堆栈大小的方法。

4

1 回答 1

2

我在webkit 源代码中找到了解决此问题的方法。但如果应用程序的高性能对您非常重要,则此解决方案不适合,因为创建和挂起线程是昂贵的操作。

我将baseword替换为size,因为在 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;
}
于 2013-01-30T19:41:49.297 回答