Comments (4)
Hello @borisu ,
You can do it with oatpp::async::ConditionVariable
and oatpp::async::Lock
Here is example:
Declare Lock and CV to share between oatpp coroutine and some other thread:
oatpp::async::Lock lock;
oatpp::async::ConditionVariable cv;
Coroutine:
class TestCoroutineWait : public oatpp::async::Coroutine<TestCoroutineWait> {
private:
std::shared_ptr<Resource> m_resource;
oatpp::async::LockGuard m_lockGuard; // Use async::LockGuard to guard the lock
oatpp::async::ConditionVariable* m_cv;
public:
TestCoroutineWait(std::shared_ptr<Resource> resource,
oatpp::async::Lock* lock, // pass async::Lock to coroutine
oatpp::async::ConditionVariable* cv) // pass async::ConditionVariable to coroutine
: m_resource(resource)
, m_lockGuard(lock)
, m_cv(cv)
{}
Action act() override {
return m_cv->wait(m_lockGuard, [this]{return m_resource->counter == 100;}) // long wait until counter == 100
.next(yieldTo(&TestCoroutineWait::onReady));
}
Action onReady() {
OATPP_ASSERT(m_lockGuard.owns_lock()) // Now coroutine owns the lock
return finish();
}
};
Other thread:
std::thread t([&resource, &lock, &cv] {
{ // very long operation
std::lock_guard<oatpp::async::Lock> guard(lock);
for(int i = 0; i < 100; i++) {
resource->counter++;
}
}
cv.notifyAll();
})
Note1: you can find working example here - https://github.com/oatpp/oatpp/blob/master/test/oatpp/core/async/ConditionVariableTest.cpp
Note2: It's possible to implement it with just oatpp::async::CoroutineWaitList but it's cleaner to use do it this way. There are details that you may miss when working with CoroutineWaitList.
Note3: Always use thread pool in case you are spawning threads from coroutine.
Note4: Make sure to have latest master as oatpp::async::ConditionVariable
is an early feature of 1.4.0
available already in 1.3.0
from oatpp.
Thanks a lot , I will try it and ccomment here with results
from oatpp.
Worked well. Thanks Indeed I needed to upgrade to 1.3-latest as I understand it was relatively new additions.
from oatpp.
Related Issues (20)
- Enhance Deserializer's "Error. Unknown field"
- How can I use OATPP to handle web browser request download file. HOT 2
- Memory leak in Object mapping HOT 1
- Deserialize json when not include required field only gives error when the field is specified as null
- [Question] readBodyIntoDTO when there's a list in the body
- API question
- how to make dynamic sql use oatpp-sqlite HOT 1
- LazyStringMapTemplate::get returns Nullptr wich is not in oatpp.io Documentation
- Please add support for CMake 3.16
- Cannot test if required integer query parameter is empty
- [Question] Is it possible during compilation to write out the generated oas-3.0.0.json file?
- May memory leak
- How should I make my process send requests to the target when using OATPP HOT 4
- Stack corruption issue in ConnectionProvider.cpp
- Can not build project successfully
- Is it possible make oatpp::network::tcp::client::ConnectionProvider support bind client IP?
- io_uring support request HOT 1
- Compilation error on Windows with MinGW
- Facing linker issue (undefined reference to `oatpp::base::Environment::getComponent) in windows using mingw gcc compiler
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oatpp.