Thread resolution (3)

Source: Internet
Author: User

Author: Cao zhongming, a lecturer at Huaqing vision embedded College.

We have discussed the creation and cancellation of threads. Here we will talk about the synchronization problem between threads.

When multiple threads exist in the same process, multiple threads share the same memory to ensure that each thread can see a consistent data view, if each thread does not read or modify the shared variables, there will be no consistency problem. If the shared variables are read-only, this problem will not exist. However, when a thread can modify a variable while other threads read or modify the variable, synchronization between threads is required, make sure that they do not access invalid data when accessing the variable content.

A synchronization method is introduced here: mutex

A mutex is essentially a lock. When you access a shared resource, the mutex is locked and unlocked after the access. Here we will explain how to operate the mutex lock.

1. Create and undo

The mutex Volume is expressed by the pthread_mutex_t data type. It must be initialized before use and released after use. You can use pthread_mutex_initializer to initialize mutex (static initialization) or the pthread_mutex_init function to dynamically allocate memory for mutex, after use, you must use pthread_mutex_destroy to release memory units. The following is a prototype of these functions:
Pthread_mutex_t mutex = pthread_mutex_initializer;
Int pthread_mutex_init (pthread_mutex_t * restrict mutex,
Const pthread_mutexattr_t * restrict ATTR );
Int pthread_mutex_destroy (pthread_mutex_t * mutex );

2. Lock Operation

Lock operations include pthread_mutex_lock (), pthread_mutex_unlock (), and pthread_mutex_trylock. Use pthread_mutex_lock to lock the mutex. here you need to obtain the lock. If the lock cannot be obtained, the calling thread will block other threads from calling pthread_mutex_unlock to unlock the mutex.
Int pthread_mutex_lock (pthread_mutex_t * mutex );
Int pthread_mutex_trylock (pthread_mutex_t * mutex );
Int pthread_mutex_unlock (pthread_mutex_t * mutex );

If the thread does not want to be blocked, call pthread_mutex_trylock to lock the mutex. If the mutex is not locked, the function returns 0 and locks the mutex. Otherwise, the system fails, returns ebusy.

3. Example

Next we will use a routine to illustrate the use of these functions.

# Include <stdio. h>
# Include <pthread. h>

Pthread_mutex_t mutex;

Void * thread_a (void * Arg)
{
Printf ("thread a enter/N ");
Pthread_mutex_lock (& mutex );
Printf ("mutex lock/N ");
Sleep (10 );
Pthread_mutex_unlock (& mutex );
Printf ("mutex unlock/N ");
}

Void * thread_ B (void * Arg)
{
Printf ("thread B enter/N ");
While (pthread_mutex_trylock (& mutex ))
{
Printf ("pthread trylock/N ");
Sleep (1 );
}
Printf ("mutex lock/N ");
Pthread_mutex_unlock (& mutex );
Printf ("mutex unlock/N ");
}

Int main (INT argc, char ** argv)
{
Pthread_t tid_a, tid_ B;
Int err;

If (pthread_mutex_init (& mutex, null )! = 0)
{
Perror ("pthread_mutex_init ");
}

Err = pthread_create (& tid_a, null, thread_a, null );
If (ERR <0)
{
Perror ("pthread_create thread_a ");
}
Sleep (1 );
Err = pthread_create (& tid_ B, null, thread_ B, null );
If (ERR <0)
{
Perror ("pthread_create thread_a ");
}

Sleep (20 );
Printf ("the main close/N ");
Return 0;
}

Result:
Thread a enter
Mutex lock
Thread B enter
Pthread trylock
Pthread trylock
Pthread trylock
Pthread trylock
Pthread trylock
Pthread trylock
Pthread trylock
Pthread trylock
Pthread trylock
Mutex unlock
Mutex lock
Mutex unlock
The main close

The results show the usage of mutex and the usage of several related functions.

 

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.