Movatterモバイル変換


[0]ホーム

URL:



This page is a snapshot from the LWG issues list, see theLibrary Active Issues List for more information and the meaning ofC++11 status.

1497.lock() postcondition can not be generally achieved

Section: 32.7[thread.condition]Status:C++11Submitter: SwitzerlandOpened: 2010-08-25Last modified: 2016-01-28

Priority:Not Prioritized

View all otherissues in [thread.condition].

View all issues withC++11 status.

Discussion:

Addresses CH-30

Iflock.lock() throws an exception, the postcondition can not be generally achieved.

[Resolution proposed by ballot comment:]

Either state that the postcondition might not be achieved, depending on the error condition, orstate thatterminate() is called in this case.

[2010-08-13 Peter Sommerlad comments and provides wording]

32.7.4[thread.condition.condvar], 32.7.5[thread.condition.condvarany]

p. 13, last bullet, and corresponding paragraphs in all wait functions

Problem:
Condition variable wait might fail, because the lock cannot be acquired when notified.CH-30 says: "If lock.lock() throws an exception, the postcondition can not be generally achieved."CH-30 proposes: "Either state that the postcondition might not be achieved, depending on the error condition, or state that terminate() is called in this case."

The discussion in Rapperswil concluded that callingterminate() might be too drastic in this case and a corresponding exception should be thrown/passed on and one should use a lock type that allows querying its status, whichunique_lock allows forstd::condition_variable

We also had some additional observations while discussing in Rapperswil:

and add the following proposed solution:

[2011-02-27: Daniel adapts numbering to n3225]

Proposed resolution:

  1. Change 32.7.4[thread.condition.condvar] as indicated:
    void wait(unique_lock<mutex>& lock);

    9Requires:lock.owns_lock() istrue andlock.mutex() is locked by the calling thread, and either

    • no other thread is waiting on thiscondition_variable object or
    • lock.mutex() returns the same value for each of thelock arguments supplied by all concurrentlywaiting (viawait ortimed_wait) threads.
    [..]

    11Postcondition:lock.owns_lock() istrue andlock.mutex() is locked by the calling thread.

    [..]
    template <class Predicate>void wait(unique_lock<mutex>& lock, Predicate pred);

    ??Requires:lock.owns_lock() istrue andlock.mutex() is locked by the calling thread, and either

    • no other thread is waiting on thiscondition_variable object or
    • lock.mutex() returns the same value for each of thelock arguments supplied by all concurrentlywaiting (viawait ortimed_wait) threads.

    14Effects:

    while (!pred())  wait(lock);

    ??Postcondition:lock.owns_lock() istrue andlock.mutex() is locked by the calling thread.

    ??Throws:std::system_error when an exception is required (30.2.2).

    ??Error conditions:

    • equivalent error condition fromlock.lock() orlock.unlock().
    template <class Clock, class Duration>cv_status wait_until(unique_lock<mutex>& lock,  const chrono::time_point<Clock, Duration>& abs_time);

    15Requires:lock.owns_lock() istrue andlock.mutex() is locked by the calling thread, and either

    • no other thread is waiting on thiscondition_variable object or
    • lock.mutex() returns the same value for each of thelock arguments supplied by all concurrentlywaiting (viawait,wait_for, orwait_until) threads.

    [..]

    17Postcondition:lock.owns_lock() istrue andlock.mutex() is locked by the calling thread.

    [..]

    20Error conditions:

    • operation_not_permitted — if the thread does not own the lock.
    • equivalent error condition fromlock.lock() orlock.unlock().
    template <class Rep, class Period>cv_status wait_for(unique_lock<mutex>& lock,  const chrono::duration<Rep, Period>& rel_time);

    21Requires:lock.owns_lock() istrue andlock.mutex() is locked by the calling thread, and either

    • no other thread is waiting on thiscondition_variable object or
    • lock.mutex() returns the same value for each of thelock arguments supplied by all concurrentlywaiting (viawait,wait_for, orwait_until) threads.

    [..]

    24Postcondition:lock.owns_lock() istrue andlock.mutex() is locked by the calling thread.

    [..]

    26Error conditions:

    • operation_not_permitted — if the thread does not own the lock.
    • equivalent error condition fromlock.lock() orlock.unlock().
    template <class Clock, class Duration, class Predicate>bool wait_until(unique_lock<mutex>& lock,  const chrono::time_point<Clock, Duration>& abs_time,    Predicate pred);

    ??Requires:lock.owns_lock() istrue andlock.mutex() is locked by the calling thread, and either

    • no other thread is waiting on thiscondition_variable object or
    • lock.mutex() returns the same value for each of thelock arguments supplied by all concurrentlywaiting (viawait ortimed_wait) threads.

    27Effects:

    while (!pred())  if (wait_until(lock, abs_time) == cv_status::timeout)    return pred();return true;

    28Returns:pred()

    ??Postcondition:lock.owns_lock() istrue andlock.mutex() is locked by the calling thread.

    29 [Note: The returned value indicates whether the predicate evaluates to true regardless of whether thetimeout was triggered. —end note ]

    ??Throws:std::system_error when an exception is required (30.2.2).

    ??Error conditions:

    • equivalent error condition fromlock.lock() orlock.unlock().
    template <class Rep, class Period, class Predicate>bool wait_for(unique_lock<mutex>& lock,  const chrono::duration<Rep, Period>& rel_time,    Predicate pred);

    30Requires:lock.owns_lock() istrue andlock.mutex() is locked by the calling thread, and either

    • no other thread is waiting on thiscondition_variable object or
    • lock.mutex() returns the same value for each of thelock arguments supplied by all concurrentlywaiting (viawait,wait_for, orwait_until) threads.

    [..]

    33Postcondition:lock.owns_lock() istrue andlock.mutex() is locked by the calling thread.

    [..]

    37Error conditions:

    • operation_not_permitted — if the thread does not own the lock.
    • equivalent error condition fromlock.lock() orlock.unlock().
  2. Change 32.7.5[thread.condition.condvarany] as indicated:

    [..]

    template <class Lock, class Predicate>void wait(Lock& lock, Predicate pred);

    [Note: if any of the wait functions exits with an exception it is indeterminate if theLock is held. One can use aLock type that allows to query that, such as theunique_lock wrapper. —end note]

    11Effects:

    while (!pred())  wait(lock);

    [..]

    31Error conditions:

    • operation_not_permitted — if the thread does not own the lock.
    • equivalent error condition fromlock.lock() orlock.unlock().

[8]ページ先頭

©2009-2026 Movatter.jp