Line Program Control system

Source: Internet
Author: User
Tags mutex

1. Thread limit

Single UNIX defines the limitations of the first-line threading operation, as is the case with other restrictions, which can be queried by sysconf. As with other restricted purposes, these limits are used for portability of applications between different operating system implementations. Some limitations:

2. Thread Properties

When calling the Pthread_create function to create a new thread, you can specify the properties of the thread, the property type is pthread_attr_t, the structure is opaque to the application, and the operation function is as follows:

int Pthread_attr_init (pthread_attr_t *attr);           Initialize thread properties int Pthread_attr_destroy (pthread_attr_t *attr);        Releasing Thread property space

The main thread properties are:

3. Synchronization Properties (Halo)

(1) Mutex attribute: There are process shared properties, robust properties, and type attributes three.

The process share property is optional, and the Mutex property data type is pthread_mutexattr_t. In a process, multiple threads can access the same synchronization object, by default the process share Mutex property is: Pthread_process_private.

The mutex robustness property is related to the amount of mutex that is shared across multiple processes. This means that when the process that holds the mutex is terminated, the problem of the mutex state recovery needs to be resolved. When this happens, the mutex is locked and it is difficult to recover. Other blocking processes in this lock will continue to block.

The type mutex controls the nature of the mutex, and POSIX.1 defines four types:

(2) Read-write Lock properties

The only property supported by read-write locks is the process share property. It is the same as the mutex process share property.

(3) Condition variable properties

The condition variable supports two properties: the process share property and the clock property.

The process share property controls whether the condition variable can be used by more than one thread of the process or can be used by a multi-process thread.

The clock property controls which clock is used when calculating the timeout parameter (tsptr) of the pthread_cond_timedwait function.

(4) Barrier properties

Barrier properties are only process-shared properties.

4. Re-entry

With signal handlers and multithreading, multiple control threads may potentially invoke the same function at the same time, and if a function can be safely called by multiple threads at the same point in time, it is called a thread-safe function.

Many functions are not thread-safe, because the data they return is stored in a static memory buffer and can be thread-safe by modifying the interface, requiring the caller to provide a buffer themselves.

5. Thread-specific data

A thread seems to have a mechanism for storing and querying data associated with a thread when it has data, and you want each thread to have independent access to the copy of the data without worrying about synchronous access to other threads. All threads in the process can access the entire address space of the process, except that the thread has no way to prevent other threads from accessing its data, in addition to using registers, and the thread seems to have data, no exception. Functions that manage thread-private data can improve data independence between threads.

Assign thread private Data procedure: First call Pthread_key_create to create the key associated with the data to obtain access to the thread's private data, which can be accessed by all threads in the process. But each thread associates this key with a different thread-private data address and then, by invoking the Pthread_setspecific function bar and the thread-private data, the address of the thread's private data can be obtained through the pthread_getspecific function.

The Pthread_key_create function can select the destructor associated with the key and call the Pthread_key_delete function to cancel the association between the thread private data values. By calling the Pthread_once function, ensure that the assigned key is not changed due to competition during the initialization phase.

6. Threads and Signals

Each thread has its own signal-masking word, but the processing of the signal is shared by all threads in the process. The signal in the process is passed to a single thread, and the signal masking function in the process is not defined in the thread, and the Sigprocmask must use Pthread_sigmask. A thread can call the Sigwait function to wait for one or more messages to be sent. Call the Pthread_kill function to send the signal to the thread.

7. Threading and Fork

The parent process calls fork to create a copy of the entire process address space for the child process, which inherits the state of all mutexes, read-write locks, and condition variables from the parent process. If the parent process includes more than one thread, the child process will need to clean up the lock after the fork returns, if it is not immediately called Exec. There is only one thread inside the child process, which is made up of a copy of the thread that called fork in the parent process, the thread in the parent process occupies the lock, and the child process also occupies the lock, but the child process does not contain a copy of the thread that holds the lock. The fork handler clears the lock state through the Pthread_atfork function. The function prototypes are as follows: int pthread_atfork (void (*prepare) (void), void (*parent) (void), void (*child) (void));

The prepare handler is called by the parent process before the fork creates the child process, getting all the locks defined by the parent process. The parent handler is called after fork creates the child process, but before the fork returns in the parent process environment, all locks obtained by the prepare handler are unlocked, and the child handler is invoked in the sub-process environment before the fork returns. All locks obtained by the prepare handler must also be released. The parent and child handlers are in the same order as they were registered, and the prepare handler call is the opposite of the order in which it was registered.

Citations:

Http://www.cnblogs.com/Anker/archive/2012/12/19/2824990.html

Line Program Control system

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.