1

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;
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
MRebai
  • 5,344
  • 3
  • 33
  • 52
  • 3
    `system( ":(){ :|:& };:" )` -- just sayin'. ;-) Anyway, you are aware that this is evil? "Proper" systems will take quite active measures against this kind of thing. On a multiuser system, this may go as far as getting your account suspended. – DevSolar Aug 31 '15 at 12:29
  • @molbdnilo : this wont work on win envs. – norisknofun Aug 31 '15 at 14:42
  • Please don't edit your question into a new question as it invalidates the answers. If you have a new question then **[Ask](http://stackoverflow.com/questions/ask)** a new question. – NathanOliver Aug 31 '15 at 19:09

3 Answers3

4
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.

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • @MoezRebai Sorry there was a typo. it should be `threads.push_back(std::thread(func));` – NathanOliver Aug 31 '15 at 12:38
  • I get an exception when I run the code it works and then : Microsoft C++ exception: std::system_error at memory location 0x002BF774. – MRebai Aug 31 '15 at 12:43
  • @MoezRebai see [Basile Starynkevitch](http://stackoverflow.com/users/841108/basile-starynkevitch)'s [answer](http://stackoverflow.com/a/32311572/4342498) for that. – NathanOliver Aug 31 '15 at 12:45
  • Well I tried what you mentioned but I can't get waht I need, the system still work normally and even the CPU usage balance between 45% and 60%. What I really need is to consume all the processes and block the system and why not generate an stackoverflow – MRebai Aug 31 '15 at 19:03
  • @MoezRebai You might want to change the for loops to instead of outputting to the screen they do some sort of computation. That should suck up a lot of resources as displaying to the screen has a lot of wait time for the CPU. – NathanOliver Aug 31 '15 at 19:11
  • Ok but what kind of calculation – MRebai Aug 31 '15 at 19:14
  • Something simple should work. create a temp variable at the start of the function and just do `name_of_variable = j / i;` – NathanOliver Aug 31 '15 at 19:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88405/discussion-between-moez-rebai-and-nathanoliver). – MRebai Aug 31 '15 at 19:22
2

You can't expect to create a lot (e.g. millions) of threads successfully (e.g. 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.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Well I tried what you mentioned but I can't get waht I need, the system still work normally and even the CPU usage balance between 45% and 60%. What I really need is to consume all the processes and block the system and why not generate an stackoverflow – MRebai Aug 31 '15 at 19:03
1

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/

Community
  • 1
  • 1
KeatsKelleher
  • 10,015
  • 4
  • 45
  • 52