我试图创建一个简单的订阅者 - 生产者模式,其中多个订阅者和一个生成器在不同的线程中运行(原因是要了解有关线程的更多信息).然而,我努力使互斥体成为生产者类的成员.代码如下:
class Producer {
private:
vector<Subscriber* > subs;
thread th;
int counter;
mutex mux;
public:
Producer() : counter(counter), th(&Producer::run, this) {};
void addSubscriber(Subscriber* s);
void notify();
void incrementCounter();
void run();
void callJoin(){th.join(); }
};
void Producer::run() {
for (int i = 0; i < 10; i++) {
incrementCounter();
this_thread::sleep_for(std::chrono::milliseconds(3000));
}
}
void Producer::addSubscriber(Subscriber* s) {
lock_guard<mutex> lock(mux);
subs.push_back(s);
}
void Producer::notify() {
lock_guard<mutex> lock(mux);
for (auto it = subs.begin(); it != subs.end(); ++it) {
(*it)->setCounterCopy(counter);
}
}
void Producer::incrementCounter() {
counter++;
notify();
}
Run Code Online (Sandbox Code Playgroud)
订阅者类:
class Subscriber {
private:
string name;
thread th;
atomic<int> counterCopy = 0;
public:
Subscriber(string name) : name(name), th(&Subscriber::run, this) {};
void run() {
while (true) {
cout << name << ": " << counterCopy << endl;
this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
void callJoin() { th.join(); }
void setCounterCopy(int counterCopy) { this->counterCopy = counterCopy; };
};
Run Code Online (Sandbox Code Playgroud)
主要:
int main() {
Producer p;
Subscriber s1("Sub1");
p.addSubscriber(&s1);
s1.callJoin();
p.callJoin();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这意味着lock_guard当订户被添加到向量时,阻止生产者同时通知向量中的订户.然而,这个例外是在通知的lock_guard中引发了Exception thrown at 0x59963734 (msvcp140d.dll) in Project1.exe: 0xC0000005: Access violation reading location 0x00000000.吗?有人知道这个例外可能是什么原因吗?如果将互斥锁设置为全局参数,则可以正常工作.随意评论代码的其他问题.线程对我来说都是新的.
所以这里发生的是一个初始化顺序古怪.
类成员按照在类中声明的顺序构造.在您的情况下,这意味着首先是订阅者的向量,然后是线程,然后是计数器(!),最后是互斥体.在构造函数中指定初始值设定项的顺序无关紧要.
但!构造线程对象需要启动线程,运行其功能.最终,这会导致使用互斥锁,可能在Producer构造函数到达实际初始化点之前.所以你最终使用的是一个尚未构造的互斥体,并且(不是它是你的问题的原因)也是一个尚未初始化的计数器.
一般而言,只要您有提及的成员初始化程序this或不同的类成员(包括调用成员函数),您就应该保持警惕.它设置用于访问未初始化对象的场景.
在您的情况下,只需将互斥锁和计数器成员移动到线程成员之前即可.