pthread_setpthreadoption_np()--Set Pthread Run-Time Option Data


  Syntax:
 #include <pthread.h>
 int pthread_setpthreadoption_np(pthread_option_np_t *optionData);  
  Service Program Name: QP0WTCBH

  Default Public Authority: *USE

  Threadsafe: Yes

  Signal Safe: Yes

The pthread_setpthreadoption_np() function sets option data in the pthread run-time for the process.

Input data is specified uniquely based on the specified optionData. See the table below for details about input and output. The option field in the optionData parameter is always required; other fields may be input, output, or ignored, based on the specific option used.

For all options, every reserved field in the structure represented by optionData must be binary zero or the EINVAL error is returned. Unless otherwise noted for an option, the target field in the option parameter is always ignored, and the contents of the optionData structure is not changed by the pthread_setpthreadoption_np() function.

The currently supported options, the data they represent, and the valid operations are as follows:

option field of the option parameter Description
PTHREAD_OPTION_POOL_NP When a thread terminates and is detached or joined to, certain data structures from the pthreads run-time are maintained in a pool for possible reuse by future threads. This improves performance for creating threads. Typically, an application should not be concerned with this storage pool. Use this option to set the current maximum size of the allowed storage pool. The optionValue field of the optionData parameter is used to set the current maximum number of thread structures that will be allowed in the storage pool. By default, the optionValue field must be a valid integer greater than or equal to zero, or the EINVAL error is returned. The default maximum size of the storage reuse pool contains enough room for 512 thread structures.
PTHREAD_OPTION_POOL_CURRENT_NP If the option field of the optionData parameter is set to this option, the EINVAL error is returned.
PTHREAD_OPTION_THREAD_CAPABLE_NP If the option field of the optionData parameter is set to this option, the EINVAL error is returned.


Authorities and Locks

None.


Parameters

option
(Input/Output) Address of the variable containing option information and to contain output option information

Return Value

0
pthread_getpthreadoption_np() was successful.

value
pthread_getpthreadoption_np() was not successful. value is set to indicate the error condition.

Error Conditions

If pthread_getpthreadoption_np() was not successful, the error condition returned usually indicates one of the following errors. Under some conditions, the value returned could indicate an error other than those listed here.

[EINVAL]

The value specified for the argument is not correct.


Related Information


Example

See Code disclaimer information for information pertaining to code examples.

#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"

#define NUMTHREADS   5

void *threadfunc(void *parm)
{
  printf("Inside the thread\n");
  return NULL;
}

void showCurrentSizeOfPool(void) {
  int                   rc;
  pthread_option_np_t   opt;

  memset(&opt, 0, sizeof(opt));
  opt.option = PTHREAD_OPTION_POOL_CURRENT_NP;
  rc = pthread_getpthreadoption_np(&opt);
  checkResults("pthread_getpthreadoption_np()\n", rc);

  printf("Current number of thread structures in pool is %d\n",
         opt.optionValue);
  return;
}

int main(int argc, char **argv)
{
  pthread_t             thread[NUMTHREADS];
  int                   rc=0;
  int                   i=0;
  pthread_option_np_t   opt;

  printf("Enter Testcase - %s\n", argv[0]);

  printf("Create threads and prime the storage pool\n");
  for (i=0; i<NUMTHREADS; ++i) {
    rc = pthread_create(&thread[i], NULL, threadfunc, NULL);
    checkResults("pthread_create(NULL)\n", rc);
  }

  printf("Joining all threads at once so thread n does not reuse\n"
         "thread n-1's data structures\n");
  for (i=0; i<NUMTHREADS; ++i) {
    rc = pthread_join(thread[i], NULL);
    checkResults("pthread_join()\n", rc);
  }

  showCurrentSizeOfPool();

  /* Set the maximum size of the storage pool to 0. I.e. No reuse of  */
  /* pthread structures                                               */
  printf("Set the max size of the storage pool to 0\n");
  memset(&opt, 0, sizeof(opt));
  opt.option      = PTHREAD_OPTION_POOL_NP;
  opt.optionValue = 0;
  rc = pthread_setpthreadoption_np(&opt);
  checkResults("pthread_setpthreadoption_np()\n", rc);

  printf("Create some more threads. Each thread structure will come\n"
         "from the storage pool if it exists, but based on the max size of 0,\n"
         "the thread structure will not be allowed to be reused\n");
  for (i=0; i<NUMTHREADS; ++i) {
    rc = pthread_create(&thread[i], NULL, threadfunc, NULL);
    checkResults("pthread_create(NULL)\n", rc);

    showCurrentSizeOfPool();

    rc = pthread_join(thread[i], NULL);
    checkResults("pthread_join()\n", rc);
  }



  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPSEtopT
Create threads and prime the storage pool
Joining all threads at once so thread n does not reuse
thread n-1's data structures
Inside the thread
Inside the thread
Inside the thread
Inside the thread
Inside the thread
Current number of thread structures in pool is 5

Set the max size of the storage pool to 0
Create some more threads. Each thread structure will come
from the storage pool if it exists, but based on the max size of 0,
the thread structure will not be allowed to be reused
Current number of thread structures in pool is 4
Inside the thread
Current number of thread structures in pool is 3
Inside the thread
Current number of thread structures in pool is 2
Inside the thread
Current number of thread structures in pool is 1
Inside the thread
Current number of thread structures in pool is 0
Inside the thread
Main completed



API introduced: V4R3
Top | Pthread APIs | APIs by category