《Win32多线程程序设计》学习笔记 第4章 同步控制之 事件(Event Objects) 和 interlocked variables...

 event

Event对象是一种核心对象,它的唯一目的就是成为激发状态或者未激发状态。这两种状态全由程序来控制,不会成为Wait..()函数的副作用。他们的状态完全在你的掌控之下。Mutex和semaphore就不同了,他们的状态会因为诸如WaitForSingleObject()之类的函数调用而变化。所以,你可以精确地告诉一个event对象做了什么事,以及什么时候做。

创建event对象

 

 

Creates or opens a named or unnamed  event   object .

HANDLE WINAPI CreateEvent(
  LPSECURITY_ATTRIBUTES lpEventAttributes,  
// 安全属性
  BOOL bManualReset,           // 如未FALSE,表示这个event将在变成激发状态(因而唤醒一个线程)之后,自动重置为(reset)未非激发状态,如果为TRUE,表示不会自动重置,必须靠程序操作(调用ResetEvent)才能将激发状态的event重置为非激发状态。
  BOOL bInitialState,         // 如果为true,表示这个event一开始处于激发状态。如果FALSE,则表示这个event一开始处于非激发状态。
  LPCTSTR lpName        // event的名称。
);

返回值:如果成功,会传回一个eventhandle,失败传回null。

使用event对象

  •  SetEvent(): 把event对象设为激发状态。
  • ResetEvent(): 把event对象设为非激发状态。
  • PulseEvent():如果是一个manual reset event:把event对象设为激发状态,唤醒所有的等待中的线程,然后event恢复为非激发状态。 如果是一个auto reset event:把event对象设为激发状态,唤醒一个等待中的线程,然后event恢复为非激发状态。

 Interlocked Variables

 

 同步机制的最简单类型是使用interlocked 函数,对着标准的32位变量进行操作。这些函数并没有提供“等待”机能,他们只是保证对某个特定变量的存取操作是“一个一个接顺序来”。所谓的interlocked函数,有2个:interlockedIncrement()和interlockedDecrement():

 

Increments (increases by one) the value of the specified  32 - bit variable  as  an atomic operation.To operate on  64 - bit values, use the InterlockedIncrement64 function.

LONG __cdecl InterlockedIncrement(
 LONG 
volatile *  Addend    // A pointer to the variable to be incremented.
);

Return Value
The function returns the resulting incremented value. 
/// //
Decrements (decreases by one) the value of the specified  32 - bit variable  as  an atomic operation. To operate on  64 - bit values, use the InterlockedDecrement64 function.

LONG __cdecl InterlockedDecrement(
   LONG 
volatile *  Addend   // A pointer to the variable to be decremented.
);

Return Value
The function returns the resulting decremented value. 


interlockedExchange()可以设定一个新值并传回旧值。就想以上2个函数一样。提供了在多线程环境下的安全做法。

 

Sets a  32 - bit variable to the specified value  as  an atomic operation. To operate on a pointer variable, use the InterlockedExchangePointer function.

LONG __cdecl InterlockedExchange(
  LONG 
volatile *  Target,    // 32位变量的地址。必须指向 long word
  LONG Value        // 用以取代Target所指内容的新值
);

返回值: 传回先前由lpTarget所指的内容。

 

转载于:https://www.cnblogs.com/kwliu/archive/2010/11/27/2195915.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值