So, I want to create a async data holder in C++ which can be got only if it has been…
Jump to turn (6)
- 1So, I want to create a async data holder in C++ which can be got only if it has
- 2Is it better if I use enum Status, or instead use std::optional for value?
- 3What about using uniqueptr instead of optional?
- 4Then what do you recommend out of those 3?
- 5From the examples that you give, how come the condition variable is gone?
- 6Yes, I want the init process to be async and Get() to be blocking. Can you modif
- User
So, I want to create a async data holder in C++ which can be got only if it has been initialized asynchronously previously. If it has not been initialized, it will return the default value of the type. Is this sufficient?
template class AsyncDataHolder { enum class Status { NOT_INITIALIZED, INITIALIZING, INITIALIZED };
codestd::function<T()> initializer_; Status status_{NOT_INITIALIZED}; mutable bthread::ConditionVariable cv_; mutable bthread::Mutex mtx_; T value_;public: AsyncDataHolder(std::function<T()> initializer) : initializer_(initializer) { }
code
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.