C++ ';标准::系统错误';,生产者消费者C++;并发性
下面的生产者/消费者程序应该一次将一个字符传输到缓冲区,然后打印它。程序最初运行,但在使用者循环的第三次迭代中总是失败-pthread包含在编译时 该程序应该能够遍历“poem”变量,最终将整首诗逐字打印出来C++ ';标准::系统错误';,生产者消费者C++;并发性,c++,concurrency,condition-variable,C++,Concurrency,Condition Variable,下面的生产者/消费者程序应该一次将一个字符传输到缓冲区,然后打印它。程序最初运行,但在使用者循环的第三次迭代中总是失败-pthread包含在编译时 该程序应该能够遍历“poem”变量,最终将整首诗逐字打印出来 #include <iostream> #include <cmath> #include <cstdlib> #include <thread> #include <fstream> #include <chrono>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <thread>
#include <fstream>
#include <chrono>
#include <mutex>
#include <condition_variable>
#include <vector>
#include <queue>
using namespace std;
mutex mtx;
condition_variable cond_var;
int thread_ID = 1;
vector<char> txtImport(vector<char> output){
ifstream file("poem.txt");
char character;
while (file.get(character)){
output.push_back(character);
}
return output;
}
void producer(vector<char> poem, char* buffer, int poem_size) {
cout << "\n\nPROD- \n";
int poem_position = 68;
const int producer_ID = 1;
unique_lock<mutex> lock(mtx);
while(true){
cout << "\n1";
if(poem_position == poem_size) {exit(1);}
if(thread_ID!=producer_ID) {cond_var.wait(lock);}
else{
cout << "\nlock\n";
*buffer = poem[poem_position];
poem_position += 1;
thread_ID = 2;
cout << poem_position << " 2 \n";
cout << poem[poem_position] << " 4 \n";
cout << "CHAR: " << *buffer << " \n------\n\n";
lock.unlock();
cond_var.notify_all();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
}
void consumer(char* buffer){
cout << "\n\n -CONS\n";
const int consumer_ID = 2;
unique_lock<mutex> lock(mtx);
while(true){
cout << "\n one ";
if(thread_ID!=consumer_ID) {cond_var.wait(lock);}
else{
cout << "\n lock\n";
cout << " ->" << *buffer << " <-\n\n";
thread_ID = 1;
lock.unlock();
cond_var.notify_all();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
}
int main(void) {
vector<char> poem;
poem = txtImport(poem);
char buffer = 'z';
int poem_size = poem.size();
cout << "\n--MAIN--\n";
thread thread_two(consumer, &buffer);
thread thread_one(producer, poem, &buffer, poem_size);
thread_one.join();
thread_two.join();
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
互斥mtx;
条件变量条件变量;
int thread_ID=1;
矢量输入(矢量输出){
ifstream文件(“poem.txt”);
字符;
while(file.get(字符)){
输出。推回(字符);
}
返回输出;
}
无效生成程序(矢量诗、字符*缓冲区、整型诗大小){
你是否有未定义的行为
调用cond_var.wait
时,传递的互斥锁必须由当前线程锁定。它不会发生在代码中(第一次迭代除外)
你的情况:
unique_lock<mutex> lock(mtx);
while (true)
cond_var.wait(lock);
lock.unlock();
唯一锁(mtx);
while(true)
条件变量等待(锁定);
lock.unlock();
所以在while循环的第二次迭代中,当调用wait
时,lock
被解锁。错误
要修复此问题,请在循环时将唯一锁定
结构同时移动到和(消费者/生产者函数中的相同问题)
lock-std::unique_lock类型的对象,必须为
被当前线程锁定
from.请你澄清一下你所说的:'要修复,请在两个'@A.Simpson中移动唯一的锁结构'。我是说while(true){unique\u lock(mtx);…其余的代码1:1…}
。我用for/while名称键入。lock
是在while()中构造的你可以把代码清理到一个最小的可重复的例子中,你有很多代码没有使用或者什么都不做,变量传递是很不清楚的。对于初学者来说,TxTimPoT并不重要,写得不好,你在乎的是,为什么没有重复地重复向量。这是你第一次去C++吗?或者通过引用和RET来传递。urn nothing或bool表示失败,或者简单地返回一个向量以获取返回值优化并不传入任何内容(但可能文件名是您应该传入的…),如果您使用的是流,为什么要使用get?请使用运算符。
unique_lock<mutex> lock(mtx);
while (true)
cond_var.wait(lock);
lock.unlock();