Mutex 作为 class 的成员
Mutex as member of class
我正在尝试制作一个简单的订阅者-生产者模式,其中多个订阅者和一个生产者 运行 在不同的线程中(原因是要了解有关线程的更多信息)。然而,我正在努力使互斥锁成为生产者 class 的成员。代码如下:
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();
}
订户class:
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; };
};
主线:
int main() {
Producer p;
Subscriber s1("Sub1");
p.addSubscriber(&s1);
s1.callJoin();
p.callJoin();
return 0;
}
lock_guard
的目的是防止生产者在将订户添加到矢量时同时通知矢量中的订户。然而这个异常是在通知 Exception thrown at 0x59963734 (msvcp140d.dll) in Project1.exe: 0xC0000005: Access violation reading location 0x00000000.
的 lock_guard 中抛出的 有谁知道这个异常的原因是什么?如果将互斥量设置为全局参数,则效果很好。也可以随意评论代码的其他问题。线程对我来说是全新的。
所以这里发生的是一个古怪的初始化顺序。
Class 成员按照它们在 class 中声明的顺序构造。在您的情况下,这首先意味着订阅者向量,然后是线程,然后是计数器(!),最后是互斥体。在构造函数中指定初始值设定项的顺序无关紧要。
但是!构造线程对象需要启动线程,运行 它的功能。最终,可能会在 Producer
构造函数到达它实际初始化它的位置之前使用互斥锁。所以你最终使用了一个尚未构造的互斥锁,并且(不是它是你问题的原因)也是一个尚未初始化的计数器。
一般来说,当你有一个提到 this
或不同的 class 成员的成员初始值设定项时,你应该小心,包括调用成员函数。它设置了访问未初始化对象的场景。
在您的情况下,只需将互斥锁和计数器成员移动到线程成员之前就足够了。
我只想将此添加到@Sneftel 给出的答案中以供参考:
来自 CPP 标准 (N4713),突出显示相关部分:
15.6.2 Initializing bases and members [class.base.init]
...
13 In a non-delegating constructor, initialization proceeds in the following order:
(13.1) — First, and only for the constructor of the most derived class (6.6.2), virtual base classes are initialized in
the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes,
where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
(13.2) — Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list
(regardless of the order of the mem-initializers).
(13.3) — Then, non-static data members are initialized in the order they were declared in the class definition
(again regardless of the order of the mem-initializers).
(13.4) — Finally, the compound-statement of the constructor body is executed.
[ Note: The declaration order is mandated to ensure that base and member subobjects are destroyed in the reverse order of initialization. —end note ]
我正在尝试制作一个简单的订阅者-生产者模式,其中多个订阅者和一个生产者 运行 在不同的线程中(原因是要了解有关线程的更多信息)。然而,我正在努力使互斥锁成为生产者 class 的成员。代码如下:
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();
}
订户class:
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; };
};
主线:
int main() {
Producer p;
Subscriber s1("Sub1");
p.addSubscriber(&s1);
s1.callJoin();
p.callJoin();
return 0;
}
lock_guard
的目的是防止生产者在将订户添加到矢量时同时通知矢量中的订户。然而这个异常是在通知 Exception thrown at 0x59963734 (msvcp140d.dll) in Project1.exe: 0xC0000005: Access violation reading location 0x00000000.
的 lock_guard 中抛出的 有谁知道这个异常的原因是什么?如果将互斥量设置为全局参数,则效果很好。也可以随意评论代码的其他问题。线程对我来说是全新的。
所以这里发生的是一个古怪的初始化顺序。
Class 成员按照它们在 class 中声明的顺序构造。在您的情况下,这首先意味着订阅者向量,然后是线程,然后是计数器(!),最后是互斥体。在构造函数中指定初始值设定项的顺序无关紧要。
但是!构造线程对象需要启动线程,运行 它的功能。最终,可能会在 Producer
构造函数到达它实际初始化它的位置之前使用互斥锁。所以你最终使用了一个尚未构造的互斥锁,并且(不是它是你问题的原因)也是一个尚未初始化的计数器。
一般来说,当你有一个提到 this
或不同的 class 成员的成员初始值设定项时,你应该小心,包括调用成员函数。它设置了访问未初始化对象的场景。
在您的情况下,只需将互斥锁和计数器成员移动到线程成员之前就足够了。
我只想将此添加到@Sneftel 给出的答案中以供参考:
来自 CPP 标准 (N4713),突出显示相关部分:
15.6.2 Initializing bases and members [class.base.init]
...
13 In a non-delegating constructor, initialization proceeds in the following order:
(13.1) — First, and only for the constructor of the most derived class (6.6.2), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
(13.2) — Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
(13.3) — Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
(13.4) — Finally, the compound-statement of the constructor body is executed.
[ Note: The declaration order is mandated to ensure that base and member subobjects are destroyed in the reverse order of initialization. —end note ]