简体   繁体   中英

How to crash system using Multithreading

I start learning Multi-threading in C++ and I' m trying to crash and block my system by occupying all the processors. As fact I tried to create many threads and run them, but I didn't get what I need

void func()
{
    std::cout << "C++11 MULTITHREADING\n";
    for (int i = 1; i < INT_MAX; i++)
    {
        for (int j = 1; j < INT_MAX; j++)
            std::cout << i / j << " ";
    }
}


int main()
{

    for (int i = 0; i < INT_MAX; i++)
    {
        std::thread t(func);
        t.join();
    }
    std::cout << " ***END OF A PROGRAM***\n";

    return 0;
}
t.join();

Is going to "Join" the thread you just created into the current thread. This means that you will run the entire function and return before you create the next thread. It would be the same as doing:

for (int i = 0; i < INT_MAX; i++)
{
    func();
}

If you want to spawn multiple threads and run them then store them in a std::vector and then call join() on all of them after you create them.

std::vector<std::thread> threads;
threads.reserve(INT_MAX);  // save reallocations
for (int i = 0; i < INT_MAX; i++)
{
    threads.push_back(std::thread(func));
}

for (int i = 0; i < INT_MAX; i++)
{
    threads[i].join();
}

As Basile Starynkevitch's answer points out you will need to create less threads to avoid system errors.

You can't expect to create a lot (eg millions) of threads successfully (eg because each thread needs some call stack space, typically a few megabytes per thread). You typically can create only a few hundreds, or a few thousands of threads. So INT_MAX is not reasonable, for the number of threads (1000 or 100 should be enough).

Threads are a quite costly resource, probably more heavy that an opened file descriptor . In practice, you want only a dozen of threads.

Then if you want to overload your computer, you should not join a thread as soon as you have created it. You want to have all threads running in parallel, and join once every thread has been created.

So use Nathan Oliver's answer but replace INT_MAX by MY_THREADS_MAX after adding

 #define MY_THREADS_MAX 1000

(or even less, probably 50 should be more than enough)

If on Linux, see also setrlimit(2) ; probably, the default limits prohibit you from crashing the system (unless you are root) but might make it unresponsive.

It could happen that on your system, you'll need special administrator privileges to crash your computer.

As @NathanOliver suggested you'll want to finish running your program rather than just detaching it.

But you should also consider using high-priority processes. This implementation is platform-specific, but you can check out some good answers:

What is the 'realtime' process priority setting for? or http://www.nixtutor.com/linux/changing-priority-on-linux-processes/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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