Home > Enterprise >  How to use do while function but also not stop other codes [closed]
How to use do while function but also not stop other codes [closed]

Time:09-21

I am trying to fix this problem where if you use do and while code, it will stop other commands, but if it is done, then it will continue those commands.

for (int i = 0; i < 1000; i  ) {
    std::cout << "hey";
    Sleep(1000);
}

for (int i = 0; i < 1000; i  ) {
    std::cout << "hey number 2";
    Sleep(1000);
}

it is supposed to output together

hey

hey number 2

but instead it's just

hey

and then once it's done it's just

hey number 2

CodePudding user response:

I think you'll need threads.

Take this example. It should do the trick

#include <iostream>
#include <thread>
#include <synchapi.h>

using namespace std;

void fun1() {
    for(int i = 0 ; i < 1000 ; i  ) {
        std::cout << "Hey";
        Sleep(1000);
    }
}

void fun2() {
    for(int i = 0 ; i < 1000 ; i  ) {
        std::cout << "Hey you too";
        Sleep(630);
    }

}

int main()
{
    std::thread first(fun1);
    std::thread second(fun2);

    first.join();
    second.join();

    std::cout << "done";
    return 0;
}
  • Related