简体   繁体   中英

std::this_thread::sleep_for doesn't exist - Windows 10 g++

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?

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.

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