Linux Multithreading Practice (iii) Basic property settings API for threads

Source: Internet
Author: User
Tags terminates

The POSIX line libraries defines the thread Property object pthread_attr_t, which encapsulates the thread properties that the creator of the thread can access and modify. The main properties include the following:

1. Scope (SCOPE)

2. Stack size (stack size)

3. Stack Address

4. Priorities (priority)

5. State of separation (detached)

6. Scheduling policies and parameters (scheduling policy and parameters)


A thread Property object can be associated with one thread or multiple line threads. When a thread Property object is used, it is configured for thread and thread group behavior. All threads that use a Property object will have all the properties defined by the Property object. Although they share property objects, they maintain their own separate thread IDs and registers.

Initialize/Destroy Thread properties

int Pthread_attr_init (pthread_attr_t *attr);  int Pthread_attr_destroy (pthread_attr_t *attr);  
Thread Detach Properties
int pthread_attr_getdetachstate (const pthread_attr_t *attr, int *detachstate);  int Pthread_attr_setdetachstate (pthread_attr_t *attr, int detachstate);  
Thread Stack size
int Pthread_attr_setstacksize (pthread_attr_t *attr, size_t stacksize);  int Pthread_attr_getstacksize (pthread_attr_t *attr, size_t *stacksize);

In general, this value is set to 0, using the system default setting of the thread stack size, which may cause problems with program portability

Thread stacks overflow protected area size
int Pthread_attr_setguardsize (pthread_attr_t *attr, size_t guardsize);  int Pthread_attr_getguardsize (pthread_attr_t *attr, size_t *guardsize);  

Guardsize means that if we use the thread stack beyond the set size, the system will also use some extended memory to prevent stack overflow. This part of the extended memory size is guardsize. However, if you modify the stack allocation location, then this option fails, the effect is equivalent to set Guardsize to 0.

each thread has its own stack, If these stacks are connected, If you access more than your own stack, you might be able to modify the stack to another thread. If we set the Guardsize, the thread stack will open up Guarszie-sized memory, and SIGSEGV signal will be triggered when the memory is accessed.

Thread Competition Range (process-wide competition or system-wide competition)
int Pthread_attr_getscope (const pthread_attr_t *attr,int *contentionscope);  int Pthread_attr_setscope (pthread_attr_t *attr, int contentionscope);  

Threads can compete for resources within two competing domains:

1. Process scope: With other threads in the same process

2. System scope: With all threads in the system

Scope properties describe which threads a particular thread will compete with resources for. A thread with a system domain will be dispatched with all threads in the system domain competing for processor resources at the priority level.

Thread scheduling Policy

int Pthread_attr_getschedpolicy (const pthread_attr_t *attr, int *policy);  int Pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);  

The scheduling policy and priority of the process are the main thread, in other words, the scheduling policy and priority of the set process only affect the scheduling policy and priority of the main thread, without changing the scheduling policy and priority of the peer (note that this sentence is not exactly correct). Each peer can have its own scheduling policy and priority that is independent of the primary thread.

In a Linux system, there are three scheduling strategies for the process: Sched_fifo ( FIFO scheduling strategy), SCHED_RR (time slice rotation scheduling algorithm), and Sched_other (when threads start running, Until it is preempted or until the thread is blocked or stopped ), the thread is no exception, and there are three of these policies.

scheduling policy for thread inheritance
int pthread_attr_getinheritsched (const pthread_attr_t *attr, int *inheritsched);  int pthread_attr_setinheritsched (pthread_attr_t *attr, int inheritsched);  

In the Pthread library, a function is provided to set the scheduling properties of the thread being created: whether to inherit the Schedule property (scheduling policy and priority) from the creator thread, or to set the Schedule property from the Property object. The function is:

int pthread_attr_setinheritsched (pthread_attr_t *   attr, int    inherit) where the value of inherit is one of the following values: Enum{pthread_ inherit_sched,///thread scheduling properties inherit from creator thread pthread_explicit_sched//Thread schedule property set to attr property};

When a new thread is created, when the function is called to set the parameter to pthread_inherit_sched, when the priority of the process is modified, the process inherits that priority and has not changed its priority The thread will also change the priority (that is, the reason for the correct part of the phrase just now).


Thread scheduling parameters (in fact we generally only care about one parameter: Thread priority, default is 0)
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);  Sched_param structure  struct Sched_param {      int sched_priority;     /* Scheduling priority */  };  
concurrency level for threads
int pthread_setconcurrency (int new_level);  

Description: The concurrency level is only valid in the N:M threading model, setting the concurrency level, giving the kernel a hint: the core thread that provides a given number of levels to map user threads is efficient (just a hint), default is 0, and the kernel is concurrency by default;

Let's write an example to apply:

/** View Thread default properties **/void Printthreadattr () {pthread_attr_t attr;        Pthread_attr_init (&AMP;ATTR);      int detachstate;      Pthread_attr_getdetachstate (&attr, &detachstate);               cout << "detach-state:" << (detachstate = = pthread_create_joinable?)        "Pthread_create_joinable": "pthread_create_detached") << Endl;      size_t size;      Pthread_attr_getstacksize (&attr, &size);        cout << "stack-size:" << size << Endl;      Pthread_attr_getguardsize (&attr, &size);        cout << "guard-size:" << size << Endl;      int scope;      Pthread_attr_getscope (&attr, &scope);               cout << "Scope:" << (scope = = Pthread_scope_system?)        "Pthread_scope_system": "Pthread_scope_process") << Endl;      int policy;      Pthread_attr_getschedpolicy (&attr, &policy);      cout << "policy:"; Switch (pOlicy) {case sched_fifo:cout << "Sched_fifo";      Break          Case Sched_rr:cout << "SCHED_RR";      Break          Case Sched_other:cout << "Sched_other";      Break      Default:break;        } cout << Endl;      int inheritsched;      Pthread_attr_getinheritsched (&attr, &inheritsched);               cout << "inheritsched:" << (inheritsched = = pthread_inherit_sched?)        "pthread_inherit_sched": "pthread_inherit_sched") << Endl;      struct Sched_param param;      Pthread_attr_getschedparam (&AMP;ATTR,¶M);      cout << "Scheduling priority:" << param.sched_priority << Endl;      cout << "Concurrency:" << pthread_getconcurrency () << Endl;  Pthread_attr_destroy (&AMP;ATTR);   }

Description

Binding properties:

Linux uses a "one-on-one" threading mechanism, which is a user thread that corresponds to a kernel thread . A binding attribute means that a user thread is pinned to a kernel thread, because the CPU time slice is scheduled to target a kernel thread (that is, a lightweight process) , so a thread with a bound attribute can guarantee that there will always be a kernel thread corresponding to it when needed. The corresponding non-binding attribute means that the relationship between the user thread and the kernel thread is not always fixed, but is controlled by the system.  

Detach attribute:

The detach attribute is used to determine how a thread terminates itself. In a non-detached situation, when a thread ends, the system resources it consumes are not released, that is, there is no real termination. only when the Pthread_join () function returns does the created thread release the system resources that it occupies . In the case of detached attributes, the system resources that it occupies are released immediately at the end of a thread. one thing to note here is that if you set the Detach property of a thread, and the thread runs very fast, it is likely to terminate before the Pthread_create () function returns, and it may pass the thread number and system resources to other threads for use after it terminates .



Linux Multithreading Practice (iii) Basic property settings API for threads

Related Article

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.