- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的线程代码中存在异常问题。基本上这是一个生产者-消费者问题,不同之处在于消费者必须先清空自己的队列,然后再从主队列消费,此外,根据值,他们将数字添加到另一个消费者队列或对其进行处理。
代码按预期工作,但在随机运行一段时间后会抛出以下错误:
在抛出“std::system_error”实例后调用终止
what(): 不允许操作
我的想法用完了,有时它处理 6 个值,其他的 600 个。
提前致谢!
#include <iostream>
#include <thread>
#include <mutex>
#include <fstream>
#include <queue>
#include <cstdlib>
#include <condition_variable>
using namespace std;
queue<int> main_queue;
queue<int> baker_queue;
queue<int> groceries_queue;
mutex mutex1;
void clients_arrival(){
int random;
int random_2;
int i=0;
srand((unsigned)time(NULL));
while(true){
unique_lock<mutex> locker(mutex1);
random = 1+(rand()%3);
random_2 = 100+(rand()%1200);
main_queue.push(random);cout << "arrives client number "<<i+1<<" and has value "<<main_queue.back() << endl;
locker.unlock();
std::this_thread::sleep_for(chrono::milliseconds(random_2));
i++;
}
}
void baker_reception() {
while (true) {
unique_lock<mutex> locker(mutex1);
if (!main_queue.empty()) {
if (baker_queue.empty()) {
int a = main_queue.front();
main_queue.pop();
baker_queue.push(a);
cout << "baker empty, it removed an " << baker_queue.front() << endl;
if (a == 2) {
int b = a;
groceries_queue.push(b);
baker_queue.pop();
}
if (a == 3) {
int b = 2;
groceries_queue.push(b);
baker_queue.pop();
}
if (a == 1) {
baker_queue.pop();
}
locker.unlock();
std::this_thread::sleep_for(chrono::milliseconds(400));
}
if (!baker_queue.empty()) {
cout << "baker not empty, removed an " << baker_queue.front() << endl;
int a = baker_queue.front();
if (a == 2) {
int b = a;
groceries_queue.push(b);
baker_queue.pop();
}
if (a == 3) {
int b = 2;
groceries_queue.push(b);
baker_queue.pop();
}
if (a == 1) {
baker_queue.pop();
}
locker.unlock();
std::this_thread::sleep_for(chrono::milliseconds(400));
}
}
else {
locker.unlock();
std::this_thread::sleep_for(chrono::milliseconds(400));
}
}
}void groceries_reception() {
while (true) {
unique_lock<mutex> locker(mutex1);
if(!main_queue.empty()){
if (groceries_queue.empty()) {
int a = main_queue.front();
main_queue.pop();
groceries_queue.push(a);
cout << "Groceries empty, it removed an " << groceries_queue.front() << endl;
if (a == 1) {
int b = a;
baker_queue.push(b);
groceries_queue.pop();
}
if (a == 3) {
int b = 1;
baker_queue.push(b);
groceries_queue.pop();
}
if (a == 2) {
groceries_queue.pop();
}
locker.unlock();
std::this_thread::sleep_for(chrono::milliseconds(400));
}
if (!groceries_queue.empty()) {
cout << "Groceries not empty, removed an " << groceries_queue.front() << endl;
int a = groceries_queue.front();
if (a == 1) {
baker_queue.push(a);
groceries_queue.pop();
}
if (a == 3) {
int b = 1;
baker_queue.push(b);
groceries_queue.pop();
}
if (a == 2) {
groceries_queue.pop();
}
locker.unlock();
std::this_thread::sleep_for(chrono::milliseconds(400));
}
}
else {
locker.unlock();
std::this_thread::sleep_for(chrono::milliseconds(400));
}
}
}
int main() {
std::thread client(clients_arrival);
std::thread groceries(groceries_reception);
std::thread baker(baker_reception);
client.join();
baker.join();
groceries.join();
}
最佳答案
std::system_error
由 std::thread
构造函数或 unlock
之一抛出。
既然你说程序正确启动了线程,那么后者就是问题所在。 unlock
抛出 std::system_error
如果没有关联的互斥体或互斥体未锁定。
因此在您的情况下,您正在尝试解锁未锁定的互斥体,例如因为你之前已经解锁了它。
查看 groceries_reception
。如果您采用第一个 if (groceries_queue.empty())
分支,您将在 sleep 前解锁互斥量。休眠后继续执行,下一个 if
将被测试,即 if (!groceries_queue.empty())
。因为其他线程之间可能修改了groceries_queue
,所以现在也可以使用这个分支。但是在这个分支中,您再次调用了 locker.unlock()
。这是抛出异常的地方,因为您实际上不再持有锁。
同样的问题存在于另一个函数中。如果只想执行其中一个分支,请使用 else if
。
然而,通过作用域末尾的析构函数解锁锁会更简洁。如果你想在解锁后休眠,只需在锁的作用域之外做一个人工作用域:
{
unique_lock<mutex> locker(mutex1);
// do something with lock, don't call unlock
}
std::this_thread::sleep_for(chrono::milliseconds(400));
或者如果在第一个循环迭代开始时发生很好的话,你可以在获取锁之前先休眠:
std::this_thread::sleep_for(chrono::milliseconds(400));
unique_lock<mutex> locker(mutex1);
// do something with lock, don't call unlock
关于c++ - 线程 'std::system_error' what() 操作不允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53282723/
我正在尝试编写一个多线程记录器,当我测试要写出的行队列是否为空时,出现带有无效参数的std::system_error。构造unique_lock时会发生这种情况。如果我通过std::try_to_l
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why does this simple std::thread example not work? 代码:
这个问题在这里已经有了答案: Why is locking a std::mutex twice 'Undefined Behaviour'? (2 个答案) 关闭 7 年前。 这是代码 mutex
C++11 引入了包含处理错误代码的通用系统的 header 。一个 std::error_code是一个包含 int 的元组、错误代码和对 std::error_category 的引用,它定义了错
我正在尝试运行我的程序,但在几次运行中我遇到了一个错误: terminate called after throwing an instance of 'std::system_error' wh
我的线程代码中存在异常问题。基本上这是一个生产者-消费者问题,不同之处在于消费者必须先清空自己的队列,然后再从主队列消费,此外,根据值,他们将数字添加到另一个消费者队列或对其进行处理。 代码按预期工作
当我运行我的代码时: nb workers = 12 I'm i : 0 HELLO I'm func1 BYE I'm func2 terminate called after throwing a
最近我开始研究 Grpc。在 Grpc C++ 编译中出现以下错误,不确定是什么导致了这个问题。 我不是 C++ 背景,任何帮助对我来说都会非常有用。 [HOSTLD] 链接/home/test/gr
我开发了一种使用推力 的算法。我的办公室计算机有一张支持 CUDA 的卡,其架构为: --- General information about Device 0 Name: Quadro 2000
我有这个抛出异常的代码(非常类似于 what is suggested here): int accept4(int sockfd, sockaddr *addr, socklen_t *addrle
我在具有 2.1 计算能力的 Nvidia 卡上使用 Thrust 运行蒙特卡洛模拟。如果我尝试一次 transform_reduce 整个 device_vector,我会收到以下错误。这不是耗尽设
中定义的异常(例如 std::logic_error 、 std::runtime_error 及其子类,例如 std::system_error )具有需要字符串参数的构造函数,例如: domai
我正在开发一个系统,该系统旨在使用名为 error_code、error_condition 和 error_category 的类——一个新的方案std:在 C++11 中,尽管目前我实际上正在使用
我看了一篇深思熟虑的series of blog posts关于新 C++11 中的 header 。它说标题定义了 error_code表示操作(例如系统调用)返回的特定错误值的类。它说标题定义了
这个问题在这里已经有了答案: What are the correct link options to use std::thread in GCC under linux? (5 个回答) 关闭9年
我正在使用 tensorflow 训练 resNet50,使用具有以下属性的共享服务器: Ubuntu 16.04 3 gtx 1080 gpu tensorflow 1.3 python 2.7 但
我在Android应用程序中使用boost inide并获得随机SIGABRT: "terminating with uncaught exception of type boost::wra
我使用从 std::system_error 继承的类进行错误处理,我想控制调用 what() 时返回的内容。原因:标准(C++11 和 C++1y CD 草案 - N3690,下面的 § 引用是后者
如果我调用通过 GetLastError 报告错误的 Win32 函数,例如 RegisterClassEx ,如何为该错误抛出 std::system_error ? 最佳答案 检查 GetLast
我正在尝试使用 system_error 工具来处理我的库中的错误。我将简要讨论该库的结构,以防您发现它对您有所帮助:该库的 namespace 称为 commons,在此之下我还有另一个 names
我是一名优秀的程序员,十分优秀!