到底用不用CSingleLock和CMultiLock ?

力为
博客专家认证
2005-09-27 11:46:57
MSDN这样说的:
In order to use the synchronization classes CSemaphore, CMutex, CCriticalSection, and CEvent, you must create either a CSingleLock or CMultiLock object to wait on and release the synchronization object.

但又这样说的:
Alternatively, you can create a CSemaphore object stand-alone, and access it explicitly before attempting to access the controlled resource. This method, while clearer to someone reading your source code, is more prone to error.

前面说必须用CSingleLock 或 CMultiLock ,后面说可以不用。
但到底是用还是不用呢?
不用会容易发生什么错误呢?
...全文
167 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
力为 2005-10-13
  • 打赏
  • 举报
回复
再没有人讨论我就结帖了哦
DentistryDoctor 2005-09-27
  • 打赏
  • 举报
回复
比较一下:
//CCriticalSection m_csLock;

void Function()
{
CSingleLock lock(&m_csLock,TRUE);
...
}


void Function2()
{
m_csLock.Lock();
...
m_csLock.Unlock();
}

DentistryDoctor 2005-09-27
  • 打赏
  • 举报
回复
不是说必须用。
但CSingleLock/CMultiLock可以很方便的实现自旋锁。

15,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧