设为首页 加入收藏

TOP

多线程同步机制(Vc++)(一)
2014-11-23 21:34:50 】 浏览:1109
Tags:线程 同步 机制

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 mo

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇VC多线程编程之线程创建与示例 下一篇如何结束线程VC++

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目