【问题标题】:C++ 11 thread simple exampleC++ 11 线程简单示例
【发布时间】:2014-01-17 12:41:34
【问题描述】:

我是 C++ 新手,我正在研究一些 C++ 跨平台线程教程。我正在调查这个:http://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/

并试图执行以下代码:

#include <iostream>
#include <thread>

static const int num_threads = 10;

//This function will be called from a thread

void call_from_thread(int tid) {
    std::cout << "Launched by thread " << tid << std::endl;
}

int main() {
    std::thread t[num_threads];

    //Launch a group of threads
    for (int i = 0; i < num_threads; ++i) {
        t[i] = std::thread(call_from_thread, i);
    }

    std::cout << "Launched from the main\n";

    //Join the threads with the main thread
    for (int i = 0; i < num_threads; ++i) {
        t[i].join();
    }

    return 0;
}

我得到的输出如下,我不明白为什么:

syd@syd-HP-Compaq-dx7500-Microtower:~/Desktop$ ./ref
Launched by thread Launched by thread Launched by thread Launched by thread Launched by thread 201
Launched by thread 5

Launched by thread 6
4
Launched by thread 7
3

Launched by thread 8
Launched from the main
Launched by thread 9

我知道数字每次都是随机的,但有时我没有显示任何数字,我想知道为什么?

【问题讨论】:

  • std::cout &lt;&lt; "something" 没有逐行或operator&lt;&lt; 调用同步。这就是为什么多个线程的输出可以交错的原因。
  • 更准确地说,C++ 标准在 [iostreams.objects.overview]/4 中为标准流指定“并发访问同步标准 iostream 对象的格式化和未格式化输入和输出函数或标准多个线程的 C 流不应导致数据竞争。[注意:如果用户希望避免交错字符,则仍必须同步多个线程对这些对象和流的并发使用。- 结束注意 ]"
  • 类似 std::string str = "Launched by thread " 然后 str.append(tid) 和 then 打印它会正常工作
  • @James:不是完全没用,只是有点不方便,迫使您在流式传输之前将整行(或您想要组合在一起的任何内容)格式化为单个字符串。如果您不关心类型安全或对用户定义类型的支持,printf 可能更方便。
  • @LanPac:这也不能保证。

标签: c++ multithreading c++11 iostream


【解决方案1】:

您需要做的就是添加一个互斥体并将其锁定在正确的位置:

std::mutex mtx;

-

void call_from_thread(int tid) {
    mtx.lock();
-----------------------------------------------------------
    std::cout << "Launched by thread " << tid << std::endl;
-----------------------------------------------------------
    mtx.unlock();
}

-

mtx.lock();
-----------------------------------------------------------
std::cout << "Launched from the main\n";
-----------------------------------------------------------
mtx.unlock();

refer to this

【讨论】:

    【解决方案2】:

    他们都在那里。它们只是被弄乱了,因为控制台输出以模糊的随机顺序发生。

    特别是看看第一行输出的结尾。

    【讨论】:

      【解决方案3】:

      在 IO(cout) 中存在竞争条件

      std::cout << "Launched by thread " << tid << std::endl;
      

      实际上,cout ("Launched by thread", tid, std::endl) 排序没有任何保证。它的行为是这样的:

      std::cout << "Launched by thread " ;
      cout<< tid ;
      cout<< std::endl;
      

      您可以将 call_from_thread 更改为:

      void call_from_thread(int tid) {
          std::cout << std::string("Launched by thread " + std::to_string(tid) + "\n");
      }
      

      下一点在

      t[i] = std::thread(call_from_thread, i);
      

      当你创建一个线程时,函数call_from_thread会被调用。

      所以还是搬家吧

      std::cout << "Launched from the main\n";
      

      之前

      //Launch a group of threads
      for (int i = 0; i < num_threads; ++i) {
          t[i] = std::thread(call_from_thread, i);
      }
      

      您也可以使用holders:

      mutex g_i_mutex;  // protects cout
      
      void call_from_thread(int tid) {
          lock_guard<mutex> lock(g_i_mutex);
          cout << "Launched by thread " ;
          cout<< tid ;
          cout<< std::endl;
      } 
      

      【讨论】:

        【解决方案4】:

        刷新流应该可以解决问题:)

        下一行:std::cout &lt;&lt; "Launched by thread " &lt;&lt; tid &lt;&lt; std::endl;

        更改为:std::cout &lt;&lt; "Launched by thread " &lt;&lt; tid &lt;&lt; std::endl &lt;&lt; std::flush;

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-03-27
          • 2012-01-11
          • 1970-01-01
          • 2013-04-18
          • 1970-01-01
          • 2021-11-25
          相关资源
          最近更新 更多