Chinaunix首页 | 论坛 | 博客
  • 博客访问: 110923
  • 博文数量: 52
  • 博客积分: 2050
  • 博客等级: 大尉
  • 技术积分: 451
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-06 15:52
文章分类

全部博文(52)

文章存档

2011年(1)

2010年(2)

2009年(49)

我的朋友

分类:

2009-11-19 09:55:16

The CSemaphore constructor have these param: lInitialCount and lMaxCount .Can you tell me what 's lInitialCount used for? lMaxCount is the maximum number of thread that could access critical section,isn't it? I found this, but i can't understand why we need lInitialCount :

CSemaphore(
LONG lInitialCount = 1,
LONG lMaxCount = 1,
LPCTSTR pstrName = NULL,
LPSECURITY_ATTRIBUTES lpsaAttributes = NULL
);

lInitialCount

The initial usage count for the semaphore. Must be greater than or equal to 0, and less than or equal to lMaxCount .

lMaxCount

The maximum usage count for the semaphore. Must be greater than 0.

pstrName

The name of the semaphore. Must be supplied if the semaphore will be accessed across process boundaries. If NULL, the object will be unnamed. If the name matches an existing semaphore, the constructor builds a new CSemaphore object which references the semaphore of that name. If the name matches an existing synchronization object that is not a semaphore, the construction will fail.

lpsaAttributes

Security attributes for the semaphore object. For a full description of this structure, see in the Platform SDK .

---------------------------------------------
Initial count is the number of threads that have initial access. This is usually set to the same number as the maximum count.
Each time a thread accesses the locked section, the initial count is decremented by 1.
When the initial count becomes 0, no more threads are then given access to that section.
When a thread releases the semaphore, the initial count is incremented by 1.
Here is an example - 

 

---------------------------------------------------

1 CSemaphore 信号量的用法,可以看上面链接msdn的例子,说的比较清楚

2 CSemaphore的初始化有个疑问。lInitialCount 可以看做资源的数目,也就是可用信号的数目。lMaxCount 是信号量的最大使用数目。lInitialCount 应该小于lMaxCount ,从逻辑上时可以理解的。但是有个问题,既然lInitialCount 已经制定了初始的信号个数,为什么还要加上个lMaxCount ?很明显lMaxCount 必须大于lInitialCount

阅读(307) | 评论(0) | 转发(0) |
0

上一篇:CSemaphore

下一篇:非模式窗口创建的sample

给主人留下些什么吧!~~