0

The following code often freezes when I compile/run it using GCC 12.2.0

#include <iostream>
#include <latch>
#include <thread>
#include <vector>

int main() {  
  for(unsigned k = 0; k != 10; k++) {
    std::latch sync{2};
    auto task = [&sync]() { sync.arrive_and_wait(); };
    std::thread t1(task);
    std::thread t2(task);
    t1.join(); std::cout << k << ":1" << std::endl;
    t2.join(); std::cout << k << ":2" << std::endl;
  }
}

If I remove sync.arrive_and_wait() then everything is fine. The last line of outputs I saw in multiple runs that led to deadlock are: 2:1, 1:2, 0:2, ...

Isn't this code supposed to be deadlock-free?

Koosha
  • 1,492
  • 7
  • 19
  • 1
    `2:`, `1:`, `0:` is impossible in the shown code. – 273K Sep 28 '22 at 05:48
  • `arive_and_wait` is allowed to throw exceptions; maybe that's what's happening? Maybe try adding a `try` block around the `arive_and_wait` call? – Miles Budnek Sep 28 '22 at 05:49
  • @273K Why impossible? They are copy-pasted from my terminal (only the last line, as I mentioned) – Koosha Sep 28 '22 at 06:24
  • @MilesBudnek The following is from cppreference about `std::thread`: "The return value of the top-level function is ignored and if it terminates by throwing an exception, std::terminate is called." So in case of an exception, the program should be terminated (anyway, I did put try-catch, and that is not the problem). – Koosha Sep 28 '22 at 06:26
  • They can't be on the single line. They cannot be in reverse order. – 273K Sep 28 '22 at 06:30
  • `2:1` is the last line from one run, `1:2` is the last line from another run, and `0:2` is the last line from another run. I don't see any point in printing all the previous lines (`std::cout` are in a single-threaded part of the code). – Koosha Sep 28 '22 at 06:32
  • @Scheff'sCat, I am not sure I fully understand your comment, but the output I shared is from the code I shared. If you mean, the code I shared is deadlock-free, that is my problem. This code deadlocks on my machine. If you mean the problem could be your machines or installation, ... then that is certainly a possibility, although I did not do any customization to the options when I built/installed GCC other than `--enable-checking=release`, `--with-sysroot=/usr/local/system_root`, and `--enable-threads`. – Koosha Sep 29 '22 at 03:14
  • Sorry, I overlooked the `k` in `std::cout << k <<`... – Scheff's Cat Sep 29 '22 at 05:09
  • I can't reproduce this with stock g++ 12.1.0 on Ubuntu 22.04 amd64. So please share full information about your setup. Versions of OS, gcc, libstdc++, libc, operating system, CPU architecture, etc. Exact options used when compiling and linking. Also, just to rule out the trivialities: you've made certain that you are actually running the same code you are showing us here? You must know how many questions here get resolved when the asker figures out they forgot to save the source file in their editor, and were running an old version of their code... – Nate Eldredge Oct 01 '22 at 03:42

0 Answers0