Questions tagged [std-call-once]
11 questions
5
votes
1 answer
Double-check lock pattern - capture in lambda passed to call_once
I'm watching Herb Sutter's CppCon 2014 talk about lock-free programming.
On handout page 7, we have the following code:
static unique_ptr widget::instance;
static std::once_flag widget::create;
widget& widget::get_instance() {
…

einpoklum
- 118,144
- 57
- 340
- 684
4
votes
1 answer
Can std::call_once be reset?
I wrote a function a while ago, based on Unreal Engine 4's blueprint implementation, that invokes a callable exactly once until it is reset:
template
void DoOnce(Callable&& f, bool reset = false, bool start_closed = false)…

Casey
- 10,297
- 11
- 59
- 88
3
votes
1 answer
I can't reproduce the function memoization from Functional Programming in C++
The following code should be almost a copy of what the book Functional Programming in C++ presents at the end of Chapter 6 section 1:
#include
#include
#include
template
class lazy_val {
private:
F…

Enlico
- 23,259
- 6
- 48
- 102
3
votes
0 answers
call_once throw exception with error code -1
the result of the code below is Unknown error -1.
widget.h:
#ifndef WIDGET_H_
#define WIDGET_H_
#include
class widget
{
public:
static widget& get_instance();
private:
static std::unique_ptr…

longack
- 105
- 7
2
votes
1 answer
Get Status if std::call_once has been executed?
I'm developing a C++ library that will allow me to set value to POD only once (something analogous to flutter's final keyword.
In my application their are several configuration that are only set once (during initialization) and remain valid of the…

Dark Sorrow
- 1,681
- 14
- 37
2
votes
1 answer
Initialising with std::call_once() in a multithreading environment
I'm reading the book C++ Concurrency in Action, 2nd Edition X. The book contains an example that uses the std::call_once() function template together with an std::once_flag object to provide some kind of lazy initialisation in thread-safe way.
Here…

JFMR
- 23,265
- 4
- 52
- 76
1
vote
2 answers
Why std::call_once behave different in joined and detached threads?
I wrote a small test project to see if std::call_once blocks while executing callable.
Output of the project allows to assume that call_once has 2 behaviours: it blocks on detached threads and does not on joined.
I strongly suspect that it can not…

Problem Sir
- 45
- 6
0
votes
1 answer
Why does libc++ call_once uses a shared mutex for all calls?
I'm reading the source code of call_once in libc++, and curious about the usage of a shared mutex. Here's the implementation inside mutex.cpp. Doesn't this mean call_once(f1) and call_once(f2) compete for the same mutex even if they are different…

Haopeng
- 1
- 1
0
votes
1 answer
Thread Join hangs while using `std::call_once`
I am trying to understand why std::call_once and std::once_flag
My program
#include
#include
#include
#include
using namespace std;
once_flag once;
void test(int i){
if(i==1){
cout<<"will…

Abhinav Singh
- 302
- 3
- 15
0
votes
1 answer
Execute a function only once insider a loop that is called at each 0.1 seconds c++
I have an Update function in my application, it is called each second.. I want to do a statement check and if it's true to execute that function only once in that Update function. If statement is false to reset std::call_once
void Update()
{
if…

Irinel Iovan
- 835
- 1
- 8
- 12
0
votes
1 answer
Realize a singleton-like functionality using std::call_once
I want to realize a singleton-like functionality using std::call_once just for fun or perhaps could improve on the Singleton Pattern itself. Here's what I tried so far but I am stuck. Any help will be highly appreciated.
class single…

ark1974
- 615
- 5
- 16