UNIX Environment Programming Learning notes (27)-multithreaded Programming (ii): Controlling Thread properties

Source: Internet
Author: User

Lienhua34
2014-11-09

1 Thread Properties Overview

The main properties of POSIX threads include the scope property, the Detach property, the stack address, the stack size, and the priority. The struct pthread_attr_t is defined in the header file Pthread.h to record the properties of the thread.

The second parameter of the function pthread_create that creates the thread is attr a pointer to the pthread_attr_t struct, through which we can control the properties of the newly created thread. If the ATRR parameter is NULL, a new thread is created that creates a default property.

The pthread_attr_t struct is transparent to the application, meaning that the application does not need to care about the implementation fields of the individual attributes in the struct. The header file Pthread.h provides functions to manipulate these properties. The Pthread_attr_init function and the Pthread_attr_destroy function are used respectively for the initialization and destruction of thread properties pthread_attr_t structs.

#include <pthread.h>

int Pthread_attr_init (pthread_attr_t *attr);

int Pthread_attr_destroy (pthread_attr_t *attr);

Return value: Returns 0 if successful, otherwise returns the error number

Before calling the Pthread_create function, call the Pthread_attr_init function to initialize the default values for all properties of the thread that the pthread_attr_t struct supports for the operating system. After calling the Pthread_create function to create the thread, call the Pthread_attr_destroy function to destroy the pthread_attr_t struct, because some thread property objects might be allocated dynamic memory space.

The following is a brief description of several major threading properties and their operational functions.

1.1 Detach Properties

The Detach property, also known as the detach state, indicates whether the new thread is out of sync with other threads in the process. If set to Pthread_create_detached, the new thread is started in a detached state, and the new thread frees the resource itself when it exits. If set to Pthread_create_joinable, after the new thread exits, it needs to call Pthread_join to get the thread's exit status and free the resources that the thread occupies. The default value is pthread_create_joinable. The action function for this property is, for more information, refer to http://pubs.opengroup.org

#include <pthread.h>

int pthread_attr_getdetachstate (const pthread_attr_t *restrict attr, int *detachstate);

int pthread_attr_setdetachstate (pthread_attr_t *restrict attr, int detachstate);

Two function return value: Returns 0 if successful, otherwise returns the error number

1.2 Scheduling policy Properties

The scheduling strategies for new threads include Sched_other (normal, non-real-time), SCHED_RR (real-time, round-robin), and Sched_fifo (real-time, first-in, first-out), the default is Sched_other, and the latter two scheduling policies are only valid for super users. The action function for the scheduling policy property is, in detail, refer to http://pubs.opengroup.org

#include <pthread.h>

int Pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);

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

1.3 Priority attributes

This property is represented by the sched_priority integer variable in the struct SCHED_PARAM structure when implemented. This property 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. The maximum and minimum priority values supported by the system can be obtained with functions Sched_get_priority_max and sched_get_priority_min. Please refer to http://pubs.opengroup.org for detailed instructions.

#include <pthread.h>

int Pthread_attr_setschedparam (pthread_attr_t *attr,const struct sched_param *param);

int Pthread_attr_getschedparam (const pthread_attr_t *attr, struct sched_param *param);

1.4 Scope Property

The Scope property represents the range of competing CPUs between threads, that is, the valid range of thread priorities. There are two valid 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 for CPU time. Please refer to http://pubs.opengroup.org for detailed instructions.

#include <pthread.h>

int Pthread_attr_setscope (pthread_attr_t *attr, int contentionscope);

int Pthread_attr_getscope (const pthread_attr_t *attr, int *contentionscope);

2 Thread Separation Status example

To create a new thread that detaches state, we call the Pthread_attr_setdetachstate function to set the Detachstate property of the attr parameter of the Pthread_create function to Pthread_create_ DETACHED, or after the thread is created, call the Pthread_detach function to set a thread as detached state. The declaration of the Pthread_detach function is as follows

#include <pthread.h>

int Pthread_detach (pthread_t tid);

Return value: Returns 0 if successful, otherwise returns the error number

If the thread is in a detached state, the thread's underlying storage resource is immediately retracted when it terminates. For threads that are in a detached state, calling the Pthread_join function returns an error. Let's look at an example of a separation state,

#include <stdlib.h>#include<stdio.h>#include<string.h>#include<pthread.h>void*My_thread (void*Arg) {printf ("In new thread. \ n"); Sleep (1); printf ("Out New thread.\n"); return((void*)0);}intMain (void){  interr;  pthread_t Tid; void*Tret;  pthread_attr_t attr; Err= Pthread_attr_init (&attr); if(Err! =0) {printf ("pthread_attr init error:%s\n", Strerror (err)); Exit (-1); } Err= Pthread_attr_setdetachstate (&attr, pthread_create_detached); if(Err! =0) {printf ("pthread_attr_setdetachstate Error:%s\n", Strerror (err)); Exit (-1); } Err= Pthread_create (&tid, &attr, My_thread, NULL); if(Err! =0) {printf ("can ' t create thread:%s\n", Strerror (err)); Exit (-1); } Err= Pthread_join (tid, &Tret); if(Err! =0) {printf ("pthread_join Error:%s\n", Strerror (err)); Exit (-1); } printf ("new Thread return value:%d\n", (int) Tret); Pthread_attr_destroy (&attr); Exit (0);}

In the above program, the call pthread_attr_setdetachstate function is set to pthread_create_detached the Detachstate property of the attr parameter passed to the Pthread_create function. The Pthread_create function creates a new thread in the detached state and then calls the Pthread_join function on the new thread. Compile the program, generate and execute the executable file Pthread_detach_demo,

gcc -o pthread_detach_demo-pthread pthread_detach_demo.clienhua34:demo$. /Pthread_detach_demo in new thread.pthread_join error:invalid argument

From the above running results, we can see that for the disconnected state of the thread, call the Pthread_join function times wrong. If you comment out a line of code that calls pthread_attr_setdetachstate in the above program, and then reproduce the program, build and execute the executable file Pthread_detach_demo,

gcc -o pthread_detach_demo-pthread pthread_detach_demo.clienhua34:demo$. /Pthread_detach_demoin0

As you can see from the results above, for a non-detached state thread, the Pthread_join function can normally get the return value of that thread.

(done)

UNIX Environment Programming Learning notes (27)-multithreaded Programming (ii): Controlling Thread properties

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.