pthread_attr_setweight_np() — Set weight of thread attribute object

Standards

Standards / Extensions C or C++ Dependencies
z/OS® UNIX both

POSIX(ON)

Format

#define _OPEN_THREADS
#define _OPEN_SYS
#include <pthread.h>

int pthread_attr_setweight_np(pthread_attr_t *attr, int threadweight);

General description

Alter the current weight of the thread setting of the thread attribute object, attr.

threadweight can be set to one of the following two symbols for the weight of the thread, as defined in the pthread.h header file.
__LIGHT_WEIGHT
Not supported.
__MEDIUM_WEIGHT
Each thread runs on a task. Upon exiting, if another thread is not queued to run, the task waits for some other thread to issue a pthread_create(), and the thread then runs on that task. The thread is assumed to cleanup all resources it used.
__HEAVY_WEIGHT
The task is attached on pthread_create() and terminates upon a pthread_exit(). Full MVS™ EOT resource cleanup occurs when exiting. When this exits, the associated MVS task can no longer request threads to process.

You can use a thread attribute object to manage the characteristics of threads in your application. It defines the set of values to be used for the thread during its creation. By establishing a thread attribute object, you can create many threads with the same set of characteristics, without defining those characteristics for each thread. You can define more than one thread attribute object.

Returned value

If successful, pthread_attr_setweight_np() returns 0.

If unsuccessful, pthread_attr_setweight_np() returns -1.

There are no documented errno values. Use perror() or strerror() to determine the cause of the error.

Example

CELEBP13
⁄* CELEBP13 *⁄
#define _OPEN_THREADS
#define _OPEN_SYS      ⁄*  Needed to identify __MEDIUM_WEIGHT  *⁄
#include <stdio.h>
#include <pthread.h>

void *thread1(void *arg)
{
   printf("hello from the thread\n");
   pthread_exit((void *)0);
}

int main()
{
   int            rc, stat;
   pthread_attr_t attr;
   pthread_t      thid;

   rc = pthread_attr_init(&attr);
   if (rc == -1) {
      perror("error in pthread_attr_init");
      exit(1);
   }

   rc = pthread_attr_setweight_np(&attr, __MEDIUM_WEIGHT);
   if (rc == -1) {
      perror("error in pthread_attr_setweight_np");
      exit(2);
   }

   rc = pthread_create(&thid, &attr, thread1, NULL);
   if (rc == -1) {
      perror("error in pthread_create");
      exit(3);
   }

   rc = pthread_join(thid, (void *)&stat);
   exit(0);
}

Related information