Domanda

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.

È stato utile?

Soluzione

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;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top