当我开始研究c++11中的线程时,我认为.join()
用于对主线程进行阻塞操作,而std::async()
用于运行非阻塞线程。
在我看来,这个答案很好地解释了std:async()
。https://stackoverflow.com/a/15035157/1770034
但我想更好地理解连接方法。我发现了几个类似的例子:https://stackoverflow.com/a/11229853/1770034,其中只有一个线程是从主线程创建的。
然后我找到了这个https://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;
}
我对此很好奇这部分就在这里:
//Join the threads with the main thread
for (int i = 0; i < num_threads; ++i) {
t[i].join();
}
如果主线程停止等待直到.join()
完成,那么循环如何运行以连接更多的线程?它工作的事实是伟大的!但是,为什么会起作用呢?
它为什么会这样工作?这是我对它是如何工作的印象。
如果它一直循环通过for循环,主线程什么时候才会被阻塞等待?
编辑
示例:
//Join the threads with the main thread
for (int i = 0; i < num_threads; ++i) {
t[i].join();
// --- Perform some long operation here ---
}
长时间的手术什么时候开始?
发布于 2017-03-30 12:57:58
当我贴出这个问题时,我不太明白多线程是怎么回事。
即使在C++中阅读有关主题的几篇教程时,它仍然会有点模糊。这是对实际发生的事情的解释。
为了保持简单,让我们使用以下代码:
#include <thread>
void my_function()
{
*** Some Code ***
}
int main()
{
std::thread threads[10]; //create 10 std::thread objects
for(int loop = 0; loop < 10; loop++)
{
threads[loop] = std::thread(my_function);
}
//Threads are executing on machine and main code is also running
for(int loop = 0; loop < 10; loop++)
{
threads[loop].join; //If threads[loop] is still running wait for it, otherwise keep moving forward.
}
return 0;
} //all 10 thread objects (threads) are going out of scope and their destructors are being called
在main的顶部(在主线程中),我们为堆栈上的10个线程对象创建空间。在第一个for循环中,我们实际上创建了一个std::thread
,它将在一个单独的线程上调用my_function
。
假设我们有一个四核处理器,并且一次只能在一个处理器上运行一个线程。(完全是假设!)主线程目前正在使用一个处理器,我们的10个线程中的一个将使用第二个处理器。其余9人中有1人将使用第3人,其余8人中有1人将使用第4人。
在for
循环之间,我们可以在主线程上执行我们想要的所有代码。(主线程没有阻塞)。接下来是第二个for
循环。这个循环将遍历并告诉每个线程到join
。
让我们假设线程1-9在这个时间点之前已经执行了,但是我们仍然在后台运行线程10。当for
循环加入线程1时,由于它已经完成,for
循环再次运行。这种情况继续在前9个线程中发生。现在我们join
线程10,它仍然在运行,主线程现在将等待线程10完成。(如果您需要在某一时刻完成代码才能继续前进,这是很好的。)
接下来,我们有我们创建的return 0;
和std::thread
,超出了作用域,因为它们是对象,所以调用析构函数。因为所有的线程都连接到主线程,所以不会抛出异常,所有的事情都会很好地结束。
如果我们想,“嘿,我不在乎线程是否完成,这个程序就结束了。”当调用std::thread
析构函数时,std::thread
对象会注意到有问题,并引发std::terminate
。
如果我们真的不在乎线程何时完成或者线程是否完成。我们可以使用detach
。这将允许它在完成任务之前运行。如果它仍然运行在main的末尾,那么线程就会毫无例外地死掉(一个现代的操作系统会用这个进程杀死它)。
什么是joinable
?如果您没有从调用方(本例中的主线程)将线程指向或detach
线程,则joinable
将为真。如果您想检查线程是否仍在运行,而不是join
,直到线程运行。您将需要使用std::async
代替。(Can C++11 tell if std::thread is active?)
https://stackoverflow.com/questions/43118107
复制相似问题