简体   繁体   English

获取 HP-UX 11 中当前线程的堆栈大小

[英]Get stack size of current thread in HP-UX 11

I'm trying to get stack size of current thread in my application running on HP-UX 11.31.我正在尝试获取在 HP-UX 11.31 上运行的应用程序中当前线程的堆栈大小。

On Linux I used pthread_getattr_np , on Solaris I can use thr_stksegment .在 Linux 上我使用pthread_getattr_np ,在 Solaris 上我可以使用thr_stksegment

Help me please find a way to know threads stack size please on C.请帮助我找到一种方法来了解 C 上的线程堆栈大小。

I found a solution for this problem in webkit sources .我在webkit 资源中找到了解决这个问题的方法。 But this solution not suitable if high performance of application is very important to you, because creating and suspending threads are expensive operations.但是如果应用程序的高性能对您来说非常重要,则此解决方案不适合,因为创建和挂起线程是昂贵的操作。

I replace base word with size , because in webkit sources we are looking for stack base, not size.我用size替换base word ,因为在 webkit 源中,我们正在寻找堆栈基数,而不是大小。 Example code:示例代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM