代码不会提前通过条件变量的等待函数
Code won't advance past wait function for conditional variable
因此,我一直在努力更好地了解条件变量的工作原理,并且编写了以下代码来尝试从同一文本文件实现读取和写入:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <condition_variable>
#include <mutex>
#include <thread>
using namespace std;
mutex mtx;
condition_variable conditionVariable;
ifstream readFile;
ofstream writeFile;
bool doneReading=false;
bool doneWriting=false;
void readFromFile()
{
string line;
readFile.open("./testFile.txt");
if(readFile.is_open())
{
cout << "Successfully opened file!" << "\n";
}
else
{
cout << "Failed to open file..." << "\n";
}
cout << "The file contents are:" << "\n";
while(getline(readFile,line))
{
unique_lock<mutex> lock(mtx);
conditionVariable.wait(lock, [] () {return doneWriting;});
cout << line << "\n";
doneReading=true;
lock.unlock();
conditionVariable.notify_one();
}
readFile.close();
}
void writeToFile()
{
string input;
writeFile.open("./testFile.txt");
cout << "Enter something you want to write to the text file:" << "\n";
cin >> input;
cout << "Going to write " << input << " to the file" << "\n";
if(writeFile.is_open())
{
cout << "Successfully opened file!" << "\n";
unique_lock<mutex> lock2(mtx);
/////////PROGRAM WON'T ADVANCE PAST THIS LINE/////////////
conditionVariable.wait(lock2, [] () {return doneReading;});
cout << "After calling the wait function for the condition variable" << "\n";
writeFile << input;
doneWriting=true;
lock2.unlock();
conditionVariable.notify_one();
writeFile.close();
}
else
{
cout << "Failed to open file..." << "\n";
}
}
int main()
{
int i;
for(i=0;i<10;i++)
{
thread t1(readFromFile);
thread t2(writeToFile);
t1.join();
t2.join();
}
}
而且,我根据以下 example 对布尔变量的使用建模
cppreference.com(向下滚动到底部以查看示例代码)。但是,它与我传递给等待函数的谓词有关系,我不太确定它有什么问题。如果有人可以提供一些见解,那就太好了。谢谢!
初始状态为:
bool doneReading=false;
bool doneWriting=false;
readFromFile
对这些变量做的第一件事就是坐等 doneWriting
变成 true
conditionVariable.wait(lock, [] () {return doneWriting;});
writeFromFile
对这些变量做的第一件事就是坐等 doneReading
变成 true
conditionVariable.wait(lock2, [] () {return doneReading;});
这两个条件都不会成立。
请注意,cppreference 示例做了一些非常不同的事情:一个线程开始执行 cv.wait(lk, []{return ready;});
,而另一个线程开始执行 ready=true;
因此,我一直在努力更好地了解条件变量的工作原理,并且编写了以下代码来尝试从同一文本文件实现读取和写入:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <condition_variable>
#include <mutex>
#include <thread>
using namespace std;
mutex mtx;
condition_variable conditionVariable;
ifstream readFile;
ofstream writeFile;
bool doneReading=false;
bool doneWriting=false;
void readFromFile()
{
string line;
readFile.open("./testFile.txt");
if(readFile.is_open())
{
cout << "Successfully opened file!" << "\n";
}
else
{
cout << "Failed to open file..." << "\n";
}
cout << "The file contents are:" << "\n";
while(getline(readFile,line))
{
unique_lock<mutex> lock(mtx);
conditionVariable.wait(lock, [] () {return doneWriting;});
cout << line << "\n";
doneReading=true;
lock.unlock();
conditionVariable.notify_one();
}
readFile.close();
}
void writeToFile()
{
string input;
writeFile.open("./testFile.txt");
cout << "Enter something you want to write to the text file:" << "\n";
cin >> input;
cout << "Going to write " << input << " to the file" << "\n";
if(writeFile.is_open())
{
cout << "Successfully opened file!" << "\n";
unique_lock<mutex> lock2(mtx);
/////////PROGRAM WON'T ADVANCE PAST THIS LINE/////////////
conditionVariable.wait(lock2, [] () {return doneReading;});
cout << "After calling the wait function for the condition variable" << "\n";
writeFile << input;
doneWriting=true;
lock2.unlock();
conditionVariable.notify_one();
writeFile.close();
}
else
{
cout << "Failed to open file..." << "\n";
}
}
int main()
{
int i;
for(i=0;i<10;i++)
{
thread t1(readFromFile);
thread t2(writeToFile);
t1.join();
t2.join();
}
}
而且,我根据以下 example 对布尔变量的使用建模 cppreference.com(向下滚动到底部以查看示例代码)。但是,它与我传递给等待函数的谓词有关系,我不太确定它有什么问题。如果有人可以提供一些见解,那就太好了。谢谢!
初始状态为:
bool doneReading=false;
bool doneWriting=false;
readFromFile
对这些变量做的第一件事就是坐等 doneWriting
变成 true
conditionVariable.wait(lock, [] () {return doneWriting;});
writeFromFile
对这些变量做的第一件事就是坐等 doneReading
变成 true
conditionVariable.wait(lock2, [] () {return doneReading;});
这两个条件都不会成立。
请注意,cppreference 示例做了一些非常不同的事情:一个线程开始执行 cv.wait(lk, []{return ready;});
,而另一个线程开始执行 ready=true;