UNIX also has the thread attribute concept. Allow the creation thread process to access or change the attributes of the thread.
The thread attributes have the following aspects:
Scope determines the priority range of the thread.
Pthread_scope_process Process
Pthread_scope_system System
The deteached state separation thread does not retain any State information at the end of the process. It releases all resources it occupies and cannot be synchronized using join.
Ptheard_create_detached separation status
Ptheard_create_joinable default status
The priority can be set to change the priority of a thread.
Sched_get_priority_max Max
Minimum sched_get_priority_min
Scheduling Policies can be used to set and change the Scheduling Policies of threads.
Sched_fifo
Sched_rr time slice Rotation
Thread stack size pthead_attr_setstacksize [/getstacksize]
Thread stack address pthead_attr_setstackaddr [/getstackaddr]
Inherited attributes:
The possible values of inheritance are pthread_inherit_sched (indicating that the new generation will inherit the Scheduling Policies and parameters of the created thread) and pthread_explicit_sched (indicating that the scheduling policies and parameters explicitly set in the schedpolicy and schedparam attributes are used ).
Use the scheduling information explicitly set in the schedpolicy and schedparam attributes. The inheritsched attribute must be set to pthread_explicit_sched.
Thread attribute functions:
int pthread_attr_init(pthread_attr_t *attr);int pthread_attr_destroy(pthread_attr_t *attr); int pthread_attr_getdetachstate(const pthread_attr_t * attr,int *detachstate);int pthread_attr_setdetachstate(pthread_attr_t *attr,int detachstate); int pthread_attr_getinheritsched(const pthread_attr_t *attr,int *inheritsched);int pthread_attr_setinheritsched(pthread_attr_t *attr,int inheritsched); int pthread_attr_getschedpolicy(const pthread_attr_t *attr,int *policy);int pthread_attr_setschedpolicy(pthread_attr_t *attr,int policy);int pthread_attr_getschedparam(const pthread_attr_t *attr,struct sched_param *param);int pthread_attr_setschedparam(pthread_attr_t *attr,const struct sched_param *param);int sched_get_priority_max(int policy);int sched_get_priority_min(int policy); int pthread_attr_setscope(pthread_attr_t *attr,int scope);int pthread_attr_getscope(const pthread_attr_t *attr,int *scope);int pthread_attr_getstacksize(const pthread_attr_t *restrict attr,size_t *restrict stacksize);int pthread_attr_setstacksize(pthread_attr_t *attr ,size_t *stacksize);int pthread_attr_getstackaddr(const pthread_attr_t *attr,void **stackaddf);int pthread_attr_setstackaddr(pthread_attr_t *attr,void *stackaddr); int pthread_attr_getguardsize(const pthread_attr_t *restrict attr,size_t *restrict guardsize);int pthread_attr_setguardsize(pthread_attr_t *attr ,size_t *guardsize);
#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <pthread.h>void *thread_function(void *arg);char message[] = "Hello World";int thread_finished = 0;int main() { int res; pthread_t a_thread; void *thread_result; pthread_attr_t thread_attr; res = pthread_attr_init(&thread_attr); if (res != 0) { perror("Attribute creation failed"); exit(EXIT_FAILURE); } res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); if (res != 0) { perror("Setting detached attribute failed"); exit(EXIT_FAILURE); } res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message); if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } (void)pthread_attr_destroy(&thread_attr); while(!thread_finished) { printf("Waiting for thread to say it's finished...\n"); sleep(1); } printf("Other thread finished, bye!\n"); exit(EXIT_SUCCESS);}void *thread_function(void *arg) { printf("thread_function is running. Argument was %s\n", (char *)arg); sleep(4); printf("Second thread setting finished flag, and exiting now\n"); thread_finished = 1; pthread_exit(NULL);}