简体   繁体   中英

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.

On Linux I used pthread_getattr_np , on Solaris I can use thr_stksegment .

Help me please find a way to know threads stack size please on C.

I found a solution for this problem in webkit sources . 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. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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