POSIX thread properties for Linux

Source: Internet
Author: User
Tags bit set posix

The function that creates the POSIX thread is

int Const pthread_attr_t *attr,                          void * (*start_routine) (voidvoid *arg) ;

The 1th parameter is a line Cheng (similar to a file descriptor), and the 3rd argument is a thread-start function (input void*, return void*, because pointers to any struct/base data type can be considered void*, and void* It is generally possible to explicitly cast a pointer to a corresponding type or even an integer, which is a common technique that does not support pure C programming, and the 4th parameter is the argument passed to the thread start function.

The 2nd parameter is generally set to null, at which time the default thread property () is used. However, there is a need to set up, even if it is not tlpi on the thread properties, but only in the 29.8 Thread Properties section gives an example. So I learned more about its usage by combining manuals and viewing pthread.h.

The

First TLPI the setup example for the on-line properties (the full version), which uses the detach (detach) attribute, which is automatically detached after the thread is created without calling the Pthread_detach function

THREAD_ATTRIB.C: Thread Properties Use example//compile: gcc thread_attrib.c-pthread#include <stdio.h> #include <stdlib.h># Include <string.h> #include <unistd.h> #include <pthread.h>//check the error code errnum, If not 0 then output MSG and error code related information static inline void checkerrnum (int errnum, const char* msg) {if (Errnum! = 0) {fprintf (Stder        R, "%s Error:%s\n", MSG, Strerror (errnum));    Exit (1);    }}//thread start function, treat input as String type static inline void* threadfunc (void* Arg) {printf ("%s\n", (char*) arg); return NULL;}    int main () {int s;//error code//Initialize thread attribute pthread_attr_t attr;    s = pthread_attr_init (&attr);    Checkerrnum (S, "pthread_attr_init");    Sets the thread detach state, this property guarantees the thread s = pthread_attr_setdetachstate (&attr, pthread_create_detached);    Checkerrnum (S, "pthread_attr_setdetachstate");    Use the Attr property to create a thread pthread_t tid;    s = pthread_create (&tid, &attr, ThreadFunc, (void*) "Hello world!");    Checkerrnum (S, "pthread_create"); Destroy thread Properties S = Pthread_attr_destroy (&attr);    Checkerrnum (S, "Pthread_attr_destroy");  Sleep (1); The main thread sleeps, which guarantees that the THREADFUNC function of the creation thread can execute return 0;}

One thing to be aware of is that using Pthread_attr_init to initialize thread properties, you need to use pthread_attr_destory destroy after you have finished using (that is, incoming pthread_create), freeing up the related resources.

It is also possible to find that the threading properties are not set up in a way that is as simple as setting properties in plain C.

For example, the system calls Open's 3rd parameter can be used S_IRUSR | S_IWUSR | S_irgrp | S_iroth such 4 macros defined as integers are also or operate to represent rw-r--r--read/write/execute permissions.

The setting of thread properties has a separate function for each property, and each function has its own options, such as the pthread_attr_setdetachstate here, the key word is detachstate (detached state). The 2nd parameter here selects Pthread_create_detached, which separates the threads immediately after they are created.

Then look at the definition of the thread property type

Union pthread_attr_t                                                                               {                           char  __size[__sizeof_pthread_attr_t];    Long int __align;};     #ifndef __have_pthread_attr_ttypedef Union pthread_attr_t pthread_attr_t;                                                       # define __have_pthread_attr_t  1#endif   

The 3rd parameter of the previous open is equivalent to 0b0001, 0b0010, 0b0100 This type of value or, can be seen as a bit (bit) array, each bit of the array can only be 0 or 1, and each thread property may have multiple values, so pthread_attr_ T is a char array with a value of 256, which means that each thread property theoretically supports up to 256 optional values. The array size _sizeof_pthrea_attr_t is determined by the system type and wordsize.

Here's a look at the header file of the glib library pthread.h to see what the threading attributes are. The prefix for the set function of a thread property is Pthread_attr_set, so look for a function prefixed with this. The list of functions I will find is organized as follows

1, the thread separation State (Detach), the default is joinable, that is, other threads can get the return result ("Connection") of the thread through the Pthread_join function, detached is separated when the thread is created and cannot be connected by another thread.

/* Detach state.   */ enum {  pthread_create_joinable,#define pthread_create_joinable    pthread_create_joinable   pthread_create_detached #define pthread_create_detached    pthread_create_detached};
/*Get Detach State attribute. */extern intPthread_attr_getdetachstate (Constpthread_attr_t *__attr,int*__detachstate) __throw __nonnull (1,2));/*Set Detach State attribute. */extern intPthread_attr_setdetachstate (pthread_attr_t *__attr,int__detachstate) __throw __nonnull (1));

2, the size of the extended memory to avoid stack overflow after the end of the thread stack, if set to 0, it means that this mechanism is invalid

/*Get the size of the guard area created for stack overflow protection. */extern intPthread_attr_getguardsize (Constpthread_attr_t *__attr, size_t*__guardsize) __throw __nonnull (1,2));/*Set the size of the guard area created for stack overflow protection. */extern intPthread_attr_setguardsize (pthread_attr_t *__attr, size_t __guardsize) __throw __nonnull ((1));

3, scheduling parameters, the type is struct struct sched_param, the 1th parameter is priority, the other parameters are unknown. The portable approach is to set the thread priority.

/* The official definition.   */ struct Sched_param                                                                               {         int  __sched_priority;};    
/*Return in *param the scheduling parameters of *attr. */extern intPthread_attr_getschedparam (Constpthread_attr_t *__restrict __attr,structSched_param *__restrict __param) __throw __nonnull (1,2));/*Set scheduling parameters (priority, etc) in *attr according to PARAM. */extern intPthread_attr_setschedparam (pthread_attr_t *__restrict __attr,Const structSched_param *__restrict __param) __throw __nonnull (1,2));

4, scheduling policy, Sched_other is the default value (time-sharing scheduling strategy), Sched_fifo and SCHED_RR are real scheduling strategy, the former is first to first service, the latter is the time slice rotation.

Refer to the article for specific details

linux-Process scheduling algorithm

The comparison of thread scheduling strategy Sched_rr (rotation method) and Sched_fifo (FIFO)

/*Return in *policy the scheduling POLICY of *attr. */extern intPthread_attr_getschedpolicy (Constpthread_attr_t *__restrict __attr,int*__restrict __policy) __throw __nonnull (1,2));/*Set scheduling policy in *attr according to policy. */extern intPthread_attr_setschedpolicy (pthread_attr_t *__attr,int__policy) __throw __nonnull (1));
// bits/sched.h /* scheduling algorithms.   */ #define Sched_other     0                                                                        #define sched_fifo      1#define SCHED_RR        2

5, the Scheduler's inheritance mode, the default is inherit, inherit from the parent thread scheduling priority, only set to explicit, their own "explicit" set of scheduling policies, priority will only work.

/* Scheduler inheritance.   */ enum {  pthread_inherit_sched,#define pthread_inherit_sched   pthread_inherit_sched   pthread_explicit_sched#define pthread_explicit_sched  pthread_explicit_sched};
/*Return in *inherit the scheduling inheritance mode of *attr. */extern intPthread_attr_getinheritsched (Constpthread_attr_t *__restrict __attr,int*__restrict __inherit) __throw __nonnull (1,2));/*Set scheduling inheritance mode in *attr according to INHERIT. */extern intPthread_attr_setinheritsched (pthread_attr_t *__attr,int__inherit) __throw __nonnull (1));

6, the scope of the dispatch content, system on behalf of the thread and all the system's threads compete for resources, process on behalf of the thread and other threads in the process to compete for resources.

/* Scope handling.   */ enum      {           pthread_scope_system,#define pthread_scope_system pthread_scope_system                                                 pthread_scope_process #define Pthread_scope_process   pthread_scope_process};
/*Return in *scope the scheduling contention SCOPE of *attr. */extern intPthread_attr_getscope (Constpthread_attr_t *__restrict __attr,int*__restrict __scope) __throw __nonnull (1,2));/*Set scheduling contention scope in *attr according to scope. */extern intPthread_attr_setscope (pthread_attr_t *__attr,int__scope) __throw __nonnull (1));

7, the thread's stack address

/*Return the previously set address for the stack. */extern intPTHREAD_ATTR_GETSTACKADDR (Constpthread_attr_t *__restrict __attr,void**__restrict __stackaddr) __throw __nonnull (1,2)) __attribute_deprecated__;/*Set The starting address of the stack of the thread to be created. Depending on whether the stack grows up or down the value must either is higher or lower than all the address in the MEM  Ory block.  The minimal size of the block must be pthread_stack_min. */extern intPTHREAD_ATTR_SETSTACKADDR (pthread_attr_t *__attr,void*__stackaddr) __throw __nonnull (1)) __attribute_deprecated__;

8, the current use of the minimum stack address size, when set this value, the size of the stack can not be less than pthread_stack_min, can not be greater than the system limit (with command ulimit-s view)

/*Return the currently used minimal stack size. */extern intPthread_attr_getstacksize (Constpthread_attr_t *__restrict __attr, size_t*__restrict __stacksize) __throw __nonnull (1,2));/*Add information about the minimum stack size needed for the thread to be started.  This size must never is less than pthread_stack_min and must also not exceed the system limits. */extern intPthread_attr_setstacksize (pthread_attr_t *__attr, size_t __stacksize) __throw __nonnull ((1));

9, the address and size of the line stacks (need to define macro __use_xopen2k)

#ifdef __use_xopen2k/*Return the previously set address for the stack. */extern intPthread_attr_getstack (Constpthread_attr_t *__restrict __attr,void**__restrict __stackaddr, size_t*__restrict __stacksize) __throw __nonnull (1,2,3));/*The following interfaces is intended to replace the last. They require setting the address as well as the size since only setting the address would make the implementation on so  Me architectures impossible. */extern intPthread_attr_setstack (pthread_attr_t *__attr,void*__stackaddr, size_t __stacksize) __throw __nonnull ((1));#endif

10, the affinity of the thread, (need to define macro __USE_GNU)

Reference article: Thread affinity research for Linux under Pthread

#ifdef __USE_GNU/*Thread created with attribute ATTR'll be limited to run is on the processors represented in Cpuset. */extern intPTHREAD_ATTR_SETAFFINITY_NP (pthread_attr_t *__attr, size_t __cpusetsize,Constcpu_set_t *__cpuset) __throw __nonnull (1,3));/*Get bit set in Cpuset representing the processors threads created with ATTR can run on. */extern intPTHREAD_ATTR_GETAFFINITY_NP (Constpthread_attr_t *__attr, size_t __cpusetsize, cpu_set_t*__cpuset) __throw __nonnull (1,3));#endif

POSIX thread properties for Linux

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.