`

多线程同步机制(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.
分享到:
评论

相关推荐

    多线程的同步机制 VC++

    多线程的同步机制 多线程的同步机制 多线程的同步机制

    vc++多线程编程_附源码

    讲述windows多线程编程的机制,包括线程同步、线程通讯等,每一个讲解附带源码

    vc++ 线程同步与异步套接字编程实例

    vc++ 线程同步与异步套接字编程实例,Windows套接字在两种模式下执行I/O操作,阻塞和非阻塞。在阻塞模式下,在I/O操作完成前,执行操作的Winsock函数会一直等待下去,不会立即返回程序(将控制权交还给程序)。而在非...

    vc++ 应用源码包_3

    实现了自绘控件,云端控制主要在CnComm类多线程串口通讯库, camerads-DirectShow使用示例 演示了摄像头的使用 CatListBoxDemo ListBox控件与其它控件阙套使用方法 CCAMS系统是一种用于局域网下的CS模式的软件...

    孙鑫C++教程(全20讲)PPT讲义源码及电子书

    第十五课:多线程与聊天室程序的创建 第十六课:线程同步与异步套接字编程 第十七课:进程间通信 第十八课:ActiveX控件 第十九课:动态链接库 第二十课:HOOK和数据库访问 希望对大家的学习有帮助,如果不错,请帮点个赞...

    vc++ 应用源码包_1

    实现了自绘控件,云端控制主要在CnComm类多线程串口通讯库, camerads-DirectShow使用示例 演示了摄像头的使用 CatListBoxDemo ListBox控件与其它控件阙套使用方法 CCAMS系统是一种用于局域网下的CS模式的软件...

    VC++编程指南(中文版).chm

    VC++编程指南(中文版),不错的一本电子书。啥也不说了,直接看内容预览...第十二章 多线程与串行通信  12.1 多任务、进程和线程  12.2 线程的同步  12.3 串行通信与重叠I/O  12.4 一个通信演示程序  12.5 小结

    vc++ 应用源码包_5

    实现了自绘控件,云端控制主要在CnComm类多线程串口通讯库, camerads-DirectShow使用示例 演示了摄像头的使用 CatListBoxDemo ListBox控件与其它控件阙套使用方法 CCAMS系统是一种用于局域网下的CS模式的软件...

    vc++ 应用源码包_2

    实现了自绘控件,云端控制主要在CnComm类多线程串口通讯库, camerads-DirectShow使用示例 演示了摄像头的使用 CatListBoxDemo ListBox控件与其它控件阙套使用方法 CCAMS系统是一种用于局域网下的CS模式的软件...

    vc++ 应用源码包_6

    实现了自绘控件,云端控制主要在CnComm类多线程串口通讯库, camerads-DirectShow使用示例 演示了摄像头的使用 CatListBoxDemo ListBox控件与其它控件阙套使用方法 CCAMS系统是一种用于局域网下的CS模式的软件...

    编程技巧(全部工程源代码,物超所值)

    在PB中利用Delphi的多线程机制实现对串口的实时监听;ActiveMovie控件在VC++多媒体程序开发中的应用;VC++环境下多虚拟桌面程序的实现;VC中动态打开显示数据库实现;利用Delphi编写远程控制程序;利用VC++6.0对...

    VC++ 编程指南_中文chm

    第一课 Windows编程和面向对象技术  1.1 Windows发展历史 ...第十二章 多线程与串行通信  12.1 多任务、进程和线程  12.2 线程的同步  12.3 串行通信与重叠I/O  12.4 一个通信演示程序  12.5 小结

    VC++6.0实效编程百例

    58·多线程方式同时进行多项任务 59·线程优先级示例==赛马 60·利用剪贴板实现exe程序间的数据交换 61·通过内存映射实现exe程序间的数据交换 62·通过消息机制实现exe程序间的数据交换 第七章 63·获取驱动器...

    vc++ 开发实例源码包

    实现了自绘控件,云端控制主要在CnComm类多线程串口通讯库, camerads-DirectShow使用示例 演示了摄像头的使用 CatListBoxDemo ListBox控件与其它控件阙套使用方法 CCAMS系统是一种用于局域网下的CS模式的软件...

    VC++6.0 分布式坦克大战游戏 源码

    多线程; 使用DIBs和DrawDIB API (基于WFW)用于动画处理; 同步机制; 消息打包和解包; 定时机制; 自动和随机地传输地形; 图像管理器; 线程内消息快速排队支持; 键盘管理器:将虚拟键映射为本地坦克的随机移动...

    生产消费者的同步问题源代码

    用同步对象、自定义计数信号量等四种方法实现生产者消费者共享有限缓冲区问题Vc++源代码。问题描述:一些生产者线程各自找出自己要搜索的范围内的素数,并放到一个有限缓冲区——数组中,另一些消费者线程线程将数组...

    VC++可视化编程指南

    第一课 Windows编程和面向对象技术  1.1 Windows发展历史 ...第十二章 多线程与串行通信  12.1 多任务、进程和线程  12.2 线程的同步  12.3 串行通信与重叠I/O  12.4 一个通信演示程序  12.5 小结

    单片机与DSP中的基于VC++ 6.0的高速串口通信数据采集系统

    结合串口通信的机理和多线程同步方式,分析了Win32系统下多线程异步串口通信程序的开发方法.  1 引 言  串行通讯是计算机与其他设备进行数据交换时经常使用的方法之一,他具有实现简单,使用灵活方便,数据传输可靠...

    Windows环境下32位汇编语言程序设计 第2版(罗文斌) 完整光盘

    使用临界区对象解决多线程同步问题 Chapter12\ThreadSyn\UseEvent ;使用事件对象解决多线程同步问题 Chapter12\ThreadSyn\UseMutex ;使用互斥对象解决多线程同步问题 Chapter12\ThreadSyn\UseSemaphore ;使用信号灯...

    Windows环境下32位汇编语言程序设计(最新琢石成器版)附属光盘

    使用信号灯对象解决多线程同步问题 Chapter13\CmdLine ;使用命令行参数 Chapter13\Process ;创建进程的例子 Chapter13\ProcessList ;显示系统中运行的进程列表 Chapter13\Patch1 ;一个内存补丁程序 Chapter13\...

Global site tag (gtag.js) - Google Analytics