Код: Выделить всё
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
std::atomic g_Flag = 0;
std::atomic cnt = 0;
void worker1(std::future fut) {
printf("this is thread 1\n");
g_Flag = 1;
cnt++;
fut.get();
printf("thread 1 exists\n");
}
void worker2(std::promise prom) {
printf("this is thread 2\n");
g_Flag = 2;
cnt++;
prom.set_value(10);
printf("thread 2 exists\n");
}
using namespace std::chrono_literals;
int main() {
std::promise prom;
std::future fut = prom.get_future();
std::thread t1(worker1, std::move(fut));
std::thread t2(worker2, std::move(prom));
while (cnt.load() != 2) {
}
t1.detach();
t2.detach();
printf("main exists\n");
return 0;
}
Код: Выделить всё
eugene@DESKTOP-P8P395D:~/tron$ ./a.out
this is thread 1
this is thread 2
main exists
thread 2 exists
thread 1 exists
thread 1 exists
Есть какие-нибудь идеи о том, почему это может происходить? Я полагаю, что это может быть связано со средой WSL2, поскольку в обычных обстоятельствах я ожидаю, что каждый поток напечатает свое сообщение о выходе только один раз. Любая помощь или направление приветствуются. Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/782 ... ce-in-wsl2
Мобильная версия