Skip to content

<locale>: Calling use_facet<collate<char>>(loc) changes output of strftime() in a different thread #6150

@StephanTLavavej

Description

@StephanTLavavej

Originally reported as DevCom-246257 / internal VSO-275595 by constructing regex{"meow"}, but regex calls use_facet so the repro can be reduced:

Important

This repro requires you to temporarily modify your system's locale settings.

C:\Temp>type meow.cpp
// Settings > Time & language > Date & time > Change the date and time format > Long time
// Change "9:40:07 AM / 2:40:07 PM" to "09:40:07 / 14:40:07"

#include <clocale>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <locale>
#include <thread>
using namespace std;

void run_on_other_thread() {
    locale loc{};
    (void) use_facet<collate<char>>(loc);
}

int main() {
    tm t{};
    t.tm_hour = 14;
    t.tm_min  = 30;
    t.tm_sec  = 45;

    setlocale(LC_ALL, "");

    char out1[100];
    strftime(out1, sizeof(out1), "%X", &t);
    puts(out1);

    {
        jthread thread(&run_on_other_thread);
    }

    char out2[100];
    strftime(out2, sizeof(out2), "%X", &t);
    puts(out2);

    if (strcmp(out1, out2) == 0) {
        puts("PASS");
    } else {
        puts("FAIL");
    }
}
C:\Temp>cl /EHsc /nologo /W4 /std:c++latest /MTd /Od /Zi /Fdmeow.pdb meow.cpp
meow.cpp

C:\Temp>meow
14:30:45
2:30:45 PM
FAIL

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions