microsoft / microsoft/STL

`<thread>`: Using `this_thread::sleep_for` in a module results in compilation errors

Open
#5,203 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

compiler modules
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

When I use vs and use std::this_thread::sleep_for in a C++ module, I get an error.

'std::this_thread::sleep_until': no matching overloaded function found
binary '+': 'const std::chrono::time_point<std::chrono::steady_clock,std::chrono::duration<__int64,std::ratio<1,1000000000>>>' does not define this operator or a conversion to a type acceptable to the predefined operator
binary '+=': no global operator found which takes type 'const std::chrono::duration<__int64,std::ratio<1,1000>>' (or there is no acceptable conversion)
binary '-': 'const std::chrono::time_point<std::chrono::steady_clock,std::chrono::duration<__int64,std::ratio<1,1000000000>>>' does not define this operator or a conversion to a type acceptable to the predefined operator
binary '>': 'const std::chrono::duration<__int64,std::ratio<1,1000>>' does not define this operator or a conversion to a type acceptable to the predefined operator

The minimum duplicate file is as follows:
Mouse.ixx

module;
#include <thread>
#include <chrono>

export module Mouse;

export struct Mouse {
    Mouse() {
        std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
    }
};

main.cpp

import Mouse;

int main() {
	Mouse mouse;

	return 0;
}

A possible solution:
Modify #include <thread> and #include <chrono> in Mouse.ixx to import std to pass the compilation. The modified Mouse.ixx file is as follows:

export module Mouse;
import std;
export struct Mouse {
    Mouse() {
        std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
    }
};

The main.cpp file does not need to be modified and can now be compiled.
I am using the vs version:

Microsoft Visual Studio Community 2022 (64-bit) - Current
Version 17.12.3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the minimal Mouse.ixx and main.cpp reproducer with Visual Studio 17.12.3, first using the listed headers and then import std. Compare the resulting diagnostics and inspect the STL module/header handling for thread and chrono; done means the reproducer compiles correctly or the remaining implementation limitation is clearly confirmed.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.