Http://blog.csdn.net/lzx_bupt/article/details/6913151
Recently, I like to hear old songs from universities. Deutschland was popular during the World Cup in Germany. It is said that it is not a theme song, but it is more popular than the theme song.
This article introduces the difficult concept of mutex lock. mutex = mutual exclusion stands for mutex =. By the way, teachers used to love the abbreviation and did not tell the full name to their classmates, can Nima have a deep understanding! The usage is as follows:
[CPP] View plaincopy
-
- # Include <iostream>
-
- # Include <pthread. h> // follow the rules
-
-
- Using NamespaceSTD;
-
-
- # Define num_threads 5
-
- IntSum = 0;// Define a global variable to allow all threads to access it. This will result in simultaneous write, and a lock mechanism is required;
-
- Pthread_mutex_t sum_mutex;
-
-
- Void* Say_hello (Void* ARGs)
-
- {
-
- Cout <"Hello in thread"<*((Int*) ARGs) <Endl;
- Pthread_mutex_lock (& sum_mutex );// Modify sum to lock the lock first. When the lock is occupied, the lock is blocked until the lock is obtained and sum is modified;
-
- Cout <"Before sum is"<Sum <"In thread"<*((Int*) ARGs) <Endl;
-
- Sum + = *((Int*) ARGs );
- Cout <"After sum is"<Sum <"In thread"<*((Int*) ARGs) <Endl;
-
- Pthread_mutex_unlock (& sum_mutex );// Unlock after completion and release it to other threads for use;
-
-
- Pthread_exit (0 );// Exit and throw a status code.
-
- }
-
-
- IntMain ()
- {
-
- Pthread_t tids [num_threads];
-
- IntIndexes [num_threads];
-
- // The next three statements are about setting thread parameters.
-
- Pthread_attr_t ATTR;
-
- Pthread_attr_init (& ATTR );
-
- Pthread_attr_setdetachstate (& ATTR, pthread_create_joinable );
-
- Pthread_mutex_init (& sum_mutex, null );// This statement is required to initialize the lock;
-
-
- For(IntI = 0; I <num_threads; ++ I)
-
- {
-
- Indexes [I] = I;
- IntRet = pthread_create (& tids [I], & ATTR, say_hello ,(Void*) & (Indexes [I]);// Modify the sum of the five processes;
-
- If(Ret! = 0)
-
- {
-
- Cout <"Pthread_create error: error_code ="<RET <Endl;
- }
-
- }
-
-
- Pthread_attr_destroy (& ATTR );// Delete parameter variables
-
-
- Void* Status;
-
- For(IntI = 0; I <num_threads; ++ I)
-
- {
- IntRet = pthread_join (tids [I], & status );
-
- If(Ret! = 0)
-
- {
-
- Cout <"Pthread_join error: error_code ="<RET <Endl;
-
- }
-
- }
-
- Cout <"Finally sum is"<Sum <Endl;
-
-
- Pthread_mutex_destroy (& sum_mutex );// Deregister the lock. We can see that all the built-in pthread variables correspond to the destroy function. It is estimated that the memory leakage is related;
-
- }
Convention: G ++-lpthread-O ex_mutex ex_mutex.cpp
Run:
[CPP] View plaincopy
-
- Hello inThread4
- Before sum is 0 inThread4
-
- After sum is 4 inThread4
-
- Hello inThread3
-
- Before sum is 4 inThread3
-
- After sum is 7 inThread3
-
- Hello inThread2
- Before sum is 7 inThread2
-
- After sum is 9 inThread2
-
- Hello inThread1
-
- Before sum is 9 inThread1
-
- After sum is 10 inThread1
-
- Hello inThread0
- Before sum is 10 inThread0
-
- After sum is 10 inThread0
-
- Finally sum is 10
It was found that thread4 was run first. It was strange that I increased progressively from 0, so the order of multithreading was chaotic and chaos was normal; as long as sum access and modification are normal, multithreading is achieved, and the running sequence cannot be used as a reference;