GithubHelp home page GithubHelp logo

condition_variable_ex's Introduction

condition_variable_ex

Adds to the condition_variable standard class the following methods:

  • cv_status_ex wait_for_ex(unique_lock &__lock, const chrono::duration<_Rep, _Period> &__rtime, _Predicate __p)

  • cv_status_ex wait_until_ex(unique_lock &__lock, const chrono::time_point<_Clock, _Duration> &__atime, _Predicate __p, const chrono::duration<int64_t,_Period> __pDuration=chrono::milliseconds(10))

Where cv_status_ex can be:

  • timeout: wait abandoned by timeout.
  • signaled: wait abandoned by singal (notify_one, notify_all).
  • predicate: wait abandoned by predicate.

The method wait_until_ex has an additional _pDuration parameter which indicates how often the predicate must be inspected.

Motivation

Sometimes it is interesting to know if the wait has been abandoned because of the signal or because of the predicate. With the standard class it is no possible, as it only returns timeout/no_timeout.

One tipical case is a backgroud worker thread, a perennial worker thread that can be activaded by the main thread or others on demand or by a timeout:

std::mutex m_working;
bool working = false;
condition_variable_ex cv_working;
bool awake_thread = false;

void bgnd_worker_thread(int timeout)
{
    // the predicate can depends on the main work
    auto predicate = []
    { return awake_thread; };

    std::unique_lock lkw(m_working);
    while (true)
    {
        // wait with timeout and predicate
        CoutThread{} << "bgnd sleeping for " << th_timeout << "ms " << endl;
        auto finalStatus = cv_working.wait_for_ex(lkw, chrono::milliseconds(th_timeout), predicate);
        if (finalStatus == cv_status_ex::signaled || !working)
        {
            break;
        }
        
        // do some work
        ...

        // reset the predicate
        awake_thread = false;
    }
}

void main() 
{
    // start bgnd working thread
    awake_thread = false;
    working = true;
    std::thread bgnd_worker(bgnd_worker_thread, loop_timeout);

    ....

    // activate bgnd thread
    awake_thread = true;

    ....

    // stop and wait bgnd thread
    std::unique_lock<std::mutex> lck(m_working);
    working = false;
    lck.unlock();
    cv_working.notify_all();
    bgnd_worker.join();

    return;
}

How to compile

  • As condition_variable_ex inherits from condition_variable, for the library to compile, the private members of condition_variable class must be declared protected. One modified copy is provided in the include folder.

  • C++17 or greater needed.

condition_variable_ex's People

Contributors

gerardcarbo avatar

Watchers

James Cloos avatar  avatar

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.