使用 Win32 事件对象
新手问题:
的示例
此链接显示了 CreateEvent 和 CreateThread http://msdn.microsoft.com/en-us/library/ms686915(v=vs.85).aspx
我的问题是 ThreadProc 是否真的是线程安全的?
具体来说,是 dwWaitResult 变量。由于所有线程都在等待同一事件,因此事实证明这段代码可以工作,但是如果创建了不同的事件,例如,这将无法正确工作?
Noob question:
This link shows an example of CreateEvent and CreateThread
http://msdn.microsoft.com/en-us/library/ms686915(v=vs.85).aspx
My question is if the ThreadProc is truly thread safe?
Specifically, the dwWaitResult variable. Since all threads are waiting on the same event, it turns out this code works but had different events been created, for example, this would not work correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
dwWaitResult
变量是该函数中的局部变量。因此,每个单独的线程都有自己的副本,这确保了变量是线程安全的。每个线程都有自己的堆栈,因此所有局部变量都是特定于各个线程的。The
dwWaitResult
variable is a local variable in that function. Thus each individual thread has its own copy, which assures that the variable is thread safe. Each thread has its own stack, therefore all local variables are specific to the individual thread.事件是按名称创建的,因此如果事件已创建,则在“创建”同名事件的任何其他线程中重用。因此,示例代码是线程安全的。
The Event is created by name, so if the event is already created is reused in any other thread "creating" an event with the same name. As a result, the example code IS thread safe.