New issue
Advanced search Search tips

Issue 736531 link

Starred by 2 users

Issue metadata

Status: Fixed
Owner:
Closed: Nov 13
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: ----
Pri: 3
Type: Bug

Blocking:
issue 752144



Sign in to add a comment

Explain thread safety and sequences, e.g. how sequences that "hop threads" are thread-safe

Project Member Reported by michae...@chromium.org, Jun 23 2017

Issue description

I don't have much experience with TaskRunners or Sequences, but I'm being asked to migrate stuff. One question I keep running into: what's the difference between "thread-safe" and "sequenced"? And how is something that is "not thread-safe", like base::WeakPtr, okay to use in a SequencedTaskRunner (which doesn't even guarantee thread-safety)?

Specifically, these two statements in the docs don't sound compatible. [1]

"Sequences inherently provide thread-safety."
"sequences can hop threads instead of being stuck behind unrelated work on a dedicated thread"

If a sequence runs tasks on different threads, how is it thread-safe? It does seem that, given some very complicated conditions[2], a SequencedTaskRunner "provides most (but not all) of the same guarantees" as "tasks are run on a single dedicated thread".

In the example given for base::WeakPtr [3], does the example's correctness rely on the conditions expressed in SequencedTaskRunner's docs in [2]? That is, do Compute and Store need to be nestable/non-nestable, be guaranteed not to post other tasks, and so forth? It seems like it would be a lot of work to ensure that each function, and the functions it calls, etc. never does any of those things.

Should the docs be expanded to explain these concepts, or do we need a multithreading-noob-friendly intro version?

[1] https://chromium.googlesource.com/chromium/src/+/master/docs/threading_and_tasks.md
[2] https://cs.chromium.org/chromium/src/base/sequenced_task_runner.h?type=cs&sq=package:chromium&q=sequencedtaskrunner&l=38-65
[3] https://chromium.googlesource.com/chromium/src/+/master/docs/threading_and_tasks.md#Using-base_WeakPtr
 

Comment 1 Deleted

Comment 2 by gab@chromium.org, Jun 26 2017

Thread-safety != Thread-affinity

Thread safety merely requires:
 1) mutual exclusion (tasks run one after the other)
 2) memory consistency (each task in the sequence unracily sees side-effects of tasks that preceded it in the same sequence)

SequencedTaskRunner provides this.

(don't worry about non-nestable, it's rarely required and if you wonder you probably don't need it)

Does that answer your question? I'm happy to add a short thread-affinity section to the docs if you think that'd help.

Comment 3 by gab@chromium.org, May 3 2018

Blocking: 752144
Status: Assigned (was: Untriaged)
This bug has an owner, thus, it's been triaged. Changing status to "assigned".
Project Member

Comment 5 by bugdroid1@chromium.org, Nov 13

The following revision refers to this bug:
  https://chromium.googlesource.com/chromium/src.git/+/a3ccc977026f6e9633b8b77ae6acfcee0f559b36

commit a3ccc977026f6e9633b8b77ae6acfcee0f559b36
Author: Gabriel Charette <gab@chromium.org>
Date: Tue Nov 13 14:43:12 2018

[Threading docs] Clarify why a sequence is thread-safe.

As requested on the bug.

R=fdoray@chromium.org

Bug:  736531 
Change-Id: I8f1fda7cc7e5973c72b2c9ff970c8a88620e0ca5
Reviewed-on: https://chromium-review.googlesource.com/c/1332768
Commit-Queue: François Doray <fdoray@chromium.org>
Reviewed-by: François Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607585}
[modify] https://crrev.com/a3ccc977026f6e9633b8b77ae6acfcee0f559b36/docs/threading_and_tasks.md

Status: Fixed (was: Assigned)
Better late than never :) -- let me know if that's not sufficient.

Sign in to add a comment