Linux thread proprietary data

Source: Internet
Author: User

1. Thread data

In a single-threaded program, there are two basic types of data: global variables and local variables. However, in multi-threaded programs, there is a third data type: thread data (TSD:
Thread-specific
Data ). It is similar to a global variable. In a thread, each function can call it like a global variable, but it is invisible to other threads outside the thread. The necessity of such data is obvious.
. For example, the common variable errno returns standard error information. Obviously, it cannot be a local variable. Almost every function can call it, but it cannot be a global variable. Otherwise
The output in thread a is probably the error message of line B. To implement such variables, we must use thread data. We create a key for each thread data, which is associated with this key, in each thread
This key is used to indicate thread data. However, in different threads, this key represents different data. In the same thread, it represents the same data content.

There are four main functions related to thread data: Create a key, specify thread data for a key, read thread data from a key, and delete a key.

The function prototype for creating a key is:
Extern int pthread_key_create _ p (pthread_key_t * _ key, void (* _ destr_function) (void *)));

  
The first parameter is a pointer to a key value, and the second parameter specifies a destructor function. If this parameter is not null, when each thread ends, the system will call this function to release
Memory block bound to this key. This function is often used with the function pthread_once (pthread_once_t * once_control, void
(* Initroutine)
(Void) to create this key only once. The pthread_once function declares an initialization function. When pthread_once is called for the first time, it executes this
Will be ignored in future calls.

In the following example, we create a key and associate it with a certain data. We need to define a function createwindow, which defines a graphical window (the data type is
Fl_window *, which is the data type in the graphic interface development tool fltk ). Since each thread calls this function, we use thread data.
/* Declare a key */
Pthread_key_t mywinkey;
/* Function createwindow */
Void createwindow (void ){
Fl_window * Win;
Static pthread_once_t once = pthread_once_init;
/* Call the createmykey function to create a key */
Pthread_once (& once, createmykey );
/* Win points to a new window */
Win = new fl_window (0, 0,100,100, "mywindow ");
/* Make some possible settings for this window, such as the size, position, and name */
Setwindow (WIN );
/* Bind the window pointer value to the key mywinkey */
Pthread_setpecific (mywinkey, Win );
}

/* The createmykey function creates a key and specifies the Destructor */
Void createmykey (void ){
Pthread_keycreate (& mywinkey, freewinkey );
}

/* Function freewinkey, release space */
Void freewinkey (fl_window * Win ){
Delete win;
}

In this way, the createmywin function can be called in different threads to obtain window variables that can be seen inside the thread.
Pthread_getspecpacific. In the above example, we have used the pthread_setspecific function to bind thread data to a key
. The two functions are prototype as follows:
Extern int pthread_setspecific _ p (pthread_key_t _ key ,__ const void * _ pointer ));
Extern void * pthread_getspecific _ p (pthread_key_t _ key ));

The parameter meanings and usage of these two functions are obvious. Note that when pthread_setspecific is used as a key to specify new thread data, you must release the original data by yourself.
To recycle space. This process function pthread_key_delete is used to delete a key. The memory occupied by this key will be released, but it should also be noted that it only consumes the key.
Does not release the memory resources occupied by the thread data associated with the key, and does not trigger the destructor function defined in the pthread_key_create function. Line
The release of data must be completed before the release key.

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.