使用string和int(like)类型初始化模板类的静态成员

时间:2015-05-22 03:43:00

标签: c++ multithreading visual-studio-2010 templates static-members

我正在尝试实现一个监视器类来处理多线程。我必须使用Visual Studio 2010,所以对我来说没有c ++ 11。我可以通过创建全局变量来实现这一点,每个类型的值都可以在线程中读取/写入,并且每个变量都使用一个互斥量,这将是简单但更繁琐。

所以我想花时间在一个类中实现监视器概念。 这项工作已经完成了#34;在某种程度上,因为数据类型是float或int,但我想进一步实现monitor类以支持C标准类型和c ++类,例如字符串。

我的方法是让静态成员作为跨线程的共享内存工作,这样我唯一要做的就是在两个线程中实例化类,我会正确设置IPC。

事情是......我必须初始化静态成员并且我无法使它工作,因为字符串和int-ish类型(int,float等...)没有以相同的方式初始化。 / p>

这是我试图做的事情:

Left a :: Right a :: ...

工作正常如果我只声明了template<class T> class Monitor : protected Mutex { public: Monitor(bool Verbose); ~Monitor(); T read(DWORD wait_time=INFINITE); void write(T value, DWORD wait_time=INFINITE); //friend void operator= (Monitor& m1, Monitor& m2); private: static T container; }; template<class T> T Monitor<T>::container = (typeid(T) == typeid(string) ? "" : 0); Monitor<string> pMonitor2(VERB_ON);。声明两者都会引发我Monitor<int> pMonitor1(VERB_ON);

此外,在类中实现这种进程间通信概念的最佳方法是什么?

到目前为止的完整代码:

monitor.cpp:

error C2440: 'initializing' : cannot convert from 'const char *' to 'int'

monitor.h:

#include "monitor.h"

using namespace std;

HANDLE Mutex::hMutex = nullptr;
unsigned int Mutex::counter = 0;

Mutex::Mutex(bool V) {
    Verbose = V;
    if(hMutex == nullptr) {
        if(Verbose) cout << "Creating mutex\n";
        hMutex = CreateMutex(
            NULL,
            FALSE, 
            (LPCWSTR) "CONTMUTEX");
            counter++;
    }
    else{
        if(Verbose) cout <<"Mutex already created\n";
        counter++;
    }
}

Mutex::~Mutex() {
    counter--;
    if(counter == 0) {
        if(Verbose) cout << "Destroying mutex\n";
        CloseHandle(hMutex);
    }
    else {
        if(Verbose) cout << "Awaiting closure of all instances of Mutex\n";
    }
}

/*void operator= (Monitor& m1, Monitor& m2) {
    m1.write(m2.read());
}*/

main.cpp进行测试:

#ifndef MONITOR_INC
#define MONITOR_INC

//#include <vector>
#include <Windows.h>
#include <iostream>

using namespace std;

enum verb{VERB_OFF, VERB_ON};

class Mutex {
public:
    Mutex(bool Verbose);
    ~Mutex();
protected:
    bool Verbose;
    static HANDLE hMutex;
    static unsigned int counter;
};

template<class T>
class Monitor : protected Mutex {
public:
    Monitor(bool Verbose);
    ~Monitor();
    T read(DWORD wait_time=INFINITE);
    void write(T value, DWORD wait_time=INFINITE);
    //friend void operator= (Monitor& m1, Monitor& m2);
private:
    static T container;
};

template<class T>
T Monitor<T>::container = (typeid(T) == typeid(string) ? "" : 0);

template<class T>
Monitor<T>::Monitor(bool Verbose) : Mutex(Verbose) {
    if(Verbose) cout << "Monitor Constructor\n";
}

template<class T>
Monitor<T>::~Monitor() {
    if(Verbose) cout << "Monitor Destructor\n";
}

template<class T>
T Monitor<T>::read(DWORD wait_time) {
    T ret_val;
    WaitForSingleObject(hMutex, wait_time);
    ret_val = container;
    ReleaseMutex(hMutex);
    return ret_val;
}

template<class T>
void Monitor<T>::write(T value, DWORD wait_time) {
    WaitForSingleObject(hMutex, wait_time);
    container = value;
    ReleaseMutex(hMutex);
}

//void operator= (Monitor& m1, Monitor& m2);
#endif

我必须事先为任何不良做法道歉。自从我上次用C ++编写代码以来已经过去了8个月,我借此机会从我的C ++编码技巧中汲取了一些成果。

EDIT1

出于某种原因,

#include "monitor.h" #include <string> #include <sstream> int main() { Mutex pMutex1(VERB_ON); Mutex pMutex2(VERB_ON); Mutex pMutex3(VERB_ON); Monitor<int> pMonitor1(VERB_ON); cout << pMonitor1.read() << "\n"; pMonitor1.write(20); cout << pMonitor1.read() << "\n"; Monitor<string> pMonitor2(VERB_ON); cout << pMonitor2.read() << "\n"; pMonitor2.write("TURN DOWN FOR WHAT"); cout << pMonitor2.read() << "\n"; return 0; } 实际上并不适用于int,只有字符串。

1 个答案:

答案 0 :(得分:1)

您可以使用专业化:

template<class T> // general case
T Monitor<T>::container = 0;

template<>
std::string Monitor<std::string>::container = "";

或使用默认值

template<class T>
T Monitor<T>::container = T();