POSIX thread (ii)

Source: Internet
Author: User

One. Thread Properties
(1)Initialize and Destroy properties
int Pthread_attr_init (pthread_attr_t *attr);
int Pthread_attr_destroy (pthread_attr_t *attr);

(2)get and set detach properties
int pthread_attr_setdetachstate (pthread_attr_t *attr, int detachstate);
int Pthread_attr_getdetachstate (pthread_attr_t *attr, int *detachstate);
(3)get and set stack size
int pthread_attr_setstacksize (pthread_attr_t *attr, size_t stacksize);
int Pthread_attr_getstacksize (pthread_attr_t *attr, size_t *stacksize);

(4)get and set stack 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);
(5)gets and sets the thread in the competition range
int Pthread_attr_setscope (pthread_attr_t *attr, int scope);
int Pthread_attr_getscope (pthread_attr_t *attr, int *scope);
(6)get and set scheduling policy
int Pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
int Pthread_attr_getschedpolicy (pthread_attr_t *attr, int *policy);
(7)get and set the inheritance of the scheduling policy
int pthread_attr_setinheritsched (pthread_attr_t *attr,
int inheritsched);
int pthread_attr_getinheritsched (pthread_attr_t *attr,
int *inheritsched);
(8)get and set schedule parameters
int Pthread_attr_setschedparam (pthread_attr_t *attr,
const struct Sched_param *param);
int Pthread_attr_getschedparam (pthread_attr_t *attr,
struct Sched_param *param);
(9)get and set the concurrency level
int pthread_setconcurrency (int new_level);
int pthread_getconcurrency (void);

only valid in N: Threading model, setting the concurrency level, giving the kernel a hint: the core thread that provides the number of given levels is efficient for mapping user threads.

#include <unistd.h> #include <sys/types.h> #include <pthread.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #define ERR_EXIT (m) do {p                 Error (m);         Exit (Exit_failure);    } while (0) int main (void) {pthread_attr_t attr;    Pthread_attr_init (&attr);//Gets and sets the detach attribute int state;    Pthread_attr_getdetachstate (&attr, &state);    if (state = = pthread_create_joinable) printf ("detachstate:pthread_create_joinable\n");    else if (state = = pthread_create_detached) printf ("detachstate:pthread_create_detached");//Get stack size size_t sizes;    Pthread_attr_getstacksize (&attr, &size);    printf ("stacksize:%d\n", size);//Get and set stack overflow protected area size pthread_attr_getguardsize (&attr, &size);    printf ("guardsize:%d\n", size);//Gets and sets the thread in the competition range int scope;    Pthread_attr_getscope (&attr, &scope); if (scope = = pthread_scope_process) printf ("Scope:Pthread_scope_process\n ");    if (scope = = Pthread_scope_system) printf ("scope:pthread_scope_system\n");//Get and set scheduling policy int policy;    Pthread_attr_getschedpolicy (&attr, &policy);    if (policy = = Sched_fifo) printf ("policy:sched_fifo\n");    else if (policy = = SCHED_RR) printf ("policy:sched_rr\n");    else if (policy = = Sched_other) printf ("policy:sched_other\n");//Get and set the inherited scheduling policy int inheritsched;    Pthread_attr_getinheritsched (&attr, &inheritsched); if (inheritsched = = pthread_inherit_sched) printf ("inheritsched:pthread_inherit_sched\n"); Inherit the calling thread property else if (inheritsched = = pthread_explicit_sched) printf ("inheritsched:pthread_explicit_sched\n");    You need to set//get and set scheduling parameters struct Sched_param param;    Pthread_attr_getschedparam (&AMP;ATTR,¶M);    printf ("sched_priority:%d\n", param.sched_priority);    Pthread_attr_destroy (&AMP;ATTR); return 0;}


two. Thread-specific data
(1) in a single-threaded program, "Global variables" are often used to realize the sharing of data among multiple functions.
(2) in a multithreaded environment, because the data space is shared, global variables are common to all threads.
(3) But sometimes it is necessary in application design to provide thread-private global variables that are valid only in one thread, but can be accessed across multiple functions.

(4) POSIX line libraries solves this problem by maintaining a certain data structure called (TSD).


The meaning of the existence of thread-specific data

now there is a global variable that all threads can use to change its value. And if each thread wants to have it alone, then it needs to use thread storage. On the surface it looks like this is a global variable that all threads can use, and its value is stored separately in each thread. This is the meaning of thread storage .


1. Create a variable of type pthread_key_t type.

2. Call Pthread_key_create () to create the variable. The function has two parameters, the first parameter is the pthread_key_t variable declared above, and the second parameter is a cleanup function, which is called when the thread is freed by threads to be stored. The function pointer can be set to NULL, so the system will invoke the default cleanup function.

3. You can call Pthread_setspcific () when you need to store special values in a thread. The function has two parameters, the first is the pthread_key_t variable declared earlier, and the second is the void* variable, so you can store any type of value.

4. If you need to remove the stored value, call Pthread_getspecific (). The function's argument is the pthread_key_t variable mentioned earlier, which returns a value of type void *.

When Pthread_key_create is called, the pthread_key_t value of the thread-specific data (TSD) that is visible to all threads is called, although there is only one pthread_key_t, but the specific data for each thread is a separate memory space, The destructor function is executed when the thread exits.

Related functions:

int pthread_key_create (pthread_key_t *key, Void (*destructor) (void*));
int Pthread_key_delete (pthread_key_t key);
void *pthread_getspecific (pthread_key_t key);
int pthread_setspecific (pthread_key_t key, const void *value);
int pthread_once (pthread_once_t *once_control,
void (*init_routine) (void));
pthread_once_t Once_control = Pthread_once_init;

Tsd.c


#include <unistd.h> #include <sys/types.h> #include <pthread.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #define ERR_EXIT (m) do {p                 Error (m);         Exit (Exit_failure);    } while (0) typedef struct tsd{pthread_t tid; char *str;} tsd_t;pthread_key_t key_tsd;pthread_once_t Once_control = pthread_once_init;void destroy_routine (void *value) {printf    ("Destory ... \ n"); Free (value);}    void Once_routine (void) {pthread_key_create (&AMP;KEY_TSD, destroy_routine); printf ("Key init ... \ n");}     void *thread_routine (void *arg) {//pthread_key_create (&key_tsd,destroy_routine); Pthread_once (&once_control, once_routine);    Ensure that Once_routine will only be called once//using malloc to allocate memory tsd_t *value = (tsd_t *) malloc (sizeof (tsd_t)); Value->tid = Pthread_self (); Fill id value->str = (char *) arg;//the string passed by//pthread_setspecific will bind the specific data of each thread with pthread_key_t Pthread_setspeciFIC (KEY_TSD, value);    printf ("%s setspecific ptr=%p\n", (char *) arg, value);    Gets a specific value of "value = pthread_getspecific" (KEY_TSD);    printf ("Tid=0x%x str=%s ptr=%p\n", (int) Value->tid, value->str, value);    Sleep (2);    Gets a specific value of "value = pthread_getspecific" (KEY_TSD);    printf ("Tid=0x%x str=%s ptr=%p\n", (int) Value->tid, value->str, value); return NULL;} int main (void) {//pthread_key_t value pthread_key_create (&AMP;KEY_TSD, which produces a thread-specific data (TSD) that all threads are visible after calling Pthread_key_create.    Destroy_routine);    pthread_t Tid1;    pthread_t Tid2;    Create two threads pthread_create (&AMP;TID1, NULL, Thread_routine, "Thread1");    Pthread_create (&tid2, NULL, Thread_routine, "thread2");    Pthread_join (TID1, NULL);    Pthread_join (Tid2, NULL);    Pthread_key_delete (KEY_TSD); return 0;}

Remove the comments from the 64,42,43 line, and only one of the first incoming threads is called Once_routine


POSIX thread (ii)

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.