Bug 93610 - std::call_once not working as expected when the invocated function thwows an exception.
Summary: std::call_once not working as expected when the invocated function thwows an ...
Status: RESOLVED DUPLICATE of bug 66146
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-02-06 00:54 UTC by Mario Galindo
Modified: 2020-02-06 08:53 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mario Galindo 2020-02-06 00:54:02 UTC
cppreference.com (https://en.cppreference.com/w/cpp/thread/call_once) says:
If that invocation throws an exception, it is propagated to the caller of call_once, and the flag is not flipped so that another call will be attempted (such call to call_once is known as exceptional).
This is not working with gcc but with clang.

The example that appears in cppreference do not run with gcc. In particular:

#include <iostream>
#include <thread>
#include <mutex>
 
std::once_flag flag2;
 
void may_throw_function(bool do_throw)
{
  if (do_throw) {
    std::cout << "throw: call_once will retry\n"; // this may appear more than once
    throw std::exception();
  }
  std::cout << "Didn't throw, call_once will not attempt again\n"; // guaranteed once
}
 
void do_once(bool do_throw)
{
  try {
    std::call_once(flag2, may_throw_function, do_throw);
  }
  catch (...) {
  }
}
 
int main()
{ 
    std::thread t1(do_once, true);
    std::thread t2(do_once, true);
    std::thread t3(do_once, false);
    std::thread t4(do_once, true);
    t1.join();
    t2.join();
    t3.join();
    t4.join();
}
Comment 1 Jonathan Wakely 2020-02-06 08:53:35 UTC
This is a known issue.

*** This bug has been marked as a duplicate of bug 66146 ***