队列访问加锁的问题

UnrealImagecoffee 2006-07-03 01:06:09
一个 环形 队列, 多线程 访问, 需要 加锁 吗??
是在 类里 加锁 还是在 定义 变量的 时候 加锁
...全文
323 4 打赏 收藏 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
UnrealImagecoffee 2006-07-03
  • 打赏
  • 举报
回复
楼上 是我 需要的 谢谢
mynamelj 2006-07-03
  • 打赏
  • 举报
回复
/**********************************************************************
* Copyright (C) 2005 IWANTFLY studio
* Module Name: CCriticalQueue
* Description:
* Create Data: 2005-11-18 Update Data: 2005-11-18
* Author: LiJiun
* E-Mail: mailto:notoldtree@126.com
*
*********************************************************************/
#pragma once
#include <deque>

class CriticalSection
{
public:
CriticalSection()
{
::InitializeCriticalSection(&m_sect);
}
virtual ~CriticalSection()
{
::DeleteCriticalSection(&m_sect);
}
public:
void Unlock()
{
::LeaveCriticalSection(&m_sect);
}
void Lock()
{
::EnterCriticalSection(&m_sect);
}
protected:
CRITICAL_SECTION m_sect;
};


template <class _Ty,
class _Container = std::deque <_Ty> >
class CCriticalQueue
{
public:
typedef _Container container_type;
typedef typename _Container::value_type value_type;
typedef typename _Container::size_type size_type;

CCriticalQueue() : c()
{
}

explicit CCriticalQueue(const _Container& _Cont)
: c(_Cont)
{ // construct by copying specified container
}

~CCriticalQueue()
{
}

bool empty() const
{ // test if queue is empty
return (c.empty());
}

size_type size() const
{ // return length of queue
return (c.size());
}

value_type& front()
{ // return first element of mutable queue
return (c.front());
}

const value_type& front() const
{ // return first element of nonmutable queue
return (c.front());
}

value_type& back()
{ // return last element of mutable queue
return (c.back());
}

const value_type& back() const
{ // return last element of nonmutable queue
return (c.back());
}

void push(const value_type& _Val)
{ // insert element at beginning
m_CriticalSection.Lock();
c.push_back(_Val);
m_CriticalSection.Unlock();
}

void pop()
{ // erase element at end
m_CriticalSection.Lock();
c.pop_front();
m_CriticalSection.Unlock();
}

bool _Eq(const CCriticalQueue<_Ty, _Container>& _Right) const
{ // test for queue equality
return (c == _Right.c);
}

bool _Lt(const CCriticalQueue<_Ty, _Container>& _Right) const
{ // test if this < _Right for queues
return (c < _Right.c);
}
protected:
_Container c; // the underlying container
CriticalSection m_CriticalSection;

};

template<class _Ty,
class _Container> inline
bool operator==(const CCriticalQueue<_Ty, _Container>& _Left,
const CCriticalQueue<_Ty, _Container>& _Right)
{ // test for queue equality
return (_Left._Eq(_Right));
}

template<class _Ty,
class _Container> inline
bool operator!=(const CCriticalQueue<_Ty, _Container>& _Left,
const CCriticalQueue<_Ty, _Container>& _Right)
{ // test for queue inequality
return (!(_Left == _Right));
}

template<class _Ty,
class _Container> inline
bool operator<(const CCriticalQueue<_Ty, _Container>& _Left,
const CCriticalQueue<_Ty, _Container>& _Right)
{ // test if _Left < _Right for queues
return (_Left._Lt(_Right));
}

template<class _Ty,
class _Container> inline
bool operator>(const CCriticalQueue<_Ty, _Container>& _Left,
const CCriticalQueue<_Ty, _Container>& _Right)
{ // test if _Left > _Right for queues
return (_Right < _Left);
}

template<class _Ty,
class _Container> inline
bool operator<=(const CCriticalQueue<_Ty, _Container>& _Left,
const CCriticalQueue<_Ty, _Container>& _Right)
{ // test if _Left <= _Right for queues
return (!(_Right < _Left));
}

template<class _Ty,
class _Container> inline
bool operator>=(const CCriticalQueue<_Ty, _Container>& _Left,
const CCriticalQueue<_Ty, _Container>& _Right)
{ // test if _Left >= _Right for queues
return (!(_Left < _Right));
}
会思考的草 2006-07-03
  • 打赏
  • 举报
回复
如果在类里面加锁,需要判断线程ID,这样也可以。
DentistryDoctor 2006-07-03
  • 打赏
  • 举报
回复
如果都是只读,不用加锁;否则需要加锁。

类里 加锁 还是在 定义 变量的 时候 加锁?这句话是什么意思?

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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