简体   繁体   English

C ++ 11线程限制?

[英]C++11 threads limit?

edit 3: WORKS on win 7, BSOD on win 8.1. 编辑3:7获胜,BSOD 适用于胜8.1。 Thank's why I hate updates. 谢谢,为什么我讨厌更新。

edit 2: I got a BSOD saying: "Attempted to write in read only memory: vtss.sys" 编辑2:我得到一个BSOD说:“试图在只读存储器中写入:vtss.sys”

edit : There is a variable WORD KEYS[1024]; 编辑 :有一个变量WORD KEYS[1024]; being read but only read , never written by all threads. 正在被readonly read ,而不是被所有线程写入。 I am using visual studio 2012 on windows 8.1. 我在Windows 8.1上使用Visual Studio 2012。 Why does it crash? 为什么会崩溃?

Is there a limitation in the number of C++11 threads? C ++ 11线程数是否有限制? I am trying to make 1k threads but sometimes the program crash at no 47... Why 47? 我正在尝试制作1k线程,但有时程序在47号时崩溃...为什么47?

#include <iostream>
#include <thread>

using namespace std;

WORD KEYS[1024];

void thread_job(int no)
{
    cout << KEYS[0] << KEYS[no] << endl;
}

int main()
{
    int items = 1000;

    std::thread *tt = new std::thread[items];

    for(int i = 0; i < items; i++)
    {
        tt[i] = thread(thread_job, i+1);
        cout << i << endl;
    }

    for(int i = 0; i < items; i++)
        tt[i].join();

    return 0;
}

Without knowing anything about your code, which OS you're targeting or which compiler you're using, I am willing to say that you can have more than 47 threads . 在不了解您的代码,您所针对的操作系统或所使用的编译器的任何知识的情况下,我愿意说您可以拥有47个以上的线程 What you are seeing is a bug in your code, not the compiler, OS or language conspiring against you. 你们看到的是在你的代码中的错误,而不是编译器,OS或语言密谋反对你。

However, there is virtually no sane reason to do what you are doing. 但是,实际上没有理智的理由去做自己在做的事情。 You don't want 1000 threads, but you can create them just fine. 您不希望有 1000个线程,但是可以创建它们。

If you want help in determining why your code crashes, post a full code sample that reproduces the problem. 如果需要帮助确定代码崩溃的原因,请发布完整的代码示例以重现该问题。

If you want to use multiple threads it is better to go for a thread pool. 如果要使用多个线程,最好使用一个线程池。

Howto 如何

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM