multithreading - 主线程比其他线程慢
问题描述
我有一个 C++ 中的多线程程序。我想知道为什么如果主线程执行部分计算,那么(在我的测试中)主线程总是比其他线程慢几秒钟。(为了比较其他线程在 9 秒内运行,主线程在 11 秒内运行)。
如果我不使用主线程进行部分计算,那么所有线程大约会同时完成。IE :
int numThreads = 4;
std::thread *threads = new std::thread[numThreads];
for (int i = 0; i < numThreads; i++){
threads[i] = std::thread(func)
}
for (int i = 0; i < numThreads; i++){
threads[i].join();
}
对比
int numThreads = 4;
std::thread *threads = new std::thread[numThreads - 1];
for (int i = 0; i < numThreads-1; i++){
threads[i] = std::thread(func);
}
func();
for (int i = 0; i < numThreads-1; i++){
threads[i].join();
}
非常感谢!
解决方案
推荐阅读
- firebase - Flutter onAuthStateChanged 在网络上不起作用
- arrays - Ruby Array Select - 带有通配符的嵌套映射
- python - Pandas:读取几个大的 .bz2 文件并附加它
- c# - 如何实现 ILogger 以将消息发送到 SignalR Hub?
- java - 如何在 Java 中启用 TLS_ECDHE_ECDSA_WITH_AES_128_CCM 和 TLS_ECDHE_ECDSA_WITH_AES_256_CCM 密码套件?
- android - 何时使用 JobIntentService 与 WorkManager?
- excel - Excel:在 1 行中重复,在另一行中是唯一的。每当重复项中存在给定的唯一值时,在另一行中标记
- python - 使用 SAT 求解器 (Python) 查找特定区域内的所有自由多骨牌组合
- python - 如何正确解析 XML 属性并使用 Python 追加到列表中?
- woocommerce - 使连锁产品适用于以编程方式添加的订单