Code Monkey home page Code Monkey logo

Comments (4)

lganzzzo avatar lganzzzo commented on June 27, 2024

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.

borisu avatar borisu commented on June 27, 2024

Thanks a lot , I will try it and ccomment here with results

from oatpp.

borisu avatar borisu commented on June 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.