Home > front end >  std::this_thread::sleep_for doesn't exist - Windows 10 g
std::this_thread::sleep_for doesn't exist - Windows 10 g

Time:11-17

I am currently running a test program which executes perfectly well online (replit.com), but when I run it locally (on Windows 10 with gcc and g ) I get an error:

test.cpp: In function 'int main()':
test.cpp:18:8: error: 'std::this_thread' has not been declared
   std::this_thread::sleep_for(std::chrono::milliseconds(time));

This is my program:

#include <iostream>
#include <thread>
#include <chrono>
#include <random>
#include <string>

int randomInt (int MIN, int MAX) {
    std::random_device rd;
    std::default_random_engine eng(rd());
    std::uniform_int_distribution<int> distr(MIN, MAX);
    return distr(eng);
}

int main() {
    int loop = 20;
    for (int i = 0; i < loop; i  ) {
        int time = randomInt(10, 100);
        std::this_thread::sleep_for(std::chrono::milliseconds(time));
    }

    return 0;
}

Why does this happen?

CodePudding user response:

I went back to replit.com to see what they were doing to compile the program. Apparently they used the -pthreads switch:

g   -pthread -std=c  17 -o main main.cpp

Which now works for me.

  • Related