在线程函数c ++中读取文件流
Reading in a file stream inside a thread function c++
我正在做一个 class 项目,运行 遇到了让我的线程完全读取文件的麻烦。我可以让他们说完一两个字,然后他们停下来,不输出任何东西。在我的 main
中,加入线程后,file.eof()
返回 true。有人对为什么会发生这种情况或如何解决它有任何建议吗?
(该项目是使用交替线程从文件中的短语 "sort" 元音和辅音;我不能使用互斥锁,这就是为什么有一个转弯变量)
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
位被设置也就不足为奇了(线程不能不再访问已关闭的 file
)。
纠正这个问题的正确方法是在你的线程返回后关闭 file
(即在 join()
中的每个线程 returns 之后)但是你的程序仍然存在一个主要缺陷,即您是如何设计 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
}
和cons
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
线程不会输入您的元音输出代码.这是一个设计流程,我相信您会知道如何处理。
如果您需要进一步的精确度,请注意不要对我的回答发表评论。
我正在做一个 class 项目,运行 遇到了让我的线程完全读取文件的麻烦。我可以让他们说完一两个字,然后他们停下来,不输出任何东西。在我的 main
中,加入线程后,file.eof()
返回 true。有人对为什么会发生这种情况或如何解决它有任何建议吗?
(该项目是使用交替线程从文件中的短语 "sort" 元音和辅音;我不能使用互斥锁,这就是为什么有一个转弯变量)
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
位被设置也就不足为奇了(线程不能不再访问已关闭的 file
)。
纠正这个问题的正确方法是在你的线程返回后关闭 file
(即在 join()
中的每个线程 returns 之后)但是你的程序仍然存在一个主要缺陷,即您是如何设计 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
}
和cons
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
线程不会输入您的元音输出代码.这是一个设计流程,我相信您会知道如何处理。
如果您需要进一步的精确度,请注意不要对我的回答发表评论。