blob: 840d5f30fee252a53b273c64779413fc60a3b56b [file] [log] [blame] [view]
fdoraybacba4a22017-05-10 21:10:001# Threading and Tasks in Chrome
2
3[TOC]
4
Gabriel Charette8917f4c2018-11-22 15:50:285Note: See [Threading and Tasks FAQ](threading_and_tasks_faq.md) for more
6examples.
7
fdoraybacba4a22017-05-10 21:10:008## Overview
9
Gabriel Charette39db4c62019-04-29 19:52:3810Chrome has a [multi-process
11architecture](https://www.chromium.org/developers/design-documents/multi-process-architecture)
12and each process is heavily multi-threaded. In this document we will go over the
13basic threading system shared by each process. The main goal is to keep the main
14thread (a.k.a. "UI" thread in the browser process) and IO thread (each process'
15thread for handling
16[IPC](https://en.wikipedia.org/wiki/Inter-process_communication)) responsive.
17This means offloading any blocking I/O or other expensive operations to other
18threads. Our approach is to use message passing as the way of communicating
19between threads. We discourage locking and thread-safe objects. Instead, objects
20live on only one (often virtual -- we'll get to that later!) thread and we pass
21messages between those threads for communication.
22
23This documentation assumes familiarity with computer science
24[threading concepts](https://en.wikipedia.org/wiki/Thread_(computing)).
Gabriel Charette90480312018-02-16 15:10:0525
Gabriel Charette364a16a2019-02-06 21:12:1526### Nomenclature
Gabriel Charette39db4c62019-04-29 19:52:3827
28## Core Concepts
29 * **Task**: A unit of work to be processed. Effectively a function pointer with
30 optionally associated state. In Chrome this is `base::Callback` created via
31 `base::Bind`
32 ([documentation](https://chromium.googlesource.com/chromium/src/+/HEAD/docs/callback.md)).
33 * **Task queue**: A queue of tasks to be processed.
34 * **Physical thread**: An operating system provided thread (e.g. pthread on
35 POSIX or CreateThread() on Windows). The Chrome cross-platform abstraction
36 is `base::PlatformThread`. You should pretty much never use this directly.
37 * **`base::Thread`**: A physical thread forever processing messages from a
38 dedicated task queue until Quit(). You should pretty much never be creating
39 your own `base::Thread`'s.
40 * **Thread pool**: A pool of physical threads with a shared task queue. In
41 Chrome, this is `base::ThreadPool`. There's exactly one instance per Chrome
42 process, it serves tasks posted through
43 [`base/task/post_task.h`](https://cs.chromium.org/chromium/src/base/task/post_task.h)
44 and as such you should rarely need to use the `base::ThreadPool` API
45 directly (more on posting tasks later).
46 * **Sequence** or **Virtual thread**: A chrome-managed thread of execution.
47 Like a physical thread, only one task can run on a given sequence / virtual
48 thread at any given moment and each task sees the side-effects of the
49 preceding tasks. Tasks are executed sequentially but may hop physical
50 threads between each one.
51 * **Task runner**: An interface through which tasks can be posted. In Chrome
52 this is `base::TaskRunner`.
53 * **Sequenced task runner**: A task runner which guarantees that tasks posted
54 to it will run sequentially, in posted order. Each such task is guaranteed to
55 see the side-effects of the task preceding it. Tasks posted to a sequenced
56 task runner are typically processed by a single thread (virtual or physical).
57 In Chrome this is `base::SequencedTaskRunner` which is-a
58 `base::TaskRunner`.
59 * **Single-thread task runner**: A sequenced task runner which guarantees that
60 all tasks will be processed by the same physical thread. In Chrome this is
61 `base::SingleThreadTaskRunner` which is-a `base::SequencedTaskRunner`. We
62 [prefer sequences to threads](#prefer-sequences-to-physical-threads) whenever
63 possible.
64
65## Threading Lexicon
66Note to the reader: the following terms are an attempt to bridge the gap between
67common threading nomenclature and the way we use them in Chrome. It might be a
68bit heavy if you're just getting started. Should this be hard to parse, consider
69skipping to the more detailed sections below and referring back to this as
70necessary.
71
72 * **Thread-unsafe**: The vast majority of types in Chrome are thread-unsafe
73 (by design). Access to such types/methods must be externally synchronized.
74 Typically thread-unsafe types require that all tasks accessing their state be
75 posted to the same `base::SequencedTaskRunner` and they verify this in debug
76 builds with a `SEQUENCE_CHECKER` member. Locks are also an option to
77 synchronize access but in Chrome we strongly
78 [prefer sequences to locks](#Using-Sequences-Instead-of-Locks).
Gabriel Charette364a16a2019-02-06 21:12:1579 * **Thread-affine**: Such types/methods need to be always accessed from the
Gabriel Charetteb984d672019-02-12 21:53:2780 same physical thread (i.e. from the same `base::SingleThreadTaskRunner`) and
Gabriel Charette39db4c62019-04-29 19:52:3881 typically have a `THREAD_CHECKER` member to verify that they are. Short of
82 using a third-party API or having a leaf dependency which is thread-affine:
83 there's pretty much no reason for a type to be thread-affine in Chrome.
84 Note that `base::SingleThreadTaskRunner` is-a `base::SequencedTaskRunner` so
Gabriel Charetteb984d672019-02-12 21:53:2785 thread-affine is a subset of thread-unsafe. Thread-affine is also sometimes
86 referred to as **thread-hostile**.
Gabriel Charette364a16a2019-02-06 21:12:1587 * **Thread-safe**: Such types/methods can be safely accessed concurrently.
Gabriel Charetteb984d672019-02-12 21:53:2788 * **Thread-compatible**: Such types provide safe concurrent access to const
89 methods but require synchronization for non-const (or mixed const/non-const
Gabriel Charette39db4c62019-04-29 19:52:3890 access). Chrome doesn't expose reader-writer locks; as such, the only use
Gabriel Charetteb984d672019-02-12 21:53:2791 case for this is objects (typically globals) which are initialized once in a
Gabriel Charette364a16a2019-02-06 21:12:1592 thread-safe manner (either in the single-threaded phase of startup or lazily
93 through a thread-safe static-local-initialization paradigm a la
Gabriel Charetteb984d672019-02-12 21:53:2794 `base::NoDestructor`) and forever after immutable.
95 * **Immutable**: A subset of thread-compatible types which cannot be modified
96 after construction.
Gabriel Charette364a16a2019-02-06 21:12:1597 * **Sequence-friendly**: Such types/methods are thread-unsafe types which
98 support being invoked from a `base::SequencedTaskRunner`. Ideally this would
99 be the case for all thread-unsafe types but legacy code sometimes has
100 overzealous checks that enforce thread-affinity in mere thread-unsafe
Gabriel Charette39db4c62019-04-29 19:52:38101 scenarios. See [Prefer Sequences to
102 Threads](#prefer-sequences-to-physical-threads) below for more details.
Gabriel Charette364a16a2019-02-06 21:12:15103
fdoraybacba4a22017-05-10 21:10:00104### Threads
105
106Every Chrome process has
107
108* a main thread
Gabriel Charette39db4c62019-04-29 19:52:38109 * in the browser process (BrowserThread::UI): updates the UI
110 * in renderer processes (Blink main thread): runs most of Blink
fdoraybacba4a22017-05-10 21:10:00111* an IO thread
Gabriel Charette39db4c62019-04-29 19:52:38112 * in the browser process (BrowserThread::IO): handles IPCs and network requests
fdoraybacba4a22017-05-10 21:10:00113 * in renderer processes: handles IPCs
114* a few more special-purpose threads
115* and a pool of general-purpose threads
116
117Most threads have a loop that gets tasks from a queue and runs them (the queue
118may be shared between multiple threads).
119
120### Tasks
121
122A task is a `base::OnceClosure` added to a queue for asynchronous execution.
123
124A `base::OnceClosure` stores a function pointer and arguments. It has a `Run()`
125method that invokes the function pointer using the bound arguments. It is
126created using `base::BindOnce`. (ref. [Callback<> and Bind()
127documentation](callback.md)).
128
129```
130void TaskA() {}
131void TaskB(int v) {}
132
133auto task_a = base::BindOnce(&TaskA);
134auto task_b = base::BindOnce(&TaskB, 42);
135```
136
137A group of tasks can be executed in one of the following ways:
138
139* [Parallel](#Posting-a-Parallel-Task): No task execution ordering, possibly all
140 at once on any thread
141* [Sequenced](#Posting-a-Sequenced-Task): Tasks executed in posting order, one
142 at a time on any thread.
143* [Single Threaded](#Posting-Multiple-Tasks-to-the-Same-Thread): Tasks executed
144 in posting order, one at a time on a single thread.
145 * [COM Single Threaded](#Posting-Tasks-to-a-COM-Single-Thread-Apartment-STA_Thread-Windows_):
146 A variant of single threaded with COM initialized.
147
Gabriel Charette39db4c62019-04-29 19:52:38148### Prefer Sequences to Physical Threads
gab2a4576052017-06-07 23:36:12149
Gabriel Charette39db4c62019-04-29 19:52:38150Sequenced execution (on virtual threads) is strongly preferred to
151single-threaded execution (on physical threads). Except for types/methods bound
152to the main thread (UI) or IO threads: thread-safety is better achieved via
153`base::SequencedTaskRunner` than through managing your own physical threads
154(ref. [Posting a Sequenced Task](#posting-a-sequenced-task) below).
gab2a4576052017-06-07 23:36:12155
Gabriel Charette39db4c62019-04-29 19:52:38156All APIs which are exposed for "current physical thread" have an equivalent for
157"current sequence"
158([mapping](threading_and_tasks_faq.md#How-to-migrate-from-SingleThreadTaskRunner-to-SequencedTaskRunner)).
gab2a4576052017-06-07 23:36:12159
Gabriel Charette39db4c62019-04-29 19:52:38160If you find yourself writing a sequence-friendly type and it fails
161thread-affinity checks (e.g., `THREAD_CHECKER`) in a leaf dependency: consider
162making that dependency sequence-friendly as well. Most core APIs in Chrome are
163sequence-friendly, but some legacy types may still over-zealously use
164ThreadChecker/ThreadTaskRunnerHandle/SingleThreadTaskRunner when they could
165instead rely on the "current sequence" and no longer be thread-affine.
fdoraybacba4a22017-05-10 21:10:00166
167## Posting a Parallel Task
168
Gabriel Charette52fa3ae2019-04-15 21:44:37169### Direct Posting to the Thread Pool
fdoraybacba4a22017-05-10 21:10:00170
171A task that can run on any thread and doesn’t have ordering or mutual exclusion
172requirements with other tasks should be posted using one of the
173`base::PostTask*()` functions defined in
Gabriel Charette04b138f2018-08-06 00:03:22174[`base/task/post_task.h`](https://cs.chromium.org/chromium/src/base/task/post_task.h).
fdoraybacba4a22017-05-10 21:10:00175
176```cpp
177base::PostTask(FROM_HERE, base::BindOnce(&Task));
178```
179
180This posts tasks with default traits.
181
182The `base::PostTask*WithTraits()` functions allow the caller to provide
183additional details about the task via TaskTraits (ref.
184[Annotating Tasks with TaskTraits](#Annotating-Tasks-with-TaskTraits)).
185
186```cpp
187base::PostTaskWithTraits(
Gabriel Charetteb10aeebc2018-07-26 20:15:00188 FROM_HERE, {base::TaskPriority::BEST_EFFORT, MayBlock()},
fdoraybacba4a22017-05-10 21:10:00189 base::BindOnce(&Task));
190```
191
fdoray52bf5552017-05-11 12:43:59192### Posting via a TaskRunner
fdoraybacba4a22017-05-10 21:10:00193
194A parallel
Gabriel Charette39db4c62019-04-29 19:52:38195[`base::TaskRunner`](https://cs.chromium.org/chromium/src/base/task_runner.h) is
196an alternative to calling `base::PostTask*()` directly. This is mainly useful
197when it isn’t known in advance whether tasks will be posted in parallel, in
198sequence, or to a single-thread (ref. [Posting a Sequenced
199Task](#Posting-a-Sequenced-Task), [Posting Multiple Tasks to the Same
200Thread](#Posting-Multiple-Tasks-to-the-Same-Thread)). Since `base::TaskRunner`
201is the base class of `base::SequencedTaskRunner` and
202`base::SingleThreadTaskRunner`, a `scoped_refptr<TaskRunner>` member can hold a
203`base::TaskRunner`, a `base::SequencedTaskRunner` or a
204`base::SingleThreadTaskRunner`.
fdoraybacba4a22017-05-10 21:10:00205
206```cpp
207class A {
208 public:
209 A() = default;
210
fdoraybacba4a22017-05-10 21:10:00211 void DoSomething() {
fdoraybacba4a22017-05-10 21:10:00212 task_runner_->PostTask(FROM_HERE, base::BindOnce(&A));
213 }
214
215 private:
216 scoped_refptr<base::TaskRunner> task_runner_ =
217 base::CreateTaskRunnerWithTraits({base::TaskPriority::USER_VISIBLE});
218};
219```
220
221Unless a test needs to control precisely how tasks are executed, it is preferred
222to call `base::PostTask*()` directly (ref. [Testing](#Testing) for less invasive
223ways of controlling tasks in tests).
224
225## Posting a Sequenced Task
226
227A sequence is a set of tasks that run one at a time in posting order (not
228necessarily on the same thread). To post tasks as part of a sequence, use a
Gabriel Charette39db4c62019-04-29 19:52:38229[`base::SequencedTaskRunner`](https://cs.chromium.org/chromium/src/base/sequenced_task_runner.h).
fdoraybacba4a22017-05-10 21:10:00230
231### Posting to a New Sequence
232
Gabriel Charette39db4c62019-04-29 19:52:38233A `base::SequencedTaskRunner` can be created by
fdoraybacba4a22017-05-10 21:10:00234`base::CreateSequencedTaskRunnerWithTraits()`.
235
236```cpp
237scoped_refptr<SequencedTaskRunner> sequenced_task_runner =
238 base::CreateSequencedTaskRunnerWithTraits(...);
239
240// TaskB runs after TaskA completes.
241sequenced_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskA));
242sequenced_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskB));
243```
244
245### Posting to the Current Sequence
246
Gabriel Charette39db4c62019-04-29 19:52:38247The `base::SequencedTaskRunner` to which the current task was posted can be
248obtained via
249[`base::SequencedTaskRunnerHandle::Get()`](https://cs.chromium.org/chromium/src/base/threading/sequenced_task_runner_handle.h).
fdoraybacba4a22017-05-10 21:10:00250
251*** note
Gabriel Charette39db4c62019-04-29 19:52:38252**NOTE:** it is invalid to call `base::SequencedTaskRunnerHandle::Get()` from a
fdoraybacba4a22017-05-10 21:10:00253parallel task, but it is valid from a single-threaded task (a
Gabriel Charette39db4c62019-04-29 19:52:38254`base::SingleThreadTaskRunner` is a `base::SequencedTaskRunner`).
fdoraybacba4a22017-05-10 21:10:00255***
256
257```cpp
258// The task will run after any task that has already been posted
259// to the SequencedTaskRunner to which the current task was posted
260// (in particular, it will run after the current task completes).
261// It is also guaranteed that it won’t run concurrently with any
262// task posted to that SequencedTaskRunner.
263base::SequencedTaskRunnerHandle::Get()->
264 PostTask(FROM_HERE, base::BindOnce(&Task));
265```
266
267## Using Sequences Instead of Locks
268
269Usage of locks is discouraged in Chrome. Sequences inherently provide
Gabriel Charettea3ccc972018-11-13 14:43:12270thread-safety. Prefer classes that are always accessed from the same
271sequence to managing your own thread-safety with locks.
272
273**Thread-safe but not thread-affine; how so?** Tasks posted to the same sequence
274will run in sequential order. After a sequenced task completes, the next task
275may be picked up by a different worker thread, but that task is guaranteed to
276see any side-effects caused by the previous one(s) on its sequence.
fdoraybacba4a22017-05-10 21:10:00277
278```cpp
279class A {
280 public:
281 A() {
282 // Do not require accesses to be on the creation sequence.
isherman8c33b8a2017-06-27 19:18:30283 DETACH_FROM_SEQUENCE(sequence_checker_);
fdoraybacba4a22017-05-10 21:10:00284 }
285
286 void AddValue(int v) {
287 // Check that all accesses are on the same sequence.
isherman8c33b8a2017-06-27 19:18:30288 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
fdoraybacba4a22017-05-10 21:10:00289 values_.push_back(v);
290}
291
292 private:
isherman8c33b8a2017-06-27 19:18:30293 SEQUENCE_CHECKER(sequence_checker_);
fdoraybacba4a22017-05-10 21:10:00294
295 // No lock required, because all accesses are on the
296 // same sequence.
297 std::vector<int> values_;
298};
299
300A a;
301scoped_refptr<SequencedTaskRunner> task_runner_for_a = ...;
Mike Bjorged3a09842018-05-15 18:37:28302task_runner_for_a->PostTask(FROM_HERE,
303 base::BindOnce(&A::AddValue, base::Unretained(&a), 42));
304task_runner_for_a->PostTask(FROM_HERE,
305 base::BindOnce(&A::AddValue, base::Unretained(&a), 27));
fdoraybacba4a22017-05-10 21:10:00306
307// Access from a different sequence causes a DCHECK failure.
308scoped_refptr<SequencedTaskRunner> other_task_runner = ...;
309other_task_runner->PostTask(FROM_HERE,
Mike Bjorged3a09842018-05-15 18:37:28310 base::BindOnce(&A::AddValue, base::Unretained(&a), 1));
fdoraybacba4a22017-05-10 21:10:00311```
312
Gabriel Charette90480312018-02-16 15:10:05313Locks should only be used to swap in a shared data structure that can be
314accessed on multiple threads. If one thread updates it based on expensive
315computation or through disk access, then that slow work should be done without
Gabriel Charette39db4c62019-04-29 19:52:38316holding the lock. Only when the result is available should the lock be used to
317swap in the new data. An example of this is in PluginList::LoadPlugins
318([`content/browser/plugin_list.cc`](https://cs.chromium.org/chromium/src/content/browser/plugin_list.cc).
319If you must use locks,
Gabriel Charette90480312018-02-16 15:10:05320[here](https://www.chromium.org/developers/lock-and-condition-variable) are some
321best practices and pitfalls to avoid.
322
Gabriel Charette39db4c62019-04-29 19:52:38323In order to write non-blocking code, many APIs in Chrome are asynchronous.
Gabriel Charette90480312018-02-16 15:10:05324Usually this means that they either need to be executed on a particular
325thread/sequence and will return results via a custom delegate interface, or they
326take a `base::Callback<>` object that is called when the requested operation is
327completed. Executing work on a specific thread/sequence is covered in the
328PostTask sections above.
329
fdoraybacba4a22017-05-10 21:10:00330## Posting Multiple Tasks to the Same Thread
331
332If multiple tasks need to run on the same thread, post them to a
Gabriel Charette39db4c62019-04-29 19:52:38333[`base::SingleThreadTaskRunner`](https://cs.chromium.org/chromium/src/base/single_thread_task_runner.h).
334All tasks posted to the same `base::SingleThreadTaskRunner` run on the same thread in
fdoraybacba4a22017-05-10 21:10:00335posting order.
336
337### Posting to the Main Thread or to the IO Thread in the Browser Process
338
Eric Seckler6cf08db82018-08-30 12:01:55339To post tasks to the main thread or to the IO thread, use
340`base::PostTaskWithTraits()` or get the appropriate SingleThreadTaskRunner using
341`base::CreateSingleThreadTaskRunnerWithTraits`, supplying a `BrowserThread::ID`
342as trait. For this, you'll also need to include
343[`content/public/browser/browser_task_traits.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_task_traits.h).
fdoraybacba4a22017-05-10 21:10:00344
345```cpp
Eric Seckler6cf08db82018-08-30 12:01:55346base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::UI}, ...);
fdoraybacba4a22017-05-10 21:10:00347
Eric Seckler6cf08db82018-08-30 12:01:55348base::CreateSingleThreadTaskRunnerWithTraits({content::BrowserThread::IO})
fdoraybacba4a22017-05-10 21:10:00349 ->PostTask(FROM_HERE, ...);
350```
351
352The main thread and the IO thread are already super busy. Therefore, prefer
fdoray52bf5552017-05-11 12:43:59353posting to a general purpose thread when possible (ref.
354[Posting a Parallel Task](#Posting-a-Parallel-Task),
355[Posting a Sequenced task](#Posting-a-Sequenced-Task)).
356Good reasons to post to the main thread are to update the UI or access objects
357that are bound to it (e.g. `Profile`). A good reason to post to the IO thread is
358to access the internals of components that are bound to it (e.g. IPCs, network).
359Note: It is not necessary to have an explicit post task to the IO thread to
360send/receive an IPC or send/receive data on the network.
fdoraybacba4a22017-05-10 21:10:00361
362### Posting to the Main Thread in a Renderer Process
363TODO
364
365### Posting to a Custom SingleThreadTaskRunner
366
367If multiple tasks need to run on the same thread and that thread doesn’t have to
Gabriel Charette39db4c62019-04-29 19:52:38368be the main thread or the IO thread, post them to a `base::SingleThreadTaskRunner`
fdoraybacba4a22017-05-10 21:10:00369created by `base::CreateSingleThreadTaskRunnerWithTraits`.
370
371```cpp
372scoped_refptr<SequencedTaskRunner> single_thread_task_runner =
373 base::CreateSingleThreadTaskRunnerWithTraits(...);
374
375// TaskB runs after TaskA completes. Both tasks run on the same thread.
376single_thread_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskA));
377single_thread_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskB));
378```
379
Gabriel Charette39db4c62019-04-29 19:52:38380Remember that we [prefer sequences to physical
381threads](#prefer-sequences-to-physical-threads) and that this thus should rarely
382be necessary.
fdoraybacba4a22017-05-10 21:10:00383
384### Posting to the Current Thread
385
386*** note
387**IMPORTANT:** To post a task that needs mutual exclusion with the current
388sequence of tasks but doesn’t absolutely need to run on the current thread, use
Gabriel Charette39db4c62019-04-29 19:52:38389`base::SequencedTaskRunnerHandle::Get()` instead of
390`base::ThreadTaskRunnerHandle::Get()` (ref. [Posting to the Current
391Sequence](#Posting-to-the-Current-Sequence)). That will better document the
392requirements of the posted task and will avoid unnecessarily making your API
393thread-affine. In a single-thread task, `base::SequencedTaskRunnerHandle::Get()`
394is equivalent to `base::ThreadTaskRunnerHandle::Get()`.
fdoraybacba4a22017-05-10 21:10:00395***
396
Gabriel Charette39db4c62019-04-29 19:52:38397To post a task to the current thread, use
398[`base::ThreadTaskRunnerHandle`](https://cs.chromium.org/chromium/src/base/threading/thread_task_runner_handle.h).
fdoraybacba4a22017-05-10 21:10:00399
400```cpp
401// The task will run on the current thread in the future.
402base::ThreadTaskRunnerHandle::Get()->PostTask(
403 FROM_HERE, base::BindOnce(&Task));
404```
405
406*** note
Gabriel Charette39db4c62019-04-29 19:52:38407**NOTE:** It is invalid to call `base::ThreadTaskRunnerHandle::Get()` from a parallel
fdoraybacba4a22017-05-10 21:10:00408or a sequenced task.
409***
410
411## Posting Tasks to a COM Single-Thread Apartment (STA) Thread (Windows)
412
413Tasks that need to run on a COM Single-Thread Apartment (STA) thread must be
Gabriel Charette39db4c62019-04-29 19:52:38414posted to a `base::SingleThreadTaskRunner` returned by
415`base::CreateCOMSTATaskRunnerWithTraits()`. As mentioned in [Posting Multiple
416Tasks to the Same Thread](#Posting-Multiple-Tasks-to-the-Same-Thread), all tasks
417posted to the same `base::SingleThreadTaskRunner` run on the same thread in
418posting order.
fdoraybacba4a22017-05-10 21:10:00419
420```cpp
421// Task(A|B|C)UsingCOMSTA will run on the same COM STA thread.
422
423void TaskAUsingCOMSTA() {
424 // [ This runs on a COM STA thread. ]
425
426 // Make COM STA calls.
427 // ...
428
429 // Post another task to the current COM STA thread.
430 base::ThreadTaskRunnerHandle::Get()->PostTask(
431 FROM_HERE, base::BindOnce(&TaskCUsingCOMSTA));
432}
433void TaskBUsingCOMSTA() { }
434void TaskCUsingCOMSTA() { }
435
436auto com_sta_task_runner = base::CreateCOMSTATaskRunnerWithTraits(...);
437com_sta_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskAUsingCOMSTA));
438com_sta_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskBUsingCOMSTA));
439```
440
441## Annotating Tasks with TaskTraits
442
Gabriel Charette39db4c62019-04-29 19:52:38443[`base::TaskTraits`](https://cs.chromium.org/chromium/src/base/task/task_traits.h)
Gabriel Charette52fa3ae2019-04-15 21:44:37444encapsulate information about a task that helps the thread pool make better
fdoraybacba4a22017-05-10 21:10:00445scheduling decisions.
446
Gabriel Charette39db4c62019-04-29 19:52:38447All `base::PostTask*()` functions in
Gabriel Charette04b138f2018-08-06 00:03:22448[`base/task/post_task.h`](https://cs.chromium.org/chromium/src/base/task/post_task.h)
Gabriel Charette39db4c62019-04-29 19:52:38449have an overload that takes `base::TaskTraits` as argument and one that doesn’t.
450The overload that doesn’t take `base::TaskTraits` as argument is appropriate for
451tasks that:
fdoraybacba4a22017-05-10 21:10:00452- Don’t block (ref. MayBlock and WithBaseSyncPrimitives).
453- Prefer inheriting the current priority to specifying their own.
Gabriel Charette52fa3ae2019-04-15 21:44:37454- Can either block shutdown or be skipped on shutdown (thread pool is free to
455 choose a fitting default).
fdoraybacba4a22017-05-10 21:10:00456Tasks that don’t match this description must be posted with explicit TaskTraits.
457
Gabriel Charette04b138f2018-08-06 00:03:22458[`base/task/task_traits.h`](https://cs.chromium.org/chromium/src/base/task/task_traits.h)
Eric Seckler6cf08db82018-08-30 12:01:55459provides exhaustive documentation of available traits. The content layer also
460provides additional traits in
461[`content/public/browser/browser_task_traits.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_task_traits.h)
462to facilitate posting a task onto a BrowserThread.
463
Gabriel Charette39db4c62019-04-29 19:52:38464Below are some examples of how to specify `base::TaskTraits`.
fdoraybacba4a22017-05-10 21:10:00465
466```cpp
467// This task has no explicit TaskTraits. It cannot block. Its priority
468// is inherited from the calling context (e.g. if it is posted from
Gabriel Charette141a442582018-07-27 21:23:25469// a BEST_EFFORT task, it will have a BEST_EFFORT priority). It will either
fdoraybacba4a22017-05-10 21:10:00470// block shutdown or be skipped on shutdown.
471base::PostTask(FROM_HERE, base::BindOnce(...));
472
Gabriel Charette52fa3ae2019-04-15 21:44:37473// This task has the highest priority. The thread pool will try to
Gabriel Charette141a442582018-07-27 21:23:25474// run it before USER_VISIBLE and BEST_EFFORT tasks.
fdoraybacba4a22017-05-10 21:10:00475base::PostTaskWithTraits(
476 FROM_HERE, {base::TaskPriority::USER_BLOCKING},
477 base::BindOnce(...));
478
479// This task has the lowest priority and is allowed to block (e.g. it
480// can read a file from disk).
481base::PostTaskWithTraits(
Gabriel Charetteb10aeebc2018-07-26 20:15:00482 FROM_HERE, {base::TaskPriority::BEST_EFFORT, base::MayBlock()},
fdoraybacba4a22017-05-10 21:10:00483 base::BindOnce(...));
484
485// This task blocks shutdown. The process won't exit before its
486// execution is complete.
487base::PostTaskWithTraits(
488 FROM_HERE, {base::TaskShutdownBehavior::BLOCK_SHUTDOWN},
489 base::BindOnce(...));
Eric Seckler6cf08db82018-08-30 12:01:55490
491// This task will run on the Browser UI thread.
492base::PostTaskWithTraits(
493 FROM_HERE, {content::BrowserThread::UI},
494 base::BindOnce(...));
fdoraybacba4a22017-05-10 21:10:00495```
496
497## Keeping the Browser Responsive
498
499Do not perform expensive work on the main thread, the IO thread or any sequence
500that is expected to run tasks with a low latency. Instead, perform expensive
501work asynchronously using `base::PostTaskAndReply*()` or
Gabriel Charette39db4c62019-04-29 19:52:38502`base::SequencedTaskRunner::PostTaskAndReply()`. Note that
503asynchronous/overlapped I/O on the IO thread are fine.
fdoraybacba4a22017-05-10 21:10:00504
505Example: Running the code below on the main thread will prevent the browser from
506responding to user input for a long time.
507
508```cpp
509// GetHistoryItemsFromDisk() may block for a long time.
510// AddHistoryItemsToOmniboxDropDown() updates the UI and therefore must
511// be called on the main thread.
512AddHistoryItemsToOmniboxDropdown(GetHistoryItemsFromDisk("keyword"));
513```
514
515The code below solves the problem by scheduling a call to
516`GetHistoryItemsFromDisk()` in a thread pool followed by a call to
517`AddHistoryItemsToOmniboxDropdown()` on the origin sequence (the main thread in
518this case). The return value of the first call is automatically provided as
519argument to the second call.
520
521```cpp
522base::PostTaskWithTraitsAndReplyWithResult(
523 FROM_HERE, {base::MayBlock()},
524 base::BindOnce(&GetHistoryItemsFromDisk, "keyword"),
525 base::BindOnce(&AddHistoryItemsToOmniboxDropdown));
526```
527
528## Posting a Task with a Delay
529
530### Posting a One-Off Task with a Delay
531
532To post a task that must run once after a delay expires, use
Gabriel Charette39db4c62019-04-29 19:52:38533`base::PostDelayedTask*()` or `base::TaskRunner::PostDelayedTask()`.
fdoraybacba4a22017-05-10 21:10:00534
535```cpp
536base::PostDelayedTaskWithTraits(
Gabriel Charetteb10aeebc2018-07-26 20:15:00537 FROM_HERE, {base::TaskPriority::BEST_EFFORT}, base::BindOnce(&Task),
fdoraybacba4a22017-05-10 21:10:00538 base::TimeDelta::FromHours(1));
539
540scoped_refptr<base::SequencedTaskRunner> task_runner =
Gabriel Charetteb10aeebc2018-07-26 20:15:00541 base::CreateSequencedTaskRunnerWithTraits({base::TaskPriority::BEST_EFFORT});
fdoraybacba4a22017-05-10 21:10:00542task_runner->PostDelayedTask(
543 FROM_HERE, base::BindOnce(&Task), base::TimeDelta::FromHours(1));
544```
545
546*** note
547**NOTE:** A task that has a 1-hour delay probably doesn’t have to run right away
Gabriel Charetteb10aeebc2018-07-26 20:15:00548when its delay expires. Specify `base::TaskPriority::BEST_EFFORT` to prevent it
fdoraybacba4a22017-05-10 21:10:00549from slowing down the browser when its delay expires.
550***
551
552### Posting a Repeating Task with a Delay
553To post a task that must run at regular intervals,
554use [`base::RepeatingTimer`](https://cs.chromium.org/chromium/src/base/timer/timer.h).
555
556```cpp
557class A {
558 public:
559 ~A() {
560 // The timer is stopped automatically when it is deleted.
561 }
562 void StartDoingStuff() {
563 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(1),
564 this, &MyClass::DoStuff);
565 }
566 void StopDoingStuff() {
567 timer_.Stop();
568 }
569 private:
570 void DoStuff() {
571 // This method is called every second on the sequence that invoked
572 // StartDoingStuff().
573 }
574 base::RepeatingTimer timer_;
575};
576```
577
578## Cancelling a Task
579
580### Using base::WeakPtr
581
582[`base::WeakPtr`](https://cs.chromium.org/chromium/src/base/memory/weak_ptr.h)
583can be used to ensure that any callback bound to an object is canceled when that
584object is destroyed.
585
586```cpp
587int Compute() { … }
588
589class A {
590 public:
591 A() : weak_ptr_factory_(this) {}
592
593 void ComputeAndStore() {
594 // Schedule a call to Compute() in a thread pool followed by
595 // a call to A::Store() on the current sequence. The call to
596 // A::Store() is canceled when |weak_ptr_factory_| is destroyed.
597 // (guarantees that |this| will not be used-after-free).
598 base::PostTaskAndReplyWithResult(
599 FROM_HERE, base::BindOnce(&Compute),
600 base::BindOnce(&A::Store, weak_ptr_factory_.GetWeakPtr()));
601 }
602
603 private:
604 void Store(int value) { value_ = value; }
605
606 int value_;
607 base::WeakPtrFactory<A> weak_ptr_factory_;
608};
609```
610
611Note: `WeakPtr` is not thread-safe: `GetWeakPtr()`, `~WeakPtrFactory()`, and
612`Compute()` (bound to a `WeakPtr`) must all run on the same sequence.
613
614### Using base::CancelableTaskTracker
615
616[`base::CancelableTaskTracker`](https://cs.chromium.org/chromium/src/base/task/cancelable_task_tracker.h)
617allows cancellation to happen on a different sequence than the one on which
618tasks run. Keep in mind that `CancelableTaskTracker` cannot cancel tasks that
619have already started to run.
620
621```cpp
622auto task_runner = base::CreateTaskRunnerWithTraits(base::TaskTraits());
623base::CancelableTaskTracker cancelable_task_tracker;
624cancelable_task_tracker.PostTask(task_runner.get(), FROM_HERE,
Peter Kasting341e1fb2018-02-24 00:03:01625 base::DoNothing());
fdoraybacba4a22017-05-10 21:10:00626// Cancels Task(), only if it hasn't already started running.
627cancelable_task_tracker.TryCancelAll();
628```
629
630## Testing
631
632To test code that uses `base::ThreadTaskRunnerHandle`,
633`base::SequencedTaskRunnerHandle` or a function in
Gabriel Charette39db4c62019-04-29 19:52:38634[`base/task/post_task.h`](https://cs.chromium.org/chromium/src/base/task/post_task.h),
635instantiate a
fdoraybacba4a22017-05-10 21:10:00636[`base::test::ScopedTaskEnvironment`](https://cs.chromium.org/chromium/src/base/test/scoped_task_environment.h)
Gabriel Charette39db4c62019-04-29 19:52:38637for the scope of the test. If you need BrowserThreads, use
638`content::TestBrowserThreadBundle` instead of
639`base::test::ScopedTaskEnvironment`.
fdoraybacba4a22017-05-10 21:10:00640
Gabriel Charette39db4c62019-04-29 19:52:38641Tests can run the `base::test::ScopedTaskEnvironment`'s message pump using a
642`base::RunLoop`, which can be made to run until `Quit()` (explicitly or via
643`RunLoop::QuitClosure()`), or to `RunUntilIdle()` ready-to-run tasks and
644immediately return.
Wezd9e4cb772019-01-09 03:07:03645
646ScopedTaskEnvironment configures RunLoop::Run() to LOG(FATAL) if it hasn't been
647explicitly quit after TestTimeouts::action_timeout(). This is preferable to
648having the test hang if the code under test fails to trigger the RunLoop to
649quit. The timeout can be overridden with ScopedRunTimeoutForTest.
650
fdoraybacba4a22017-05-10 21:10:00651```cpp
652class MyTest : public testing::Test {
653 public:
654 // ...
655 protected:
656 base::test::ScopedTaskEnvironment scoped_task_environment_;
657};
658
659TEST(MyTest, MyTest) {
660 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::BindOnce(&A));
661 base::SequencedTaskRunnerHandle::Get()->PostTask(FROM_HERE,
662 base::BindOnce(&B));
663 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
664 FROM_HERE, base::BindOnce(&C), base::TimeDelta::Max());
665
666 // This runs the (Thread|Sequenced)TaskRunnerHandle queue until it is empty.
667 // Delayed tasks are not added to the queue until they are ripe for execution.
668 base::RunLoop().RunUntilIdle();
669 // A and B have been executed. C is not ripe for execution yet.
670
671 base::RunLoop run_loop;
672 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::BindOnce(&D));
673 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, run_loop.QuitClosure());
674 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::BindOnce(&E));
675
676 // This runs the (Thread|Sequenced)TaskRunnerHandle queue until QuitClosure is
677 // invoked.
678 run_loop.Run();
679 // D and run_loop.QuitClosure() have been executed. E is still in the queue.
680
Gabriel Charette52fa3ae2019-04-15 21:44:37681 // Tasks posted to thread pool run asynchronously as they are posted.
fdoraybacba4a22017-05-10 21:10:00682 base::PostTaskWithTraits(FROM_HERE, base::TaskTraits(), base::BindOnce(&F));
683 auto task_runner =
684 base::CreateSequencedTaskRunnerWithTraits(base::TaskTraits());
685 task_runner->PostTask(FROM_HERE, base::BindOnce(&G));
686
Gabriel Charette52fa3ae2019-04-15 21:44:37687 // To block until all tasks posted to thread pool are done running:
688 base::ThreadPool::GetInstance()->FlushForTesting();
fdoraybacba4a22017-05-10 21:10:00689 // F and G have been executed.
690
691 base::PostTaskWithTraitsAndReplyWithResult(
692 FROM_HERE, base::TaskTrait(),
693 base::BindOnce(&H), base::BindOnce(&I));
694
695 // This runs the (Thread|Sequenced)TaskRunnerHandle queue until both the
696 // (Thread|Sequenced)TaskRunnerHandle queue and the TaskSchedule queue are
697 // empty:
698 scoped_task_environment_.RunUntilIdle();
699 // E, H, I have been executed.
700}
701```
702
Gabriel Charette52fa3ae2019-04-15 21:44:37703## Using ThreadPool in a New Process
fdoraybacba4a22017-05-10 21:10:00704
Gabriel Charette52fa3ae2019-04-15 21:44:37705ThreadPool needs to be initialized in a process before the functions in
Gabriel Charette04b138f2018-08-06 00:03:22706[`base/task/post_task.h`](https://cs.chromium.org/chromium/src/base/task/post_task.h)
Gabriel Charette52fa3ae2019-04-15 21:44:37707can be used. Initialization of ThreadPool in the Chrome browser process and
fdoraybacba4a22017-05-10 21:10:00708child processes (renderer, GPU, utility) has already been taken care of. To use
Gabriel Charette52fa3ae2019-04-15 21:44:37709ThreadPool in another process, initialize ThreadPool early in the main
fdoraybacba4a22017-05-10 21:10:00710function:
711
712```cpp
Gabriel Charette52fa3ae2019-04-15 21:44:37713// This initializes and starts ThreadPool with default params.
714base::ThreadPool::CreateAndStartWithDefaultParams(“process_name”);
Gabriel Charette04b138f2018-08-06 00:03:22715// The base/task/post_task.h API can now be used. Tasks will be // scheduled as
716// they are posted.
fdoraybacba4a22017-05-10 21:10:00717
Gabriel Charette52fa3ae2019-04-15 21:44:37718// This initializes ThreadPool.
719base::ThreadPool::Create(“process_name”);
Gabriel Charette04b138f2018-08-06 00:03:22720// The base/task/post_task.h API can now be used. No threads // will be created
721// and no tasks will be scheduled until after Start() is called.
Gabriel Charette52fa3ae2019-04-15 21:44:37722base::ThreadPool::GetInstance()->Start(params);
723// ThreadPool can now create threads and schedule tasks.
fdoraybacba4a22017-05-10 21:10:00724```
725
Gabriel Charette52fa3ae2019-04-15 21:44:37726And shutdown ThreadPool late in the main function:
fdoraybacba4a22017-05-10 21:10:00727
728```cpp
Gabriel Charette52fa3ae2019-04-15 21:44:37729base::ThreadPool::GetInstance()->Shutdown();
fdoraybacba4a22017-05-10 21:10:00730// Tasks posted with TaskShutdownBehavior::BLOCK_SHUTDOWN and
731// tasks posted with TaskShutdownBehavior::SKIP_ON_SHUTDOWN that
732// have started to run before the Shutdown() call have now completed their
733// execution. Tasks posted with
734// TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN may still be
735// running.
736```
Gabriel Charetteb86e5fe62017-06-08 19:39:28737## TaskRunner ownership (encourage no dependency injection)
Sebastien Marchandc95489b2017-05-25 16:39:34738
739TaskRunners shouldn't be passed through several components. Instead, the
740components that uses a TaskRunner should be the one that creates it.
741
742See [this example](https://codereview.chromium.org/2885173002/) of a
743refactoring where a TaskRunner was passed through a lot of components only to be
744used in an eventual leaf. The leaf can and should now obtain its TaskRunner
745directly from
Gabriel Charette04b138f2018-08-06 00:03:22746[`base/task/post_task.h`](https://cs.chromium.org/chromium/src/base/task/post_task.h).
Gabriel Charetteb86e5fe62017-06-08 19:39:28747
Gabriel Charette39db4c62019-04-29 19:52:38748As mentioned above, `base::test::ScopedTaskEnvironment` allows unit tests to
749control tasks posted from underlying TaskRunners. In rare cases where a test
750needs to more precisely control task ordering: dependency injection of
751TaskRunners can be useful. For such cases the preferred approach is the
752following:
Gabriel Charetteb86e5fe62017-06-08 19:39:28753
754```cpp
Gabriel Charette39db4c62019-04-29 19:52:38755class Foo {
Gabriel Charetteb86e5fe62017-06-08 19:39:28756 public:
757
Gabriel Charette39db4c62019-04-29 19:52:38758 // Overrides |background_task_runner_| in tests.
Gabriel Charetteb86e5fe62017-06-08 19:39:28759 void SetBackgroundTaskRunnerForTesting(
Gabriel Charette39db4c62019-04-29 19:52:38760 scoped_refptr<base::SequencedTaskRunner> background_task_runner) {
761 background_task_runner_ = std::move(background_task_runner);
762 }
Gabriel Charetteb86e5fe62017-06-08 19:39:28763
764 private:
michaelpg12c04572017-06-26 23:25:06765 scoped_refptr<base::SequencedTaskRunner> background_task_runner_ =
766 base::CreateSequencedTaskRunnerWithTraits(
Gabriel Charetteb10aeebc2018-07-26 20:15:00767 {base::MayBlock(), base::TaskPriority::BEST_EFFORT});
Gabriel Charetteb86e5fe62017-06-08 19:39:28768}
769```
770
771Note that this still allows removing all layers of plumbing between //chrome and
772that component since unit tests will use the leaf layer directly.
Gabriel Charette8917f4c2018-11-22 15:50:28773
774## FAQ
775See [Threading and Tasks FAQ](threading_and_tasks_faq.md) for more examples.