C++ 11 线程简单示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20842762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:21:31  来源:igfitidea点击:

C++ 11 thread simple example

c++multithreadingc++11iostream

提问by syd619

I'm new to c++ and I was looking into some c++ cross-platform thread tutorials. I was looking into this: http://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/

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

and was trying to execute the following code:

并试图执行以下代码:

#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;
}

The output I'm getting is the following and I can not understand why:

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

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

I understand that the numbers are random each time, but some times I get no numbers displayed and I wonder why?

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

采纳答案by Lightness Races in Orbit

They're all there. They're just mangled up because the console output happens in vaguely random orders.

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

In particular have a look at the end of the first line of output.

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

回答by Sway Jan

all you need to do is adding a mutex and lock it in the proper position:

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

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

参考这个

回答by Sam Mokari

There is a race condition in IO(cout) in

IO(cout) 中存在竞争条件

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

Actually, there is no guaranty to cout ("Launched by thread", tid, std::endl) sequencing. And it behaves like this:

实际上,并不能保证 cout(“由线程启动”、tid、std::endl)排序。它的行为是这样的:

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

You can change call_from_threadto :

您可以将call_from_thread更改为:

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

The next point is in

下一点是在

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

When you create a thread, at creation time the function call_from_threadwill be called.

创建线程时,将在创建时调用函数call_from_thread

So It is better to move

所以最好移动

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

Before

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

You also can use 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;
} 

回答by user1898781

Flushing stream should do the trick :)

冲洗流应该可以解决问题:)

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

以下行: std::cout << "Launched by thread " << tid << std::endl;

Change to: std::cout << "Launched by thread " << tid << std::endl << std::flush;

改成: std::cout << "Launched by thread " << tid << std::endl << std::flush;