Skip to content

Commit

Permalink
add minimal timed CV wait example
Browse files Browse the repository at this point in the history
  • Loading branch information
josuttis committed Sep 10, 2018
1 parent c0eb01c commit da5e6da
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -12,3 +12,4 @@ Makefile.h
*.ext
*.xtr
*.out
.pdf
36 changes: 36 additions & 0 deletions test_cv.cpp
Expand Up @@ -248,6 +248,39 @@ void testCVStdThreadPred(bool callNotify)

//------------------------------------------------------

void testMinimalWaitFor()
{
// test the basic timed CV wait API
std::cout << "*** start testMinimalWaitFor()" << std::endl;
auto dur = std::chrono::seconds{5};

bool ready = false;
std::mutex readyMutex;
std::condition_variable2 readyCV;
{
std::jthread t1([&ready, &readyMutex, &readyCV, dur] {
std::cout << "\n- start t1" << std::endl;
auto t0 = std::chrono::steady_clock::now();
{
std::unique_lock lg{readyMutex};
readyCV.iwait_for(lg,
dur,
[&ready] {
return ready;
});
}
assert(std::chrono::steady_clock::now() < t0 + dur);
std::cout << "\n- t1 done" << std::endl;
});

std::this_thread::sleep_for(1.5s);
std::cout << "- leave scope (should signal interrupt and unblock CV wait)" << std::endl;
} // leave scope of t1 without join() or detach() (signals cancellation)
std::cout << "\n*** OK" << std::endl;
}

//------------------------------------------------------

template<typename Dur>
void testTimedCV(bool callNotify, bool callInterrupt, Dur dur)
{
Expand Down Expand Up @@ -542,6 +575,9 @@ int main()

std::cout << std::boolalpha;

std::cout << "\n\n**************************\n";
testMinimalWaitFor();

std::cout << "\n\n**************************\n";
testCVStdThreadNoPred(false); // signal cancellation
std::cout << "\n\n**************************\n";
Expand Down
Binary file added tex/jthread_proposed_wording_180910.pdf
Binary file not shown.

0 comments on commit da5e6da

Please sign in to comment.