logo
down
shadow

condition_variable::wait_for method not returning - even after the timeout


condition_variable::wait_for method not returning - even after the timeout

By : arathi ballamudi
Date : November 22 2020, 02:59 PM
I hope this helps . As suggested by @Zan Lynx, the culprit was #pragma pack statement. I initially used only #pragma pack(1) to byte align some structures. This affected many of mutex structures to 1 byte alignment.
I changed the #pragma pack(1) statements to #pragma pack(push, 1) #pragma pack(pop). Its all working fine. Hence my problem is solved; i learnt something. a Lot. ! :-)
code :


Share : facebook icon twitter icon
What is the difference between std::condition_variable::wait_for and std::condition_variable::wait_until?

What is the difference between std::condition_variable::wait_for and std::condition_variable::wait_until?


By : abilash
Date : March 29 2020, 07:55 AM
I wish this help you The difference is in how the wait duration is represented: wait_for takes a relative time ("wait for up to 10 seconds"), whereas wait_until takes an absolute time ("wait until 12:00 on October 30, 2012").
Compare the declarations of the time parameters:
code :
// wait_for:
const std::chrono::duration<Rep, Period>& rel_time

// wait_until:
const std::chrono::time_point<Clock, Duration>& abs_time
condition_variable::wait_for always waits?

condition_variable::wait_for always waits?


By : John Ferguson Jr.
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Check that you are actually calling signal or broadcast on ready. There's a possible race if you're not careful where you can signal the condition before you actually wait on it (which will cause a wait until timeout).
How to use a condition_variable to really wait_for no longer than a certain duration

How to use a condition_variable to really wait_for no longer than a certain duration


By : James
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I think that you misuse the condition_variable's lock. It's for protecting condition only, not for protecting a time-consuming work.
Your example can be fixed easily by splitting the mutex into two - one is for critical section, another is for protecting modifications of ready condition. Here is the modified fragment:
code :
typedef std::unique_lock<std::mutex> lock_type;
auto sec = std::chrono::seconds(1);
std::mutex mtx_work;
std::mutex mtx_ready;
std::condition_variable cv;
bool ready = false;

void task1() {
    log("Starting task 1. Waiting on cv for 2 secs.");
    lock_type lck(mtx_ready);
    bool done = cv.wait_for(lck, 2*sec, []{log("Checking condition..."); return ready;});
    std::stringstream ss;
    ss << "Task 1 finished, done==" << (done?"true":"false") << ", " << (lck.owns_lock()?"lock owned":"lock not owned");
    log(ss.str());
}

void task2() {
    // Allow task1 to go first
    std::this_thread::sleep_for(1*sec);
    log("Starting task 2. Locking and sleeping 2 secs.");
    lock_type lck1(mtx_work);
    std::this_thread::sleep_for(2*sec);
    lock_type lck2(mtx_ready);
    ready = true; // This happens around 3s into the program
    log("OK, task 2 unlocking...");
    lck2.unlock();
    cv.notify_one();
}
@2 ms: Starting task 1. Waiting on cv for 2 secs.
@2 ms: Checking condition...
@1002 ms: Starting task 2. Locking and sleeping 2 secs.
@2002 ms: Checking condition...
@2002 ms: Task 1 finished, done==false, lock owned
@3002 ms: OK, task 2 unlocking...
0 as a timeout in std::condition_variable::wait_for

0 as a timeout in std::condition_variable::wait_for


By : user3321436
Date : March 29 2020, 07:55 AM
will help you According to the working draft and C++11 standard section 32.6.3 (int the working draft), wait_for is
code :
_cv.wait(lk, []() {
    return someCondition;
}
C++ condition_variable wait_for returns instantly

C++ condition_variable wait_for returns instantly


By : Irfan Nizar Nazarudd
Date : March 29 2020, 07:55 AM
Any of those help Short answer: compile with -pthread and your issue will go away.
Update:
Related Posts Related Posts :
  • Complexity analysis of loop with limited looping time
  • Boost test fails with enum classes inside namespaces
  • is this a function declaration?
  • Socket recv() one byte at a time
  • C++ Glibc Detected error. Double Free or corruption
  • OpenCV - How to write IplImage array in Mat form?
  • Qt event when anything changed on the window/screen + Screenshot
  • OpenGL Linker error, linking with uncompiled shader
  • Find 4 specific corner pixels and use them with warp perspective
  • Cin Execution Not Working (program.exe < filewithdata.txt)
  • cudaMallocManaged causes Access Violation
  • How to correctly read a value from stdin into a variable
  • Binary Search Tree Forgetting Every Node I Add
  • C++ Return Value from function not same as that value in function
  • Microsoft Visual Studio C++, OpenCV animation
  • C++ function pointer syntax. Why does (*) work but * not?
  • C++: Template class binary operator overloading - seg fault?
  • Passing buffer between two terminals (Named Pipe)
  • C++ parse sub-string to integer
  • Create stereo context for modern opengl
  • Memory leak, when using GLM
  • Get Control under cursor
  • Is this proper behavior? std::map iterator invalidation
  • 2D isometric engine - Math problems - Cube selection - diamond shape map
  • Open second window in Qt
  • Stack Overflow error with Vectors
  • Pure Virtual Friend Class
  • Object initialization syntax in C++
  • Variable undefined error
  • C++ tolower/toupper char pointer
  • Overhead with std::function
  • Is there a way to make a loop that get user input but doesn't stop if the user doesn't input anything?
  • OpenCV in cmake-based project: checking for contrib modules
  • shared_ptr vs unique_ptr uses in classes and children
  • MFC dialog Border padding changed after switching from VS2010 to 2012 or later
  • String rotator in C++ (bitwise rotation)
  • Custom sort vector of pair based on their values
  • Pointer to const overloaded member function
  • how to avoid this for-loop mess in c++?
  • Can the state of a standard C++ iostream manipulator be polled?
  • How to make sure a data type is as large as it needs to be in C++
  • Why is my first ofstream output in my else block missing the fill character?
  • Returning static/ normal arrays in recursion/another function
  • c++, why use const std::string & parameterName?
  • expression did not evaluate to a constant in C++ VS
  • Owner object that takes pre-created values ? Wrong design?
  • Cannot use Macro in a C++ constructor?
  • vector is loosing mat4 information
  • Invalid declarator before with map of struct?
  • std::initializer_list to return member variables returns incorrect values
  • Unable to use Boost + Qt in Mac
  • Passing template function and overload as function argument
  • non standard extension warning when searching in a vector of unique_ptrs
  • Qt Windows x64 build succeeds in IDE but not on the command line
  • boost::asio usage in self-contained class
  • CGAL triangulation with constraints changes points coords
  • How to resolve an ambiguous reference caused by a conflicting identifier from inline namespace
  • When UTF8 emoji character is removed from NSMutableString the conversion to std::string fails
  • XOR of two strings of 0s and 1s
  • OpenCV: check if pixel is within bounding rectangle, separated by contour line
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co