Multiple threads in the process use a global variable.

Source: Internet
Author: User

In the signal section, let's look back at the concept of A reentrant function. The reentrant function is a function that can be called in the signal processing function. It does not use global variables or static variables.

The thread has the concept of a thread-safe function, which is similar to a reentrant function. A thread-safe function is a function that can be safely called in the thread. What kind of function will be called by the thread unsafely? Functions that access critical resources are insecure. Although global and static variables are critical resources, this concept is different from non-reentrant functions. If unsafe functions use synchronous measures for critical zones, this function is still a non-reentrant function, but it is thread-safe.

If multiple threads in a process use a global variable, the variable becomes a critical resource, the Posix thread Library provides us with a method to make the same global variable private to each thread (for example, errno is a global variable, but each thread has its own errno private ). This method is called the private data of a thread (the values of the same global variable are different in each thread). It is implemented by binding the key value, and the key is created/deleted through the pthread_key_create ()/pthread_key_delete () function, use pthread_setspecific () to bind the key value and global variable, and use pthread_getspecific () to obtain the private global variable value corresponding to the key value.


[Cpp] int pthread_key_create (pthread_key_t * key, void (* destructor) (void *));
Int pthread_key_delete (pthread_key_t key );
Int pthread_setspecific (pthread_key_t key, const void * value );
Void * pthread_getspecific (pthread_key_t key );

Int pthread_key_create (pthread_key_t * key, void (* destructor) (void *));
Int pthread_key_delete (pthread_key_t key );
Int pthread_setspecific (pthread_key_t key, const void * value );
Void * pthread_getspecific (pthread_key_t key); the signal is the semantics in the process. How is the signal processed in the process? Previously, the signal sent to the process will be sent to a thread in the process. When the thread is created, the signal shielding characters will be inherited, but the signal shielding characters between threads will not affect each other, however, the previously learned function sigprocmask () is used to change the blocking word of the entire process (All threads). How can we change the blocking word of only one thread? Use the pthread_sigmask () function:


[Cpp] int pthread_sigmask (int how, const sigset_t * set, sigset_t * oset );

Int pthread_sigmask (int how, const sigset_t * set, sigset_t * oset); the parameters and return values of this function are the same as those of sigprocmask. A typical measure to process signals in multithreaded programming is to specify a special thread to process the specified signal, while other threads shield the signal.

What should we do if we want to send a signal to a thread (it can only be between threads in the same process, because the thread id is meaningful only in the same process )? Use the pthread_kill function:


[Cpp] int pthread_kill (pthread_t thread, int signo );

Int pthread_kill (pthread_t thread, int signo); If signo is 0, it can be used to test whether the thread exists.

There is also a sigwait () function, which blocks the current thread and places the signal in the set in the pending signal set to signop.


[Cpp] int sigwait (const sigset_t * set, int * signop );

Int sigwait (const sigset_t * set, int * signop );


 

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.