1. 程式人生 > >多執行緒同步機制(Vc++)

多執行緒同步機制(Vc++)

Synchronizing Execution of Multiple Threads
To avoid race conditions and deadlocks, it is necessary to synchronize access by multiple threads to shared resources. Synchronization is also necessary to ensure that interdependent code is executed in the proper sequence.
避免死鎖,多執行緒同步機制很主重要。有助於解決資源共享和協助工作的問題。

There are a number of objects whose handles can be used to synchronize multiple threads. These objects include:
有很東西的控制代碼可以同步多執行緒

Console input buffers
輸入buf的控制檯
Events
事件
Mutexes
互斥
Processes
程序
Semaphores
訊號
Threads
執行緒
Timers
定時器
The state of each of these objects is either signaled or not signaled. When you specify a handle to any of these objects in a call to one of the wait functions, the execution of the calling thread is blocked until the state of the specified object becomes signaled.
這些東西的狀態是有訊號或者無訊號的。當你呼叫等待函式的時候給這些東西一個控制代碼。執行緒的回撥函式被阻止知道訊號的狀態改變(即用訊號狀態來控制程序函式的執行與否)
Some of these objects are useful in blocking a thread until some event occurs. For example, a console input buffer handle is signaled when there is unread input, such as a keystroke or mouse button click. Process and thread handles are signaled when the process or thread terminates. This allows a process, for example, to create a child process and then block its own execution until the new process has terminated.
這些東西很有用在阻止一個執行緒的執行直到某些事件發生。比如,控制檯輸入buf控制代碼被標記當還有待讀取的輸入,比如按鍵或者滑鼠點選。(保證使用者的輸入被執行)。程序和執行緒的控制代碼被標記當程序或執行緒終止(terminates)。允許程序,比如建立子程序的時候阻止它自己執行,直到新的程序被建立。
Other objects are useful in protecting shared resources from simultaneous access. For example, multiple threads can each have a handle to a mutex object. Before accessing a shared resource, the threads must call one of the wait functions to wait for the state of the mutex to be signaled. When the mutex becomes signaled, only one waiting thread is released to access the resource. The state of the mutex is immediately reset to not signaled so any other waiting threads remain blocked. When the thread is finished with the resource, it must set the state of the mutex to signaled to allow other threads to access the resource.
在同時訪問的時候保護資源共享,其他的東西也是很有用的。比如,多執行緒的每一個執行緒可以有一個互斥鎖的控制代碼。在訪問共享資源之前,執行緒必須等待執行緒鎖的狀態被標記。當執行緒鎖的被標記,只有一個等待執行緒被釋放去訪問這些資源。這個互斥鎖的狀態立即去掉標記,其他等待的執行緒被阻止。當這個執行緒完成了,它必須改變互斥鎖為標記狀態,以便其他的程序可以訪問這些資源。
For the threads of a single process, critical-section objects provide a more efficient means of synchronization than mutexes. A critical section is used like a mutex to enable one thread at a time to use the protected resource. A thread can use the EnterCriticalSection function to request ownership of a critical section. If it is already owned by another thread, the requesting thread is blocked. A thread can use the TryEnterCriticalSection function to request ownership of a critical section, without blocking upon failure to obtain the critical section. After it receives ownership, the thread is free to use the protected resource. The execution of the other threads of the process is not affected unless they attempt to enter the same critical section.
單程序的執行緒,(臨界區)critical-section 物件提供比互斥鎖更有效的同步機制。臨界區物件提供類似互斥鎖來協調某一個時刻只有一個執行緒去訪問共享資源。執行緒可以用函式EnterCriticalSection 來請求獲取臨界區的所有權。如果臨界區被其他程序所佔有,則請求程序被阻止。
The WaitForInputIdle function makes a thread wait until a specified process is initialized and waiting for user input with no input pending. Calling WaitForInputIdle can be useful for synchronizing parent and child processes, because CreateProcess returns without waiting for the child process to complete its initialization.

For more information, see Synchronization.