Linux C Note-line program control system (II)

Source: Internet
Author: User
Tags mutex terminates

There are two ways to terminate a thread in Linux, one is to return from a thread function by calling return, and the second is to call a function
#include
Voidpthread_exit (void *retavl);

Note: First, if the main thread is returned from the main function or the Exit function is called to exit the main thread, the entire process terminates, and all other threads are terminated at this time. The other is that if the main thread calls the Pthread_exit function, only the main thread dies, the process does not end, the other threads do not end, and the process ends until all the threads are finished.

The separation state of a thread determines how a thread terminates itself. The default attribute of a thread is usually taken, that is, the non-detached state, in which case the original thread waits for the thread to be created to end. Only when the Pthread_join () function returns does the thread created terminate to free the system resources that it occupies. The separation thread is not the case, it is not waiting for the other threads, the end of their own run, the thread is terminated, immediately release the system resources.
Avoid a potential memory leak problem when you use Pthread to prevent threads from being released properly when the thread ends, and to ensure that the thread is in the detached state at the end of the thread, you will need to call the Pthread_join () function to reclaim it.




Private data


All threads within a process share the data space of the process, so global variables are common to all threads. In some scenarios, the thread needs to save its own private data, so you can create a thread-private (thread-specific data) TSD to resolve. Inside the thread, private data can be accessed by the threads ' interfaces, but Cheng the other lines.

Thread-private data uses a one-key multivalued technique, and a key that corresponds to multiple values. Access to data is accessed through key values. When using thread-private data, there are four main interfaces that need to be created for each thread to create an associated key,linux:

1. Pthread_key_create: Create a key
int Pthread_key_create (pthread_key_t *key, Void (*destr_function) (void*));

First, assign an entry from the Linux TSD pool and assign its value to key for later access. The first parameter of the interface is a pointer to the parameter, the second argument is a function pointer, and if the pointer is not empty, then when the thread execution exits, the content that the key points to is called destr_function (), releasing the allocated buffer and other data.
After the key has been created, because it is a global variable, all threads can be accessed. Each thread can populate a key with different values depending on the requirements, which is equivalent to providing a global variable with the same name but with a different value, that is, one-click Multi-value. One-key multi-valued dependency of a struct-body array, i.e.
static struct pthread_key_struct Pthread_keys[pthread_keys_max] ={{0,null}};

Create a TSD, which is equivalent to setting the SEQ value of one element of a struct array to "In_use" and returning its index to *key, and then setting Destr_function () to Destr (). Pthread_key_create Create a new thread-private Data key, the system searches for an array of key structures in its process, finds an unused element, and assigns its index to *key.

2. Pthread_setspecific: Set thread private data for the specified key value
int pthread_setspecific (pthread_key_t key, const void *pointer);

The interface relates the value of the pointer pointer (the pointer value, not the content it points to), and the thread must free the original data to reclaim the space when the new thread data is specified with Pthread_setspecific for a key.

3, Pthread_getspecific: Read the private data of the thread from the specified key
void * Pthread_getspecific (pthread_key_t key);

4. Pthread_key_delete: Delete a key
void * Pthread_getspecific (pthread_key_t key);

This interface is used to delete a key, the function is simply to set the key in the structure of the array Pthread_keys corresponding element to "Un_use", and change the key associated with the thread data is not freed, so the release of the thread private data must be before the key is deleted.




Mutual exclusion Lock:
To rule out the impact of competitive conditions, some operations should be turned into "atomic"-operations that are neither segmented nor interrupted.
When a thread locks the mutex, it is blocked by other threads when it is required to lock the mutex, until the other threads can unblock the recovery run until the previous thread is unlocked.
Gnu/linux guarantees that the thread will not compete in the process of locking the mutex, and that only one thread can lock the mutex, and the lock request of the other thread will be blocked.

To create a mutex (mutex), you need to:
Create a variable of type pthread_mutex_t and pass its pointer to the function pthread_mutex_init; the second parameter of the function is a pointer to a mutex Property object that is used to specify the properties of the mutex. A mutex object can be initialized only once.
A simpler approach is to use Pthread_mutex_initializer to obtain the default properties of the MUTEX object:
pthread_mutex_t mutex = Pthread_mutex_initializer;


Call the Pthread_mutex_lock function, which returns immediately if the mutex object is not locked, or if it is locked, this function blocks execution of this thread until the mutex is unlocked.
After each unlock, only one thread can unblock the recovery execution, and the other calling threads will continue to block. The selected unblocked thread is unpredictable.
Call the Pthread_mutex_unlock function to unlock the mutex object from the calling thread. In the thread that locks the mutex, you must call this function to unlock it.

Function Description:

Required Header files: pthread.h
1) Initialize Mutex lock

function prototypes: int pthread_mutex_init (pthread_mutex_t *mp, const Pthread_mutex attr_t *mattr)


Before the mutex is initialized, it must be zeroed out of its memory.

If the mutex is initialized, it is in an unlocked state. Mutexes can be in memory that is shared between processes or in the private memory of a process.

2) Lock Mutex

Function Prototypes:

int Pthread_mutex_lock (pthread_mutex_t *mutex);
pthread_mutex_t Mutex;

int ret;
ret = Pthread_ Mutex_lock (&MP); /* Acquire the mutex */

Function Description:

When Pthread_mutex_lock () returns, the mutex is locked. The calling thread is the owner of the mutex. If the mutex has been locked and owned by another thread, the calling thread will block until the mutex becomes available.

Linux C Note-line program control system (II)

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.