使用 EnterCriticalSection 时出现的问题
我需要使用多个线程中的数组,因此我使用 CRITICAL SECTION 为其提供对数据的独占访问权限。
这是我的模板:
#include "stdafx.h"
#ifndef SHAREDVECTOR_H
#define SHAREDVECTOR_H
#include <vector>
#include <windows.h>
template<class T>
class SharedVector {
std::vector<T> vect;
CRITICAL_SECTION cs;
SharedVector(const SharedVector<T>& rhs) {}
public:
SharedVector();
explicit SharedVector(const CRITICAL_SECTION& CS);
void PushBack(const T& value);
void PopBack();
unsigned int size() const;
T& operator[](int index);
virtual ~SharedVector();
};
template<class T>
SharedVector<T>::SharedVector() {
InitializeCriticalSection(&cs);
}
template<class T>
SharedVector<T>::SharedVector(const CRITICAL_SECTION& r): cs(r) {
InitializeCriticalSection(&cs);
}
template<class T>
void SharedVector<T>::PushBack(const T& value) {
EnterCriticalSection(&cs);
vect.push_back(value);
LeaveCriticalSection(&cs);
}
template<class T>
void SharedVector<T>::PopBack() {
EnterCriticalSection(&cs);
vect.pop_back();
LeaveCriticalSection(&cs);
}
template<class T>
unsigned int SharedVector<T>::size() const {
EnterCriticalSection(&cs);
unsigned int result = vect.size();
LeaveCriticalSection(&cs);
return result;
}
template<class T>
T& SharedVector<T>::operator[](int index) {
EnterCriticalSection(&cs);
T result = vect[index];
LeaveCriticalSection(&cs);
return result;
}
template<class T>
SharedVector<T>::~SharedVector() {
DeleteCriticalSection(&cs);
}
编译时,我在调用 EnterCriticalSection(&cs)
和 LeaveCriticalSection(&cs)
时遇到这样的问题:
'EnterCriticalSection' : cannot convert parameter 1 from 'const CRITICAL_SECTION *' to 'LPCRITICAL_SECTION'
我不知道是什么是错的。 也许你能看到。 因为我一直都是这么用的,而且还好。 包含 windows.h
I need to work with array from several threads, so I use CRITICAL SECTION to give it an exclusive access to the data.
Here is my template:
#include "stdafx.h"
#ifndef SHAREDVECTOR_H
#define SHAREDVECTOR_H
#include <vector>
#include <windows.h>
template<class T>
class SharedVector {
std::vector<T> vect;
CRITICAL_SECTION cs;
SharedVector(const SharedVector<T>& rhs) {}
public:
SharedVector();
explicit SharedVector(const CRITICAL_SECTION& CS);
void PushBack(const T& value);
void PopBack();
unsigned int size() const;
T& operator[](int index);
virtual ~SharedVector();
};
template<class T>
SharedVector<T>::SharedVector() {
InitializeCriticalSection(&cs);
}
template<class T>
SharedVector<T>::SharedVector(const CRITICAL_SECTION& r): cs(r) {
InitializeCriticalSection(&cs);
}
template<class T>
void SharedVector<T>::PushBack(const T& value) {
EnterCriticalSection(&cs);
vect.push_back(value);
LeaveCriticalSection(&cs);
}
template<class T>
void SharedVector<T>::PopBack() {
EnterCriticalSection(&cs);
vect.pop_back();
LeaveCriticalSection(&cs);
}
template<class T>
unsigned int SharedVector<T>::size() const {
EnterCriticalSection(&cs);
unsigned int result = vect.size();
LeaveCriticalSection(&cs);
return result;
}
template<class T>
T& SharedVector<T>::operator[](int index) {
EnterCriticalSection(&cs);
T result = vect[index];
LeaveCriticalSection(&cs);
return result;
}
template<class T>
SharedVector<T>::~SharedVector() {
DeleteCriticalSection(&cs);
}
While compiling I have such a problem for calling EnterCriticalSection(&cs)
and LeaveCriticalSection(&cs)
:
'EnterCriticalSection' : cannot convert parameter 1 from 'const CRITICAL_SECTION *' to 'LPCRITICAL_SECTION'
I do not know what is wrong. May be you can see. Just because I always used it this way and it was alright. windows.h
is included
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只需将
cs
声明为:或者删除
size()
上的 const 子句。进入临界区会修改
CRITICAL_SECTION
,离开则会再次修改它。 由于进入和离开关键部分不会使size()
方法调用在逻辑上成为非const
,因此我建议将其声明为const
>,并使cs
可变
。mutable
正是针对这种情况而引入的。另外 - 看看 Martin York 和 Joe Mucchiello 的建议 - 尽可能使用 RAII 来处理需要清理的任何类型的资源。 这对于关键部分和指针和文件句柄一样有效。
Just declare
cs
as:or else remove the const clause on
size()
Entering a critical section modifies the
CRITICAL_SECTION
, and leaving modifies it again. Since entering and leaving a critical section doesn't make thesize()
method call logically non-const
, I'd say leave it declaredconst
, and makecs
mutable
. This is the type of situationmutable
was introduced for.Also - take a look at Martin York's and Joe Mucchiello's suggestions - use RAII whenever possible to deal with any kind of resources that need to be cleaned up. This works just as well for critical sections as it does for pointers and file handles.
另外上面的代码不是异常安全的。
不保证push_back() pop_back()不会抛出异常。 如果他们这样做,他们将使您的关键部分永久锁定。 您应该创建一个储物柜类,在构造时调用 EnterCriticalSection(),在销毁时调用 LeaveCriticalSection()。
这也使得你的方法更容易阅读。 (见下文)
您应该担心的另一件事。 确保当您销毁该对象时您拥有所有权,并且在销毁期间没有其他人试图获得所有权。 希望您的 DestoryCriticalSection() 能够解决这个问题。
Also the code above is not Exception safe.
There is no guarantee that push_back() pop_back() will not throw. If they do they will leave your critical section permanently locked. You should create a locker class that calls EnterCriticalSection() on construction and LeaveCriticalSection() on destruction.
Also this makes your methods a lot easier to read. (see below)
Another thing you should worry about. Is making sure that when you destroy the object you have ownership and that nobody else tries to take ownership during destruction. Hopefully your DestoryCriticalSection() takes care of this.
我更喜欢使用单独的 Acquisition 对象而不是代码。 当 Enter 和 Leave 调用之间发生异常时,您的代码如果很脆弱:
那么在您的类方法中,您将其编码为:
I prefer using a separate Acquisition object over your code. Your code if fragile when an exception occurs between the Enter and Leave calls:
Then in your class methods you would code it as:
EnterCriticalSection
不接受 const 参数。 顺便说一句,这是一个编译错误,而不是链接错误...另外,您确定要将关键部分传递到您的构造函数中,然后让该构造函数执行
InitializeCriticalSection
调用吗? 如果你想共享你的关键部分,我想你应该先初始化它,然后再分发它。EnterCriticalSection
doesn't take a const argument. That's a compilation error, BTW, not a linking error...Also, are you sure you want to pass in a critical section into your ctor, and then have the ctor perform the
InitializeCriticalSection
call? If you want to share your critical section, I suppose you'd initialize it first, and then hand it out.我看到您声明了一个空的复制构造函数:
我确信您知道,这个函数什么也不做,而且它还使
cs
未初始化。 由于您的类包含CRITICAL_SECTION
的实例,因此您必须确保禁止复制构造函数和赋值运算符调用,除非您要完全实现它们。 您可以通过在类的private
部分中放置以下声明来实现此目的:这可以防止编译器自动生成这些方法的错误版本,也可以防止您在编写的其他代码中调用它们(因为这些只是声明,而不是带有
{}
代码块的定义)。另外,正如 Arnout 提到的,采用
CRITICAL_SECTION&
的构造函数论证似乎是错误的。 您的实现所做的是将传入的关键部分复制到cs
(这对于CRITICAL_SECTION
来说不是有效的操作),然后调用InitializeCriticalSection(& ;cs)
它会覆盖您刚刚所做的副本并创建一个新的关键部分。 对于传入关键部分的调用者来说,这似乎通过有效地忽略传入的内容而做了错误的事情。I see you have declared an empty copy constructor:
As I'm sure you are aware, this function does nothing, and it also leaves
cs
uninitialised. Because your class contains an instance of aCRITICAL_SECTION
, you must be sure to disallow copy constructor and assignment operator calls unless you are going to implement them completely. You can do this by placing the following declarations in theprivate
section of your class:This prevents the compiler from auto-generating incorrect versions of these methods, and also prevents you from calling them in other code you write (because these are only declarations, but not definitions with
{}
code blocks).Also, as Arnout mentioned, the constructor that takes a
CRITICAL_SECTION&
argument seems wrong. What your implementation does is copy the passed-in critical section tocs
(which is not a valid thing to do with aCRITICAL_SECTION
), then callsInitializeCriticalSection(&cs)
which overwrites the copy you just did and creates a new critical section. To the caller who passed in a critical section, this seems to do the wrong thing by effectively ignoring whatever was passed in.所以,访问权限有问题。
我将 size() 方法设置为非常量,现在可以了。
so, it`s something wrong with access rights.
I made size() method non-const and now it is ok.