Introduction to Thread Properties pthread_attr_t

Source: Internet
Author: User
Tags posix

Thread Properties pthread_attr_t Introduction This article edited from: http://hi.baidu.com/7828058/blog/item/256e16decd1a385e94ee3784.htmlhttp//www.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part1/  The threading attribute pthread_attr_t in POSIX threads mainly includes the scope property, the Detach property, the stack address, the stack size, and the priority. In Pthread_create, if the second parameter is set to NULL, the default property configuration is used. The main attributes of pthread_attr_t are as follows: __detachstate, indicates whether the new thread is out of sync with other threads in the process, and if set to pthread_create_detached the new thread cannot be used Pthread_join () To synchronize and release the resources that were consumed at the time of exit. The default is pthread_create_joinable state. This property can also be set with Pthread_detach () after the thread is created and run, and once set to the Pthread_create_detach state (whether set at creation or runtime), it cannot be restored to Pthread_create_ joinable status. __schedpolicy, represents the new thread scheduling strategy, mainly including sched_other (normal, non-real-time), SCHED_RR (real-time, rotation method) and Sched_fifo (real-time, first-in, first-out) three, the default is Sched_other, The latter two scheduling policies are only valid for super users. The runtime can be changed using over Pthread_setschedparam (). __schedparam, a struct SCHED_PARAM structure that currently has only one sched_priority integer variable representing the thread's run priority. This parameter is valid only if the scheduling policy is real-time (that is, SCHED_RR or SCHED_FIFO) and can be changed at run time through the Pthread_setschedparam () function, which defaults to 0. __inheritsched, there are two values to choose from: Pthread_explicit_sched and pthread_inherit_sched, which indicates that the new thread uses the explicitly specified schedule policy and schedule parameters (that is, values in attr). The latter represents the value that inherits the caller thread. The default is pthread_explicit_sched. __scope, which indicates the range of competing CPUs between threads, that is, the valid range of thread priorities. The POSIX standard defines two values: Pthread_scope_system and pthread_scope_process, which represent competing CPU time with all threads in the system, which indicates that only the threads in the same process compete with the CPU. At present Linuxthreads only realizes the Pthread_scoA value of Pe_system. To set these properties, POSIX defines a series of property-setting functions, including Pthread_attr_init (), Pthread_attr_destroy (), and pthread_attr_getxxx associated with each property/The pthread_attr_setxxx function. Before you set the thread property pthread_attr_t, you typically call Pthread_attr_init to initialize, which later calls the corresponding property-setting function. The main functions are as follows:1, Pthread_attr_init function: Initializes a thread property variable. Header file:<pthread.h>function Prototypes:intPthread_attr_init (pthread_attr_t*attr); function passed in value: attr: Thread property. function return Value: Success:0failure:-12, Pthread_attr_setscope function: Sets the thread __scope property. The Scope property represents the range of competing CPUs between threads, that is, the valid range of thread priorities. The POSIX standard defines two values: Pthread_scope_system and pthread_scope_process, which represent competing CPU time with all threads in the system, which indicates that only the threads in the same process compete with the CPU. The default is pthread_scope_process. Currently, Linuxthreads only implements the Pthread_scope_system value. Header file:<pthread.h>function Prototypes:intPthread_attr_setscope (pthread_attr_t* attr,intscope); function passed in value: attr: Thread property. Scope:pthread_scope_system, which represents competing with all threads in the system for CPU time, pthread_scope_process, represents only competing with threads in the same process for CPU function return Worth: with 1. 3, Pthread_attr_setdetachstate function: Sets the thread Detachstate property. This indicates whether the new thread is out of sync with the other threads in the process, and if set to pthread_create_detached, the new thread cannot be synchronized with Pthread_join () and frees the resource itself when exiting. The default is pthread_create_joinable state. This property can also be set with Pthread_detach () after the thread is created and run, and once set to the Pthread_create_detach state (whether set at creation or runtime), it cannot be restored to Pthread_create_ joinable status. Header file:<phread.h>function Prototypes:intPthread_attr_setdetachstate (pthread_attr_t* attr,intdetachstate); function passed in value: attr: Thread property. Detachstate:pthread_create_detached, can not use Pthread_join () to synchronize, and at the exit on its own release of the occupied Resources Pthread_create_joinable, PTHR Ead_join () to the synchronous function return is worth: the same as 1. 4, Pthread_attr_setschedparam function: Sets the thread Schedparam property, which is the priority of the call. Header file:<pthread.h>function Prototypes:intPthread_attr_setschedparam (pthread_attr_t* attr,structsched_param*param); function passed in value: attr: Thread property. PARAM: Thread priority. A struct SCHED_PARAM structure that currently has only one sched_priority integer variable that represents the thread's run priority. This parameter is only valid if the scheduling policy is real-time (that is, SCHED_RR or SCHED_FIFO) and can be changed at run time by the Pthread_setschedparam () function, which defaults to the 0 function return value: the same as 1. 5, Pthread_attr_getschedparam function: Get thread priority. Header file:<pthread.h>function Prototypes:intPthread_attr_getschedparam (pthread_attr_t* attr,structsched_param*param); function passed in value: attr: Thread attribute; param: thread priority; function return value: Same as 1. Example 1: #include<stdlib.h>#include<stdio.h>#include<errno.h>#include<pthread.h>Static voidPthread_func_1 (void); Static voidPthread_func_2 (void); intMain (intargcChar**argv) {pthread_t pt_1=0; pthread_t pt_2=0; pthread_attr_t ATRR= {0}; intRET =0; /*Initialize property thread Properties*/Pthread_attr_init (&attr); Pthread_attr_setscope (&attr, Pthread_scope_system); Pthread_attr_setdetachstate (&attr, pthread_create_detached); RET= Pthread_create (&pt_1, &attr, Pthread_func_1, NULL); if(Ret! =0) {perror ("pthread_1_create"); } ret= Pthread_create (&pt_2, NULL, pthread_func_2, NULL); if(Ret! =0) {perror ("pthread_2_create");       } pthread_join (Pt_2, NULL); return 0; }     Static voidPthread_func_1 (void)   {     inti =0;  for(; I <6; i++) {printf ("This is pthread_1.\n"); if(i = =2) {Pthread_exit (0); }     }       return; }     Static voidPthread_func_2 (void)   {     inti =0;  for(; I <3; i + +) {printf ("This is pthread_2.\n"); }       return; From the above example, you can get the result that the thread Cheng function will automatically release the resource at the end of the line, and the line Cheng will have to wait until Pthread_join to release the system resources. End!
http://blog.csdn.net/hudashi/article/details/7709413

Introduction to Thread Properties pthread_attr_t

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.