使用EnterCriticalSection时出现问题 [英] Problems using EnterCriticalSection

查看:1153
本文介绍了使用EnterCriticalSection时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用来自多个线程的数组,因此我使用CRITICAL SECTION对其进行数据的独占访问。

这里是我的模板:

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);
}

在编译时,我遇到了这样的问题: EnterCriticalSection (& cs) LeaveCriticalSection(& 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不知道是什么错了。可能你可以看到。只是因为我总是用这种方式,它是好的。 windows.h 包含

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

推荐答案

> cs as:

Just declare cs as:

mutable CRITICAL_SECTION cs;

或者删除 size()

输入关键部分修改 CRITICAL_SECTION ,并重新修改。由于进入和离开临界区不会使 size()方法调用逻辑非 - const d假定它声明 const ,并使 cs mutable 。这是 mutable 的情况类型。

Entering a critical section modifies the CRITICAL_SECTION, and leaving modifies it again. Since entering and leaving a critical section doesn't make the size() method call logically non-const, I'd say leave it declared const, and make cs mutable. This is the type of situation mutable was introduced for.

另外,请参阅马丁约克 Joe Mucchiello 的建议 - 尽可能使用RAII处理任何需要清理的资源。这对于关键部分以及对指针和文件句柄都有效。

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.

这篇关于使用EnterCriticalSection时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆