【问题标题】:How to compare outputs from one thread with outputs from another thread in C++如何在 C++ 中比较一个线程的输出与另一个线程的输出
【发布时间】:2021-11-23 16:40:00
【问题描述】:

我用 C++ 编写了一个程序来并行化使用 ~ 运算符转换数组元素的过程。元素是一和零。所以我希望程序将 1 转换为 0,反之亦然。代码正在运行,现在我想将线程 0 的输出与线程 1 的输出进行比较,但我不知道该怎么做。我的代码如下所示:

int main()
{
    omp_set_num_threads(2); 
    char array[6]={1,0,1,0,1,0,};


    #pragma omp parallel
    {
       
          cout << "Number of available threads: " << omp_get_num_threads() << endl;

              cout << "Current thread number is: " << omp_get_thread_num() << endl;

                for (int i=0; i<sizeof(array); i++)

            {   
                bitset<M> bitarray(array[i]);

                cout << ~bitarray << " ";
                    
    }   cout<< endl<<endl;

     }
            }

输出如下:

Number of available threads: 2
Current thread number is: 0
11111110 11111111 11111110 11111111 11111110 11111111 

Number of available threads: 2
Current thread number is: 1
11111110 11111111 11111110 11111111 11111110 11111111 

如何比较并确认这两个输出是否相同?

【问题讨论】:

  • 您可以通过自动格式化程序运行您的代码吗?毕竟,您希望人们阅读它,还是?作为这里的新用户,也可以使用tour 并阅读How to Ask

标签: c++ multithreading


【解决方案1】:

我可以建议 2 个选项(通常彼此非常相似)

  1. 在每个线程中将答案输出到另一个文件。当所有线程都完成后,只需比较文件的内容
  2. 创建一个std::stringstream 数组,在其中输出所需的变量,并比较这些字符串流的内容

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-22
    • 2011-10-12
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多