Linux multithreading and lock Synchronization

Source: Internet
Author: User

POSIX pthread

Libraries

It is a standard thread API-Based C/C ++. Concurrent execution. This is the most effective process flow of a multi-processor or multi-core system. It runs on another processor to speed up processing through parallel or distributed processing.

 

 

 

Introduction:

The thread Library provides three synchronization mechanisms:

Mutexes: mutex lock: It mainly prevents other threads from accessing variables and forces the thread to exclusively occupy one or more variables.

Join: enables a thread to wait until other threads finish or terminate (exit (0 ));

Contidion variables: Condition variable. The data type is phread_cond_t.

Details:

Mutexes: multithreading operates data in the same memory area at the same time to prevent data inconsistency. usage or competition usually occurs in multiple threads, and the operations executed are in the same memory area, for example, modifying a state variable. The lock is mainly used to lock shared resources. You need to add a lock for the family variables accessed by multiple threads. View code Comprehension

No lock code segment

Int counter = 0;

Void updatecounter ()

{

Counter ++

};

Code segment with lock

Pthread_mutex_t mutext = pthread_mutex_initializer

Int counter = 0;

Void updatecounter ()

{

Pthread_mutex_lock (& mutext );

Counter ++

Pthread_mutex_unlock (& mutext );

};

KEY NOTES

Pthread_mutex_lock (): gets the lock on the specified mutex variable. If the mutex has been locked by another thread, the call will block the call thread until the mutex is released.

Pthread_mutex_unlock ()-Unlock the mutex variable. If it has been unlocked or is mutually exclusive by another thread, an error is returned.

Pthread_mutex_trylock ()-an error code is returned when an attempt is made to lock a mutex. It is mainly used to prevent deadlock conditions.

Mutex lock demo

 

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

Void * updatecounter ();

Pthread_mutex_t mutex1 = pthread_mutex_initializer;
Int counter = 0;

Main (){

Int RC1, RC2;
Pthread_t thread1, thread2;
If (RC1 = pthread_create (& thread1, null, & updatecounter, null )))
{
Printf ("thread creation failed: % d \ n", RC1 );
}
If (RC2 = pthread_create (& thread2, null, & updatecounter, null )))
{
Printf ("thread creation failed: % d \ n", RC2 );
}

Pthread_join (thread1, null );
Pthread_join (thread2, null );

Exit (0 );

}

Void * updatecounter (){
 
Pthread_mutex_lock (& mutex1 );

Counter ++;

Printf ("counter value: % d \ n", counter );

Pthread_mutex_unlock (& mutex1 );
}

 

Gcc-lpthread mutex1.c-O mutext. Out

Output:

David Yang: Project weflytotti $./mutext. Out

Counter value: 1

Counter value: 2

JoinsDemo

Joins: Wait for other threads to finish. A thread scheduler can start multiple threads and wait for them to finish.

Code

 

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

# Define nthreads 10
Void * thread_function (void *);
Pthread_mutex_t mutex1 = pthread_mutex_initializer;
Int counter = 0;

Main ()
{
Pthread_t thread_id [nthreads];
Int I, J;

For (I = 0; I <nthreads; I ++)
{
Pthread_create (& thread_id [I], null, thread_function, null );
}

For (j = 0; j <nthreads; j ++)
{
Pthread_join (thread_id [J], null );
}
 

Printf ("final counter value: % d \ n", counter );
}

Void * thread_function (void * dummyptr)
{
Printf ("thread Number % LD \ n", pthread_self ());
Pthread_mutex_lock (& mutex1 );
Counter ++;
Pthread_mutex_unlock (& mutex1 );
}

 

Output:

Gcc-lpthread join. C-o join. Out

David Yang: Project weflytotti $./join. Out

Thread number 4304330752

Thread number 4305981440

Thread number 4306518016

Thread number 4307054592

Thread number 4307591168

Thread number 4308127744

Thread number 4308664320

Thread number 4309200896

Thread number 4309737472

Thread number 4310274048

Final counter value: 10

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.