gpt4 book ai didi

c++ - 在线程函数c++中读取文件流

转载 作者:太空狗 更新时间:2023-10-29 23:13:00 25 4
gpt4 key购买 nike

我正在处理一个类(class)项目,但在让我的线程完全读取一个文件时遇到了麻烦。我可以让他们说完一两个字,然后他们停下来,不输出任何东西。在我的 main 中,加入线程后,file.eof() 返回 true。有没有人对为什么会发生这种情况或如何解决它有任何建议?

(该项目是使用交替线程从文件中的短语中“排序”元音和辅音;我不能使用互斥锁,这就是为什么有一个转弯变量)

void cons(){
cout << "Turn is " << turn << endl; //outputs "Turn is 0"
while (!file.eof()){
if (turn == false){
char c = word.at(0);
if (c != 'A' || c != 'E' || c != 'I'|| c != 'O'|| c != 'U'){
cout << "Consonant Thread: " << word << '\n';
file >> word;
}
turn = true;
}
this_thread::yield();
}
}

void vowel(){
while (!file.eof()){
if (turn == true){
char c = word.at(0);
if (c == 'A' || c == 'E' || c == 'I'|| c == 'O'|| c == 'U'){
cout << "Vowel Thread: " << word << '\n';
file >> word;
}
turn = false; //keeps track of thread turn to make sure the correct one is running
}
this_thread::yield();
}
}

上面是我的一个函数的例子,另一个类似,但是用c == 'Vowel"

我的主图是这样的

ifstream file;
string word;
bool turn = true; //true for vowel, false for cons
bool done = false;

int main(int argc, char *argv[]) {
{
file.open("phrase.txt");
file >> word;

if (file.eof()){
done = true;
}

std::thread vowelThread(vowel); //passing vow function
std::thread consThread(cons); //passing cons function


cout << file.eof() << endl; //returns true

file.close();

vowelThread.join();
consThread.join();

cout << (file.eof()) << endl; //returns true
}

为了帮助澄清问题,下面是代码应该做什么的示例。在 .txt 文件 “phrase.txt” 中,有一个简单的短语,如 “Operating Systems Class Starts In The Evening” 输出应该是 Vowel Thread: Operating Consonant Thread: Systems Consonant Thread: Class 等等,直到文件被读完

任何帮助,包括有关线程的资源或帮助我的代码的建议,我们将不胜感激。提前致谢!

最佳答案

您代码中的主要问题是 main 线程关闭文件,而其他两个线程处理该文件,eof 位被设置也就不足为奇了(线程不能再访问关闭的文件)。纠正这个问题的正确方法是在您的线程返回后关闭文件(即在每个线程从join() 返回后)但是您的程序仍然留下主要缺陷是您如何设计 cons 函数和您的逻辑 || 使用,出于显而易见的原因,我已将其替换为 &&

  int main(int argc, char *argv[]) {
{
file.open("phrase.txt");
file >> word;

if (file.eof()){
done = true;
}

std::thread vowelThread(vowel); //passing vow function
std::thread consThread(cons); //passing cons function


cout << file.eof() << endl; //returns true

//file.close(); <- closes file while your threads work on it, thus the eof bit is set

vowelThread.join();
consThread.join();
file.close();
cout << (file.eof()) << endl; //returns true
}

缺点

void cons() {
cout << "Turn is " << turn << endl; //outputs "Turn is 0"
while (!file.eof()) {
if (turn == false) {
char c = word.at(0);
if (c != 'A' && c != 'E' && c != 'I' && c != 'O' && c != 'U') {
cout << "Consonant Thread: " << word << '\n';
file >> word;
}
turn = true;
}
//this_thread::yield();
}
}

附带说明一下,既然您已经看到了 cons 中的错误,您可能会理解为什么我坚持要您提供每个函数,而不是要求我们从一个函数推导出另一个函数。

还有最后一个问题(请注意,您的代码中有几个缺陷,为了保持主题我没有解决):最后一个词 Evening 没有输出,因为当 cons 打印 The 然后将 Evening 加载到 word 并且设置 eof 位,这意味着vowel 线程无法输入您的元音输出代码。这是一个设计流程,我相信您会知道如何处理。

如果您需要进一步的精确度,请注意不要对我的回答发表评论。

关于c++ - 在线程函数c++中读取文件流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42514101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com