Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用threads和this_thread::yield确定打印顺序_C++_Multithreading_C++11_Std - Fatal编程技术网

C++ 使用threads和this_thread::yield确定打印顺序

C++ 使用threads和this_thread::yield确定打印顺序,c++,multithreading,c++11,std,C++,Multithreading,C++11,Std,这会正确地拉入文本文件,但它不会以正确的顺序输出它,我需要一个线程,但当我尝试实现它时,它没有工作。每次程序运行时,它都会以随机顺序显示誓言和反对,即使屈服函数已就位 #include <iostream> #include <iomanip> #include <fstream> #include <string> #include <stdlib.h> #include <thread> #include <vec

这会正确地拉入文本文件,但它不会以正确的顺序输出它,我需要一个线程,但当我尝试实现它时,它没有工作。每次程序运行时,它都会以随机顺序显示誓言和反对,即使屈服函数已就位

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <thread>
#include <vector>
using namespace std;

void cons(string c){
//Prints consanants
cout <<"CONS:   " << c << endl;
this_thread::yield();

}

void vow(string v){
//Prints vowels
// allowing other ready threads to run
    this_thread::yield(); 
   cout <<"VOW:    " << v << endl;

}

int main() {
//Creates an array of 100
string words[100];
//Creates a starting point for i
int i = 0;
//string called x
string s;
//Takes in a file
ifstream inFile;
//Creates a vector of threads to print
vector <thread> PrintingThreads;
//Opens up the text file "phrase.txt"
inFile.open("phrase.txt");
//If It is not able to open the file
if (!inFile) {
//Display error message
   cout << "Unable to open specified file";
   //Exit with an error(1)
   exit(1); 
}
while (inFile >> s) {
words[i]=s;
i++;
}
//cycle 
for (int l=0; l<i; l++)
   {
   char first (words[l][0]);

   if ((first == 'a') || (first  == 'e') || (first  == 'i') || (first  == 'o') || (first  == 'u')||(first == 'A') || (first  == 'E') || (first  == 'I') || (first  == 'O') || (first  == 'U'))
       {
        /
       PrintingThreads.push_back(thread(vow,words[l]));

       }   


       else
       {


       PrintingThreads.push_back(thread(cons,words[l]));

       }
   }// loop with a range variable
   for (thread& t: PrintingThreads) 
       t.join();

   inFile.close();
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
无效cons(字符串c){
//印刷品消费者

cout有三个问题:

  • 您不能使用
    this_thread::yield
    来确定线程的执行顺序,因为在一个特定线程中调用它,同时建议将优先级赋予其他线程,并不保证在所有其他线程完成之前执行不会返回到当前线程(§30.3.2/2),它只为实现提供了重新调度的机会,但在运行时由线程调度机制决定是否重新调度,以及何时返回到当前线程

    因此,当遇到以元音开头的行时,可能会发生以下情况:a)它启动一个元音处理线程,b)执行立即切换到该线程,并处理
    yield
    命令,c)实现有机会重新调度线程,使其切换回主线程,d)主线程处理另一行(可能再次是元音行),e)为其创建新线程新行,f)应用
    yield
    命令,实现重新调度执行并切换到另一个线程,g)实现选择第一行仍然未完成的元音线程,因此打印该元音,完成该线程,h)实现然后切换回第二个元音线程d也完成了(仍然在遇到任何辅音之前),等等

    这只是事情发生的一种可能的顺序。第二个元音可能在第一个元音之前结束,或者它们都要等到一个辅音线程开始并在两个元音线程之前结束。顺序不是由
    yield
    决定的

  • 即使使用
    yield
    也会保证等到所有其他当前运行的线程都完成(实际上不是!),这将有两个问题:a)在执行元音线程时,任何未来的辅音线程都还不存在,因为您一个接一个地创建线程,因此元音线程将没有任何等待的内容;b)另一方面,当元音线程启动时,唯一保证存在的其他线程是主线程;但是,如果元音线程必须等到主线程完成,那么它将不得不永远等待,因为主线程的末尾包含一个
    join
    命令,该命令迫使它等待元音线程。两个线程将在永久死锁中等待对方

  • 在主程序结束时,您按照线程插入向量的顺序执行
    join
    命令。因此,即使我们假设
    yield
    会导致一个线程等待,直到另一个线程使用
    join
    来等待该线程(无论如何,这是完全错误的假设),它仍然不起作用,因为辅音线程也包含一个
    yield
    语句,所以任何正在连接的元音线程都必须等待它后面的辅音线程(按照字符串的原始顺序)完成,而它们不会这样做,因为它们的
    join
    命令还没有被调用


  • 如果您想保证输出在元音和辅音之间交替(或者,也可以打印所有辅音行,后跟所有元音行),最直接的方法是取消线程,只需将输入行放入两个数组(或向量),一个用于元音行,一个用于辅音行。最后,您可以轻松确定打印顺序,并且不会消耗比现在更多的内存(假设您将所有行存储在
    单词
    数组中).

    这样一个精心设计、实际上毫无用处的例子一定是家庭作业…@MitchWheat,自从家庭作业标记被火焰喷射器杀死后,它就毫无意义了。线程彼此异步运行。除非你使用互斥或信号量实现某种显式同步,否则它们之间没有固有的顺序。你为什么要使用线程,如果你想让它们按原始顺序打印?也许这就是这个练习的原因?我不能为此实现互斥或信号量,线程的原因是让vow和con打印出单词,但保留文本文件的原始顺序。@user1720205你想按原始顺序打印行吗?我想是的想要在元音和辅音之间交替。如果你想要原始顺序,为什么不一个一个地打印它们呢?我觉得这是一个便宜的解决方案,但我实现了