Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Is deadline_timer.cancel() safe to be called in multithread executors?

オープン
#447 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
ドキュメント
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
cpp
領域
networking

調査の方向性

リンクされている deadline_timer のスレッドセーフ性に関する注記と、steady_timer、async_wait、cancel、post、io_context、thread_pool を使用する例から始めてください。期限の処理中に別のスレッドからのキャンセルがサポートされているかどうかを確認し、どのようなドキュメントの明確化が必要になるかを特定してください。特定のファイルやテストは指定されていません。

索引モデルが issue の本文から書いたものです。

説明

Hello!
I failed to google that, or find exact answer in docs. Hope I can get an answer hee!

So deadline_timer have this note:

Thread Safety
Distinct objects: Safe.
Shared objects: Unsafe.

My problem is, I can't understand if I (as lib's user) 'share' object with asio itself.
Is code below safe in real world scenario when timer->cancel() can be called in one moment with timer expiration?

auto ioc = boost::asio::io_context();
for (auto i = 0; i < 10000000; ++i)
{
    auto timer = std::make_shared<boost::asio::steady_timer>(ioc);
    timer->expires_at(std::chrono::steady_clock::now());
    timer->async_wait([timer](auto){/*exact work doesn't matter*/});
    auto cancelation = [timer]() { timer->cancel(); };
    boost::asio::post(ioc, cancelation);
}
auto pool_size = 100;
auto pool = boost::asio::thread_pool(pool_size);
for (auto i = 0; i < pool_size; i++) {boost::asio::post(pool, [&ioc](){ioc.run();});}

Or in words: Can I call timer.cancel() method from arbitary thread* if timer was scheduled using executor(io_context/thread_pool) serviced by multiple threads?
* only one timer.cancel() at time, ofcourse, because timer public methods aren't thread safe between each other.

I ask that question because I want to schedule 'disposable' timers with option to cancel them without creating strand for each of such timers. Just using given executor.

主要言語
C++
スター
1.6k
フォーク
485
PR マージ指標
30日以内にマージされた PR はありません

環境構築

  • Dockerfile・Docker Compose ファイルなし
  • プルリクエストのテンプレートあり
  • コントリビューションガイドなし

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

boostorg/asio のほかの issue

boostorg/asio の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。