最終更新日時(UTC):
が更新

履歴 編集

function template
<shared_mutex>

std::shared_lock::try_lock_for(C++14)

template <class Rep, class Period>
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);

概要

タイムアウトする相対時間を指定して共有ロックの取得を試みる

効果

pm->try_lock_for_shared(rel_time);

pmはメンバ変数として保持している、ミューテックスオブジェクトへのポインタ

事後条件

owns_lock()の値が、pm->try_lock_for_shared(rel_time)の戻り値になること

戻り値

pm->try_lock_for_shared(rel_time)の戻り値が返る

例外

この関数は、pm->try_lock_for_shared() 関数内で投げられうるあらゆる例外を投げる可能性がある。

そのほかに、以下のerror conditionを持つsystem_error例外オブジェクトを送出する可能性がある:

#include <cassert>
#include <system_error>
#include <chrono>
#include <shared_mutex>

int main()
{
  std::shared_timed_mutex mtx;
  {
    // 遅延ロックする(ここではロックを取得しない)
    std::shared_lock<std::shared_timed_mutex> lock(mtx, std::defer_lock);

    // 共有ロックの取得を試みる(3秒でタイムアウト)
    if (!lock.try_lock_for(std::chrono::seconds(3))) {
      // 共有ロックの取得に失敗
      std::error_code ec(static_cast<int>(std::errc::device_or_resource_busy), std::generic_category());
      throw std::system_error(ec);
    }

    assert(lock.owns_lock() == true);
  }
}

出力

バージョン

言語

  • C++14

処理系