First, synchronization between threads there is also a situation where thread a needs to wait for a certain condition to continue to execute, now that the condition is not established, thread A is blocking the wait, and thread B is in the process of executing this condition, and waking up thread a continues execution. A condition variable (Condition Variable) is blocked in the Pthread library to wait for a condition, or to wake a thread waiting for the condition. Condition A variable table of type variablepthread_cond_t, which can be initialized and destroyed:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/7F/44/wKioL1cYeM2whfMMAAA72IoCNxU075.jpg "title=" L1. JPG "alt=" wkiol1cyem2whfmmaaa72iocnxu075.jpg "style=" padding:0px;margin:0px;vertical-align:top;border:none; >
Return value: Successful return 0, Failure returns error number.
A condition variable is always used in conjunction with a mutex. A thread can call pthread_cond_wait on a condition variable to block the wait, this function does the following three steps:
1. Releasing the mutex
2. Blocking wait
3. When awakened, regain the mutex and return
pthread_cond_ The Timedwait function also has an additional parameter that sets the wait timeout and returns etimedout if no other thread wakes the current thread at the time specified by Abstime. A thread can call pthread_cond_signal to wake up another thread waiting on one of the condition variable, or it can invoke Pthread_cond_broadcast wake in this condition All threads waiting on the variable.
Ii. Producer-consumer issues
This example uses the lead single-linked list to achieve the place between producer consumers, using the tail plug to delete the function. As follows, there is only one consumer, one producer. The producer implements the insertion node, and the consumer implements the delete node. A conditional variable is required to be used in conjunction with a mutex.
The code is as follows:
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M00/7F/55/wKioL1caJZrQ_xzkAACbnOPzC84976.jpg "style=" float: none; "Title=" L11. JPG "alt=" Wkiol1cajzrq_xzkaacbnopzc84976.jpg "/>
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/7F/57/wKiom1caJNbQzwzHAABd9D0dHUk208.jpg "style=" float: none; "Title=" l12. JPG "alt=" Wkiom1cajnbqzwzhaabd9d0dhuk208.jpg "/>
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M02/7F/55/wKioL1caJZuhWIvrAAB_IVVwB9g464.jpg "style=" float: none; "Title=" L13. JPG "alt=" Wkiol1cajzuhwivraab_ivvwb9g464.jpg "/>
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/7F/55/wKioL1caJZvy2D49AACHd9WXrcM095.jpg "style=" float: none; "Title=" L14. JPG "alt=" Wkiol1cajzvy2d49aachd9wxrcm095.jpg "/>
The results of the operation are as follows:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/7F/57/wKiom1caJNiDW_UxAABSiMokI-c199.jpg "style=" float: none; "Title=" L10. JPG "alt=" Wkiom1cajnidw_uxaabsimoki-c199.jpg "/>
Instead of multithreading: more consumers, more producers. Needs to be realized, mutual exclusion between consumers and consumers, mutual exclusion between producer and manufacturer, and mutual exclusion between consumers and producers. The code is as follows:
650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/7F/57/wKiom1caJTzxUNSOAABcYA2Qpx0341.jpg "style=" float: none; "Title=" L16. JPG "alt=" Wkiom1cajtzxunsoaabcya2qpx0341.jpg "/>
650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M01/7F/55/wKioL1caJgDDbv8NAABk4WxoxKA323.jpg "style=" float: none; "Title=" L17. JPG "alt=" Wkiol1cajgddbv8naabk4wxoxka323.jpg "/>
The results of the operation are as follows:
650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M02/7F/57/wKiom1caJTzB1_NLAABW9RWHLp4954.jpg "style=" float: none; "Title=" L15. JPG "alt=" Wkiom1cajtzb1_nlaabw9rwhlp4954.jpg "/>
Summary: The synchronization is realized on the basis of mutual exclusion, which not only solves the deadlock problem, but also makes the communication between threads more efficient.
This article is from the "fringe" blog, so be sure to keep this source http://ab6107.blog.51cto.com/10538332/1766888
Using conditional variables to realize the problem of producer and consumer in multi-threading under Linux