blob: 2eae39a7ee8be7d1d40663abb45dfb8058f209fb [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
Matt Falkenhagen72a2dfc2021-08-05 22:36:1314thread (a.k.a. "UI" thread in the browser process) and IO thread (each process's
15thread for receiving
16[IPC](https://en.wikipedia.org/wiki/Inter-process_communication))
17responsive. This means offloading any blocking I/O or other expensive
18operations to other threads. Our approach is to use message passing as the way
19of communicating between threads. We discourage locking and thread-safe objects.
20Instead, objects live on only one (often virtual -- we'll get to that later!)
21thread and we pass messages between those threads for communication. Absent
22external requirements about latency or workload, Chrome attempts to be a [highly
23concurrent, but not necessarily
24parallel](https://stackoverflow.com/questions/1050222/what-is-the-difference-between-concurrency-and-parallelism#:~:text=Concurrency%20is%20when%20two%20or,e.g.%2C%20on%20a%20multicore%20processor.),
Jared Saulea867ab2021-07-15 17:39:0125system.
Gabriel Charette39db4c62019-04-29 19:52:3826
cfredricff6d86c2022-02-15 16:26:1127A basic intro to the way Chromium does concurrency (especially Sequences) can be
28found
29[here](https://docs.google.com/presentation/d/1ujV8LjIUyPBmULzdT2aT9Izte8PDwbJi).
30
Gabriel Charette39db4c62019-04-29 19:52:3831This documentation assumes familiarity with computer science
32[threading concepts](https://en.wikipedia.org/wiki/Thread_(computing)).
Gabriel Charette90480312018-02-16 15:10:0533
Gabriel Charette364a16a2019-02-06 21:12:1534### Nomenclature
Gabriel Charette39db4c62019-04-29 19:52:3835
36## Core Concepts
37 * **Task**: A unit of work to be processed. Effectively a function pointer with
Alex St-Onge490a97a2021-02-04 02:47:1938 optionally associated state. In Chrome this is `base::OnceCallback` and
39 `base::RepeatingCallback` created via `base::BindOnce` and
40 `base::BindRepeating`, respectively.
Gabriel Charette39db4c62019-04-29 19:52:3841 ([documentation](https://chromium.googlesource.com/chromium/src/+/HEAD/docs/callback.md)).
42 * **Task queue**: A queue of tasks to be processed.
43 * **Physical thread**: An operating system provided thread (e.g. pthread on
44 POSIX or CreateThread() on Windows). The Chrome cross-platform abstraction
45 is `base::PlatformThread`. You should pretty much never use this directly.
46 * **`base::Thread`**: A physical thread forever processing messages from a
47 dedicated task queue until Quit(). You should pretty much never be creating
48 your own `base::Thread`'s.
49 * **Thread pool**: A pool of physical threads with a shared task queue. In
Gabriel Charette0b20ee62019-09-18 14:06:1250 Chrome, this is `base::ThreadPoolInstance`. There's exactly one instance per
51 Chrome process, it serves tasks posted through
Gabriel Charette9b6c04072022-04-01 23:22:4652 [`base/task/thread_pool.h`](https://cs.chromium.org/chromium/src/base/task/thread_pool.h)
Gabriel Charette43fd3702019-05-29 16:36:5153 and as such you should rarely need to use the `base::ThreadPoolInstance` API
Gabriel Charette39db4c62019-04-29 19:52:3854 directly (more on posting tasks later).
55 * **Sequence** or **Virtual thread**: A chrome-managed thread of execution.
56 Like a physical thread, only one task can run on a given sequence / virtual
57 thread at any given moment and each task sees the side-effects of the
58 preceding tasks. Tasks are executed sequentially but may hop physical
59 threads between each one.
60 * **Task runner**: An interface through which tasks can be posted. In Chrome
61 this is `base::TaskRunner`.
62 * **Sequenced task runner**: A task runner which guarantees that tasks posted
63 to it will run sequentially, in posted order. Each such task is guaranteed to
64 see the side-effects of the task preceding it. Tasks posted to a sequenced
65 task runner are typically processed by a single thread (virtual or physical).
66 In Chrome this is `base::SequencedTaskRunner` which is-a
67 `base::TaskRunner`.
68 * **Single-thread task runner**: A sequenced task runner which guarantees that
69 all tasks will be processed by the same physical thread. In Chrome this is
70 `base::SingleThreadTaskRunner` which is-a `base::SequencedTaskRunner`. We
71 [prefer sequences to threads](#prefer-sequences-to-physical-threads) whenever
72 possible.
73
74## Threading Lexicon
75Note to the reader: the following terms are an attempt to bridge the gap between
76common threading nomenclature and the way we use them in Chrome. It might be a
77bit heavy if you're just getting started. Should this be hard to parse, consider
78skipping to the more detailed sections below and referring back to this as
79necessary.
80
81 * **Thread-unsafe**: The vast majority of types in Chrome are thread-unsafe
82 (by design). Access to such types/methods must be externally synchronized.
83 Typically thread-unsafe types require that all tasks accessing their state be
84 posted to the same `base::SequencedTaskRunner` and they verify this in debug
85 builds with a `SEQUENCE_CHECKER` member. Locks are also an option to
86 synchronize access but in Chrome we strongly
87 [prefer sequences to locks](#Using-Sequences-Instead-of-Locks).
Gabriel Charette364a16a2019-02-06 21:12:1588 * **Thread-affine**: Such types/methods need to be always accessed from the
Gabriel Charetteb984d672019-02-12 21:53:2789 same physical thread (i.e. from the same `base::SingleThreadTaskRunner`) and
Gabriel Charette39db4c62019-04-29 19:52:3890 typically have a `THREAD_CHECKER` member to verify that they are. Short of
91 using a third-party API or having a leaf dependency which is thread-affine:
92 there's pretty much no reason for a type to be thread-affine in Chrome.
93 Note that `base::SingleThreadTaskRunner` is-a `base::SequencedTaskRunner` so
Gabriel Charetteb984d672019-02-12 21:53:2794 thread-affine is a subset of thread-unsafe. Thread-affine is also sometimes
95 referred to as **thread-hostile**.
Albert J. Wongf06ff5002021-07-08 20:37:0096 * **Thread-safe**: Such types/methods can be safely accessed in parallel.
97 * **Thread-compatible**: Such types provide safe parallel access to const
Gabriel Charetteb984d672019-02-12 21:53:2798 methods but require synchronization for non-const (or mixed const/non-const
Gabriel Charette39db4c62019-04-29 19:52:3899 access). Chrome doesn't expose reader-writer locks; as such, the only use
Gabriel Charetteb984d672019-02-12 21:53:27100 case for this is objects (typically globals) which are initialized once in a
Gabriel Charette364a16a2019-02-06 21:12:15101 thread-safe manner (either in the single-threaded phase of startup or lazily
102 through a thread-safe static-local-initialization paradigm a la
Gabriel Charetteb984d672019-02-12 21:53:27103 `base::NoDestructor`) and forever after immutable.
104 * **Immutable**: A subset of thread-compatible types which cannot be modified
105 after construction.
Gabriel Charette364a16a2019-02-06 21:12:15106 * **Sequence-friendly**: Such types/methods are thread-unsafe types which
107 support being invoked from a `base::SequencedTaskRunner`. Ideally this would
108 be the case for all thread-unsafe types but legacy code sometimes has
109 overzealous checks that enforce thread-affinity in mere thread-unsafe
Gabriel Charette39db4c62019-04-29 19:52:38110 scenarios. See [Prefer Sequences to
111 Threads](#prefer-sequences-to-physical-threads) below for more details.
Gabriel Charette364a16a2019-02-06 21:12:15112
fdoraybacba4a22017-05-10 21:10:00113### Threads
114
115Every Chrome process has
116
117* a main thread
Gabriel Charette39db4c62019-04-29 19:52:38118 * in the browser process (BrowserThread::UI): updates the UI
119 * in renderer processes (Blink main thread): runs most of Blink
fdoraybacba4a22017-05-10 21:10:00120* an IO thread
Matt Falkenhagen72a2dfc2021-08-05 22:36:13121 * in all processes: all IPC messages arrive on this thread. The application
122 logic to handle the message may be in a different thread (i.e., the IO
123 thread may route the message to a [Mojo
124 interface](/docs/README.md#Mojo-Services) which is bound to a
125 different thread).
126 * more generally most async I/O happens on this thread (e.g., through
127 base::FileDescriptorWatcher).
128 * in the browser process: this is called BrowserThread::IO.
fdoraybacba4a22017-05-10 21:10:00129* a few more special-purpose threads
130* and a pool of general-purpose threads
131
132Most threads have a loop that gets tasks from a queue and runs them (the queue
133may be shared between multiple threads).
134
135### Tasks
136
137A task is a `base::OnceClosure` added to a queue for asynchronous execution.
138
139A `base::OnceClosure` stores a function pointer and arguments. It has a `Run()`
140method that invokes the function pointer using the bound arguments. It is
141created using `base::BindOnce`. (ref. [Callback<> and Bind()
142documentation](callback.md)).
143
144```
145void TaskA() {}
146void TaskB(int v) {}
147
148auto task_a = base::BindOnce(&TaskA);
149auto task_b = base::BindOnce(&TaskB, 42);
150```
151
152A group of tasks can be executed in one of the following ways:
153
154* [Parallel](#Posting-a-Parallel-Task): No task execution ordering, possibly all
155 at once on any thread
156* [Sequenced](#Posting-a-Sequenced-Task): Tasks executed in posting order, one
157 at a time on any thread.
158* [Single Threaded](#Posting-Multiple-Tasks-to-the-Same-Thread): Tasks executed
159 in posting order, one at a time on a single thread.
Drew Stonebraker653a3ba2019-07-02 19:24:23160 * [COM Single Threaded](#Posting-Tasks-to-a-COM-Single_Thread-Apartment-STA_Thread-Windows):
fdoraybacba4a22017-05-10 21:10:00161 A variant of single threaded with COM initialized.
162
Gabriel Charette39db4c62019-04-29 19:52:38163### Prefer Sequences to Physical Threads
gab2a4576052017-06-07 23:36:12164
Gabriel Charette39db4c62019-04-29 19:52:38165Sequenced execution (on virtual threads) is strongly preferred to
166single-threaded execution (on physical threads). Except for types/methods bound
167to the main thread (UI) or IO threads: thread-safety is better achieved via
168`base::SequencedTaskRunner` than through managing your own physical threads
169(ref. [Posting a Sequenced Task](#posting-a-sequenced-task) below).
gab2a4576052017-06-07 23:36:12170
Gabriel Charette39db4c62019-04-29 19:52:38171All APIs which are exposed for "current physical thread" have an equivalent for
172"current sequence"
173([mapping](threading_and_tasks_faq.md#How-to-migrate-from-SingleThreadTaskRunner-to-SequencedTaskRunner)).
gab2a4576052017-06-07 23:36:12174
Gabriel Charette39db4c62019-04-29 19:52:38175If you find yourself writing a sequence-friendly type and it fails
176thread-affinity checks (e.g., `THREAD_CHECKER`) in a leaf dependency: consider
177making that dependency sequence-friendly as well. Most core APIs in Chrome are
178sequence-friendly, but some legacy types may still over-zealously use
Sean Maher03efef12022-09-23 22:43:13179ThreadChecker/SingleThreadTaskRunner when they could instead rely on the
180"current sequence" and no longer be thread-affine.
fdoraybacba4a22017-05-10 21:10:00181
182## Posting a Parallel Task
183
Gabriel Charette52fa3ae2019-04-15 21:44:37184### Direct Posting to the Thread Pool
fdoraybacba4a22017-05-10 21:10:00185
186A task that can run on any thread and doesn’t have ordering or mutual exclusion
187requirements with other tasks should be posted using one of the
Gabriel Charette43de5c42020-01-27 22:44:45188`base::ThreadPool::PostTask*()` functions defined in
189[`base/task/thread_pool.h`](https://cs.chromium.org/chromium/src/base/task/thread_pool.h).
fdoraybacba4a22017-05-10 21:10:00190
191```cpp
Gabriel Charette43de5c42020-01-27 22:44:45192base::ThreadPool::PostTask(FROM_HERE, base::BindOnce(&Task));
fdoraybacba4a22017-05-10 21:10:00193```
194
195This posts tasks with default traits.
196
Gabriel Charette43de5c42020-01-27 22:44:45197The `base::ThreadPool::PostTask*()` functions allow the caller to provide
198additional details about the task via TaskTraits (ref. [Annotating Tasks with
199TaskTraits](#Annotating-Tasks-with-TaskTraits)).
fdoraybacba4a22017-05-10 21:10:00200
201```cpp
Gabriel Charette43de5c42020-01-27 22:44:45202base::ThreadPool::PostTask(
Gabriel Charetteb10aeeb2018-07-26 20:15:00203 FROM_HERE, {base::TaskPriority::BEST_EFFORT, MayBlock()},
fdoraybacba4a22017-05-10 21:10:00204 base::BindOnce(&Task));
205```
206
fdoray52bf5552017-05-11 12:43:59207### Posting via a TaskRunner
fdoraybacba4a22017-05-10 21:10:00208
209A parallel
Patrick Monette2d93ad902021-11-01 19:20:22210[`base::TaskRunner`](https://cs.chromium.org/chromium/src/base/task/task_runner.h) is
Gabriel Charette43de5c42020-01-27 22:44:45211an alternative to calling `base::ThreadPool::PostTask*()` directly. This is
212mainly useful when it isn’t known in advance whether tasks will be posted in
213parallel, in sequence, or to a single-thread (ref. [Posting a Sequenced
Gabriel Charette39db4c62019-04-29 19:52:38214Task](#Posting-a-Sequenced-Task), [Posting Multiple Tasks to the Same
215Thread](#Posting-Multiple-Tasks-to-the-Same-Thread)). Since `base::TaskRunner`
216is the base class of `base::SequencedTaskRunner` and
217`base::SingleThreadTaskRunner`, a `scoped_refptr<TaskRunner>` member can hold a
218`base::TaskRunner`, a `base::SequencedTaskRunner` or a
219`base::SingleThreadTaskRunner`.
fdoraybacba4a22017-05-10 21:10:00220
221```cpp
222class A {
223 public:
224 A() = default;
225
Gabriel Charette43de5c42020-01-27 22:44:45226 void PostSomething() {
227 task_runner_->PostTask(FROM_HERE, base::BindOnce(&A, &DoSomething));
228 }
229
fdoraybacba4a22017-05-10 21:10:00230 void DoSomething() {
fdoraybacba4a22017-05-10 21:10:00231 }
232
233 private:
234 scoped_refptr<base::TaskRunner> task_runner_ =
Gabriel Charette43de5c42020-01-27 22:44:45235 base::ThreadPool::CreateTaskRunner({base::TaskPriority::USER_VISIBLE});
fdoraybacba4a22017-05-10 21:10:00236};
237```
238
239Unless a test needs to control precisely how tasks are executed, it is preferred
Gabriel Charette49e3cd02020-01-28 03:45:27240to call `base::ThreadPool::PostTask*()` directly (ref. [Testing](#Testing) for
241less invasive ways of controlling tasks in tests).
fdoraybacba4a22017-05-10 21:10:00242
243## Posting a Sequenced Task
244
245A sequence is a set of tasks that run one at a time in posting order (not
246necessarily on the same thread). To post tasks as part of a sequence, use a
Patrick Monette2d93ad902021-11-01 19:20:22247[`base::SequencedTaskRunner`](https://cs.chromium.org/chromium/src/base/task/sequenced_task_runner.h).
fdoraybacba4a22017-05-10 21:10:00248
249### Posting to a New Sequence
250
Gabriel Charette39db4c62019-04-29 19:52:38251A `base::SequencedTaskRunner` can be created by
Gabriel Charette43de5c42020-01-27 22:44:45252`base::ThreadPool::CreateSequencedTaskRunner()`.
fdoraybacba4a22017-05-10 21:10:00253
254```cpp
255scoped_refptr<SequencedTaskRunner> sequenced_task_runner =
Gabriel Charette43de5c42020-01-27 22:44:45256 base::ThreadPool::CreateSequencedTaskRunner(...);
fdoraybacba4a22017-05-10 21:10:00257
258// TaskB runs after TaskA completes.
259sequenced_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskA));
260sequenced_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskB));
261```
262
Alex Clarke0dd499562019-10-18 19:45:09263### Posting to the Current (Virtual) Thread
264
Gabriel Charettefee55662019-11-20 21:06:28265The preferred way of posting to the current (virtual) thread is via
Sean Maher03efef12022-09-23 22:43:13266`base::SequencedTaskRunner::GetCurrentDefault()`.
Alex Clarke0dd499562019-10-18 19:45:09267
268```cpp
269// The task will run on the current (virtual) thread's default task queue.
Sean Maher03efef12022-09-23 22:43:13270base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
Gabriel Charettefee55662019-11-20 21:06:28271 FROM_HERE, base::BindOnce(&Task);
Alex Clarke0dd499562019-10-18 19:45:09272```
273
Sean Maher03efef12022-09-23 22:43:13274Note that `SequencedTaskRunner::GetCurrentDefault()` returns the default queue for the
Gabriel Charettefee55662019-11-20 21:06:28275current virtual thread. On threads with multiple task queues (e.g.
276BrowserThread::UI) this can be a different queue than the one the current task
277belongs to. The "current" task runner is intentionally not exposed via a static
278getter. Either you know it already and can post to it directly or you don't and
Francois Doraya06ee172022-11-24 21:09:18279the only sensible destination is the default queue. See https://bit.ly/3JvCLsX
Sean Maher03efef12022-09-23 22:43:13280for detailed discussion.
Alex Clarke0dd499562019-10-18 19:45:09281
fdoraybacba4a22017-05-10 21:10:00282## Using Sequences Instead of Locks
283
284Usage of locks is discouraged in Chrome. Sequences inherently provide
Gabriel Charettea3ccc972018-11-13 14:43:12285thread-safety. Prefer classes that are always accessed from the same
286sequence to managing your own thread-safety with locks.
287
288**Thread-safe but not thread-affine; how so?** Tasks posted to the same sequence
289will run in sequential order. After a sequenced task completes, the next task
290may be picked up by a different worker thread, but that task is guaranteed to
291see any side-effects caused by the previous one(s) on its sequence.
fdoraybacba4a22017-05-10 21:10:00292
293```cpp
294class A {
295 public:
296 A() {
297 // Do not require accesses to be on the creation sequence.
isherman8c33b8a2017-06-27 19:18:30298 DETACH_FROM_SEQUENCE(sequence_checker_);
fdoraybacba4a22017-05-10 21:10:00299 }
300
301 void AddValue(int v) {
302 // Check that all accesses are on the same sequence.
isherman8c33b8a2017-06-27 19:18:30303 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
fdoraybacba4a22017-05-10 21:10:00304 values_.push_back(v);
305}
306
307 private:
isherman8c33b8a2017-06-27 19:18:30308 SEQUENCE_CHECKER(sequence_checker_);
fdoraybacba4a22017-05-10 21:10:00309
310 // No lock required, because all accesses are on the
311 // same sequence.
312 std::vector<int> values_;
313};
314
315A a;
316scoped_refptr<SequencedTaskRunner> task_runner_for_a = ...;
Mike Bjorged3a09842018-05-15 18:37:28317task_runner_for_a->PostTask(FROM_HERE,
318 base::BindOnce(&A::AddValue, base::Unretained(&a), 42));
319task_runner_for_a->PostTask(FROM_HERE,
320 base::BindOnce(&A::AddValue, base::Unretained(&a), 27));
fdoraybacba4a22017-05-10 21:10:00321
322// Access from a different sequence causes a DCHECK failure.
323scoped_refptr<SequencedTaskRunner> other_task_runner = ...;
324other_task_runner->PostTask(FROM_HERE,
Mike Bjorged3a09842018-05-15 18:37:28325 base::BindOnce(&A::AddValue, base::Unretained(&a), 1));
fdoraybacba4a22017-05-10 21:10:00326```
327
Gabriel Charette90480312018-02-16 15:10:05328Locks should only be used to swap in a shared data structure that can be
329accessed on multiple threads. If one thread updates it based on expensive
330computation or through disk access, then that slow work should be done without
Gabriel Charette39db4c62019-04-29 19:52:38331holding the lock. Only when the result is available should the lock be used to
332swap in the new data. An example of this is in PluginList::LoadPlugins
333([`content/browser/plugin_list.cc`](https://cs.chromium.org/chromium/src/content/browser/plugin_list.cc).
334If you must use locks,
Gabriel Charette90480312018-02-16 15:10:05335[here](https://www.chromium.org/developers/lock-and-condition-variable) are some
336best practices and pitfalls to avoid.
337
Gabriel Charette39db4c62019-04-29 19:52:38338In order to write non-blocking code, many APIs in Chrome are asynchronous.
Gabriel Charette90480312018-02-16 15:10:05339Usually this means that they either need to be executed on a particular
340thread/sequence and will return results via a custom delegate interface, or they
Alex St-Onge490a97a2021-02-04 02:47:19341take a `base::OnceCallback<>` (or `base::RepeatingCallback<>`) object that is
342called when the requested operation is completed. Executing work on a specific
343thread/sequence is covered in the PostTask sections above.
Gabriel Charette90480312018-02-16 15:10:05344
fdoraybacba4a22017-05-10 21:10:00345## Posting Multiple Tasks to the Same Thread
346
347If multiple tasks need to run on the same thread, post them to a
Patrick Monette2d93ad902021-11-01 19:20:22348[`base::SingleThreadTaskRunner`](https://cs.chromium.org/chromium/src/base/task/single_thread_task_runner.h).
Gabriel Charette39db4c62019-04-29 19:52:38349All tasks posted to the same `base::SingleThreadTaskRunner` run on the same thread in
fdoraybacba4a22017-05-10 21:10:00350posting order.
351
352### Posting to the Main Thread or to the IO Thread in the Browser Process
353
Eric Seckler6cf08db82018-08-30 12:01:55354To post tasks to the main thread or to the IO thread, use
Olivier Li56b99d4e2020-02-11 13:51:41355`content::GetUIThreadTaskRunner({})` or `content::GetIOThreadTaskRunner({})`
Gabriel Charette49e3cd02020-01-28 03:45:27356from
357[`content/public/browser/browser_thread.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_thread.h)
358
359You may provide additional BrowserTaskTraits as a parameter to those methods
360though this is generally still uncommon in BrowserThreads and should be reserved
361for advanced use cases.
362
363There's an ongoing migration ([task APIs v3]) away from the previous
364base-API-with-traits which you may still find throughout the codebase (it's
365equivalent):
fdoraybacba4a22017-05-10 21:10:00366
367```cpp
Sami Kyostila831c60b2019-07-31 13:31:23368base::PostTask(FROM_HERE, {content::BrowserThread::UI}, ...);
fdoraybacba4a22017-05-10 21:10:00369
Sami Kyostila831c60b2019-07-31 13:31:23370base::CreateSingleThreadTaskRunner({content::BrowserThread::IO})
fdoraybacba4a22017-05-10 21:10:00371 ->PostTask(FROM_HERE, ...);
372```
373
Gabriel Charette49e3cd02020-01-28 03:45:27374Note: For the duration of the migration, you'll unfortunately need to continue
375manually including
376[`content/public/browser/browser_task_traits.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_task_traits.h).
377to use the browser_thread.h API.
Gabriel Charette43de5c42020-01-27 22:44:45378
fdoraybacba4a22017-05-10 21:10:00379The main thread and the IO thread are already super busy. Therefore, prefer
fdoray52bf5552017-05-11 12:43:59380posting to a general purpose thread when possible (ref.
381[Posting a Parallel Task](#Posting-a-Parallel-Task),
382[Posting a Sequenced task](#Posting-a-Sequenced-Task)).
383Good reasons to post to the main thread are to update the UI or access objects
384that are bound to it (e.g. `Profile`). A good reason to post to the IO thread is
385to access the internals of components that are bound to it (e.g. IPCs, network).
386Note: It is not necessary to have an explicit post task to the IO thread to
387send/receive an IPC or send/receive data on the network.
fdoraybacba4a22017-05-10 21:10:00388
389### Posting to the Main Thread in a Renderer Process
Gabriel Charette49e3cd02020-01-28 03:45:27390TODO(blink-dev)
fdoraybacba4a22017-05-10 21:10:00391
392### Posting to a Custom SingleThreadTaskRunner
393
394If multiple tasks need to run on the same thread and that thread doesn’t have to
Gabriel Charette43de5c42020-01-27 22:44:45395be the main thread or the IO thread, post them to a
Gabriel Charette49e3cd02020-01-28 03:45:27396`base::SingleThreadTaskRunner` created by
397`base::Threadpool::CreateSingleThreadTaskRunner`.
fdoraybacba4a22017-05-10 21:10:00398
399```cpp
Dominic Farolinodbe9769b2019-05-31 04:06:03400scoped_refptr<SingleThreadTaskRunner> single_thread_task_runner =
Gabriel Charette43de5c42020-01-27 22:44:45401 base::Threadpool::CreateSingleThreadTaskRunner(...);
fdoraybacba4a22017-05-10 21:10:00402
403// TaskB runs after TaskA completes. Both tasks run on the same thread.
404single_thread_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskA));
405single_thread_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskB));
406```
407
Gabriel Charette39db4c62019-04-29 19:52:38408Remember that we [prefer sequences to physical
409threads](#prefer-sequences-to-physical-threads) and that this thus should rarely
410be necessary.
fdoraybacba4a22017-05-10 21:10:00411
Alexander Timine653dfc2020-01-07 17:55:06412### Posting to the Current Thread
413
414*** note
415**IMPORTANT:** To post a task that needs mutual exclusion with the current
Gabriel Charette49e3cd02020-01-28 03:45:27416sequence of tasks but doesn’t absolutely need to run on the current physical
Sean Maher03efef12022-09-23 22:43:13417thread, use `base::SequencedTaskRunner::GetCurrentDefault()` instead of
418`base::SingleThreadTaskRunner::GetCurrentDefault()` (ref. [Posting to the Current
Gabriel Charette49e3cd02020-01-28 03:45:27419Sequence](#Posting-to-the-Current-Virtual_Thread)). That will better document
420the requirements of the posted task and will avoid unnecessarily making your API
421physical thread-affine. In a single-thread task,
Sean Maher03efef12022-09-23 22:43:13422`base::SequencedTaskRunner::GetCurrentDefault()` is equivalent to
423`base::SingleThreadTaskRunner::GetCurrentDefault()`.
Alexander Timine653dfc2020-01-07 17:55:06424***
425
426If you must post a task to the current physical thread nonetheless, use
Sean Maher03efef12022-09-23 22:43:13427[`base::SingleThreadTaskRunner::CurrentDefaultHandle`](https://source.chromium.org/chromium/chromium/src/+/main:base/task/single_thread_task_runner.h).
Alexander Timine653dfc2020-01-07 17:55:06428
429```cpp
430// The task will run on the current thread in the future.
Sean Maher03efef12022-09-23 22:43:13431base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
Alexander Timine653dfc2020-01-07 17:55:06432 FROM_HERE, base::BindOnce(&Task));
433```
434
fdoraybacba4a22017-05-10 21:10:00435## Posting Tasks to a COM Single-Thread Apartment (STA) Thread (Windows)
436
437Tasks that need to run on a COM Single-Thread Apartment (STA) thread must be
Gabriel Charette39db4c62019-04-29 19:52:38438posted to a `base::SingleThreadTaskRunner` returned by
Gabriel Charette43de5c42020-01-27 22:44:45439`base::ThreadPool::CreateCOMSTATaskRunner()`. As mentioned in [Posting Multiple
440Tasks to the Same Thread](#Posting-Multiple-Tasks-to-the-Same-Thread), all tasks
441posted to the same `base::SingleThreadTaskRunner` run on the same thread in
442posting order.
fdoraybacba4a22017-05-10 21:10:00443
444```cpp
445// Task(A|B|C)UsingCOMSTA will run on the same COM STA thread.
446
447void TaskAUsingCOMSTA() {
448 // [ This runs on a COM STA thread. ]
449
450 // Make COM STA calls.
451 // ...
452
453 // Post another task to the current COM STA thread.
Sean Maher03efef12022-09-23 22:43:13454 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
fdoraybacba4a22017-05-10 21:10:00455 FROM_HERE, base::BindOnce(&TaskCUsingCOMSTA));
456}
457void TaskBUsingCOMSTA() { }
458void TaskCUsingCOMSTA() { }
459
Gabriel Charette43de5c42020-01-27 22:44:45460auto com_sta_task_runner = base::ThreadPool::CreateCOMSTATaskRunner(...);
fdoraybacba4a22017-05-10 21:10:00461com_sta_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskAUsingCOMSTA));
462com_sta_task_runner->PostTask(FROM_HERE, base::BindOnce(&TaskBUsingCOMSTA));
463```
464
Egor Pasko7f58c33b2023-02-17 13:19:56465## Memory ordering guarantees for posted Tasks
466
467This task system guarantees that all the memory effects of sequential execution
Egor Pasko049e8d352023-02-23 17:09:50468before posting a task are _visible_ to the task when it starts running. More
469formally, a call to `PostTask()` and the execution of the posted task are in the
470[happens-before
Egor Pasko7f58c33b2023-02-17 13:19:56471relationship](https://preshing.com/20130702/the-happens-before-relation/) with
Egor Pasko049e8d352023-02-23 17:09:50472each other. This is true for all variants of posting a task in `::base`,
473including `PostTaskAndReply()`. Similarly the happens-before relationship is
474present for tasks running in a sequence as part of the same SequencedTaskRunner.
Egor Pasko7f58c33b2023-02-17 13:19:56475
Egor Pasko049e8d352023-02-23 17:09:50476This guarantee is important to know about because Chrome tasks commonly access
477memory beyond the immediate data copied into the `base::OnceCallback`, and this
478happens-before relationship allows to avoid additional synchronization within
479the tasks themselves. As a very specific example, consider a callback that binds
480a pointer to memory which was just initialized in the thread posting the task.
Egor Pasko7f58c33b2023-02-17 13:19:56481
Egor Pasko049e8d352023-02-23 17:09:50482A more constrained model is also worth noting. Execution can be split into tasks
483running on different task runners, where each task _exclusively_ accesses
484certain objects in memory without explicit synchronization. Posting another task
485transfers this 'ownership' (of the objects) to the next task. With this the
486notion of object ownership can often be extended to the level of task runners,
487which provides useful invariants to reason about. This model allows to avoid
488race conditions while also avoiding locks and atomic operations. Because of its
489simplicity this model is commonly used in Chrome.
Egor Pasko7f58c33b2023-02-17 13:19:56490
fdoraybacba4a22017-05-10 21:10:00491## Annotating Tasks with TaskTraits
492
Gabriel Charette39db4c62019-04-29 19:52:38493[`base::TaskTraits`](https://cs.chromium.org/chromium/src/base/task/task_traits.h)
Gabriel Charette52fa3ae2019-04-15 21:44:37494encapsulate information about a task that helps the thread pool make better
fdoraybacba4a22017-05-10 21:10:00495scheduling decisions.
496
Gabriel Charette43de5c42020-01-27 22:44:45497Methods that take `base::TaskTraits` can be be passed `{}` when default traits
498are sufficient. Default traits are appropriate for tasks that:
Nigel Tao26ad1852023-03-23 01:14:56499
Gabriel Charettede41cad2020-03-03 18:05:06500- Don’t block (ref. MayBlock and WithBaseSyncPrimitives);
501- Pertain to user-blocking activity;
502 (explicitly or implicitly by having an ordering dependency with a component
503 that does)
Gabriel Charette52fa3ae2019-04-15 21:44:37504- Can either block shutdown or be skipped on shutdown (thread pool is free to
505 choose a fitting default).
Nigel Tao26ad1852023-03-23 01:14:56506
fdoraybacba4a22017-05-10 21:10:00507Tasks that don’t match this description must be posted with explicit TaskTraits.
508
Gabriel Charette04b138f2018-08-06 00:03:22509[`base/task/task_traits.h`](https://cs.chromium.org/chromium/src/base/task/task_traits.h)
Eric Seckler6cf08db82018-08-30 12:01:55510provides exhaustive documentation of available traits. The content layer also
511provides additional traits in
512[`content/public/browser/browser_task_traits.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_task_traits.h)
513to facilitate posting a task onto a BrowserThread.
514
Gabriel Charette39db4c62019-04-29 19:52:38515Below are some examples of how to specify `base::TaskTraits`.
fdoraybacba4a22017-05-10 21:10:00516
517```cpp
Gabriel Charettede41cad2020-03-03 18:05:06518// This task has no explicit TaskTraits. It cannot block. Its priority is
519// USER_BLOCKING. It will either block shutdown or be skipped on shutdown.
Gabriel Charette43de5c42020-01-27 22:44:45520base::ThreadPool::PostTask(FROM_HERE, base::BindOnce(...));
fdoraybacba4a22017-05-10 21:10:00521
Gabriel Charettede41cad2020-03-03 18:05:06522// This task has the highest priority. The thread pool will schedule it before
523// USER_VISIBLE and BEST_EFFORT tasks.
Gabriel Charette43de5c42020-01-27 22:44:45524base::ThreadPool::PostTask(
fdoraybacba4a22017-05-10 21:10:00525 FROM_HERE, {base::TaskPriority::USER_BLOCKING},
526 base::BindOnce(...));
527
528// This task has the lowest priority and is allowed to block (e.g. it
529// can read a file from disk).
Gabriel Charette43de5c42020-01-27 22:44:45530base::ThreadPool::PostTask(
Gabriel Charetteb10aeeb2018-07-26 20:15:00531 FROM_HERE, {base::TaskPriority::BEST_EFFORT, base::MayBlock()},
fdoraybacba4a22017-05-10 21:10:00532 base::BindOnce(...));
533
534// This task blocks shutdown. The process won't exit before its
535// execution is complete.
Gabriel Charette43de5c42020-01-27 22:44:45536base::ThreadPool::PostTask(
fdoraybacba4a22017-05-10 21:10:00537 FROM_HERE, {base::TaskShutdownBehavior::BLOCK_SHUTDOWN},
538 base::BindOnce(...));
539```
540
541## Keeping the Browser Responsive
542
543Do not perform expensive work on the main thread, the IO thread or any sequence
544that is expected to run tasks with a low latency. Instead, perform expensive
Gabriel Charette43de5c42020-01-27 22:44:45545work asynchronously using `base::ThreadPool::PostTaskAndReply*()` or
Gabriel Charette39db4c62019-04-29 19:52:38546`base::SequencedTaskRunner::PostTaskAndReply()`. Note that
547asynchronous/overlapped I/O on the IO thread are fine.
fdoraybacba4a22017-05-10 21:10:00548
549Example: Running the code below on the main thread will prevent the browser from
550responding to user input for a long time.
551
552```cpp
553// GetHistoryItemsFromDisk() may block for a long time.
554// AddHistoryItemsToOmniboxDropDown() updates the UI and therefore must
555// be called on the main thread.
556AddHistoryItemsToOmniboxDropdown(GetHistoryItemsFromDisk("keyword"));
557```
558
559The code below solves the problem by scheduling a call to
560`GetHistoryItemsFromDisk()` in a thread pool followed by a call to
561`AddHistoryItemsToOmniboxDropdown()` on the origin sequence (the main thread in
562this case). The return value of the first call is automatically provided as
563argument to the second call.
564
565```cpp
Gabriel Charette43de5c42020-01-27 22:44:45566base::ThreadPool::PostTaskAndReplyWithResult(
fdoraybacba4a22017-05-10 21:10:00567 FROM_HERE, {base::MayBlock()},
568 base::BindOnce(&GetHistoryItemsFromDisk, "keyword"),
569 base::BindOnce(&AddHistoryItemsToOmniboxDropdown));
570```
571
572## Posting a Task with a Delay
573
574### Posting a One-Off Task with a Delay
575
576To post a task that must run once after a delay expires, use
Gabriel Charette43de5c42020-01-27 22:44:45577`base::ThreadPool::PostDelayedTask*()` or `base::TaskRunner::PostDelayedTask()`.
fdoraybacba4a22017-05-10 21:10:00578
579```cpp
Gabriel Charette43de5c42020-01-27 22:44:45580base::ThreadPool::PostDelayedTask(
Gabriel Charetteb10aeeb2018-07-26 20:15:00581 FROM_HERE, {base::TaskPriority::BEST_EFFORT}, base::BindOnce(&Task),
Peter Kastinge5a38ed2021-10-02 03:06:35582 base::Hours(1));
fdoraybacba4a22017-05-10 21:10:00583
584scoped_refptr<base::SequencedTaskRunner> task_runner =
Gabriel Charette43de5c42020-01-27 22:44:45585 base::ThreadPool::CreateSequencedTaskRunner(
586 {base::TaskPriority::BEST_EFFORT});
fdoraybacba4a22017-05-10 21:10:00587task_runner->PostDelayedTask(
Peter Kastinge5a38ed2021-10-02 03:06:35588 FROM_HERE, base::BindOnce(&Task), base::Hours(1));
fdoraybacba4a22017-05-10 21:10:00589```
590
591*** note
592**NOTE:** A task that has a 1-hour delay probably doesn’t have to run right away
Gabriel Charetteb10aeeb2018-07-26 20:15:00593when its delay expires. Specify `base::TaskPriority::BEST_EFFORT` to prevent it
fdoraybacba4a22017-05-10 21:10:00594from slowing down the browser when its delay expires.
595***
596
597### Posting a Repeating Task with a Delay
598To post a task that must run at regular intervals,
599use [`base::RepeatingTimer`](https://cs.chromium.org/chromium/src/base/timer/timer.h).
600
601```cpp
602class A {
603 public:
604 ~A() {
605 // The timer is stopped automatically when it is deleted.
606 }
607 void StartDoingStuff() {
Peter Kasting53fd6ee2021-10-05 20:40:48608 timer_.Start(FROM_HERE, Seconds(1),
Erik Chen0ee26a32021-07-14 20:04:47609 this, &A::DoStuff);
fdoraybacba4a22017-05-10 21:10:00610 }
611 void StopDoingStuff() {
612 timer_.Stop();
613 }
614 private:
615 void DoStuff() {
616 // This method is called every second on the sequence that invoked
617 // StartDoingStuff().
618 }
619 base::RepeatingTimer timer_;
620};
621```
622
623## Cancelling a Task
624
625### Using base::WeakPtr
626
627[`base::WeakPtr`](https://cs.chromium.org/chromium/src/base/memory/weak_ptr.h)
628can be used to ensure that any callback bound to an object is canceled when that
629object is destroyed.
630
631```cpp
632int Compute() { … }
633
634class A {
635 public:
fdoraybacba4a22017-05-10 21:10:00636 void ComputeAndStore() {
637 // Schedule a call to Compute() in a thread pool followed by
638 // a call to A::Store() on the current sequence. The call to
639 // A::Store() is canceled when |weak_ptr_factory_| is destroyed.
640 // (guarantees that |this| will not be used-after-free).
Gabriel Charette43de5c42020-01-27 22:44:45641 base::ThreadPool::PostTaskAndReplyWithResult(
fdoraybacba4a22017-05-10 21:10:00642 FROM_HERE, base::BindOnce(&Compute),
643 base::BindOnce(&A::Store, weak_ptr_factory_.GetWeakPtr()));
644 }
645
646 private:
647 void Store(int value) { value_ = value; }
648
649 int value_;
Jeremy Roman0dd0b2f2019-07-16 21:00:43650 base::WeakPtrFactory<A> weak_ptr_factory_{this};
fdoraybacba4a22017-05-10 21:10:00651};
652```
653
Eugene Zemtsov48b30d32022-03-18 23:56:11654Note: `WeakPtr` is not thread-safe: `~WeakPtrFactory()` and
Francois Dorayf652a9d02021-07-06 13:07:52655`Store()` (bound to a `WeakPtr`) must all run on the same sequence.
fdoraybacba4a22017-05-10 21:10:00656
657### Using base::CancelableTaskTracker
658
659[`base::CancelableTaskTracker`](https://cs.chromium.org/chromium/src/base/task/cancelable_task_tracker.h)
660allows cancellation to happen on a different sequence than the one on which
661tasks run. Keep in mind that `CancelableTaskTracker` cannot cancel tasks that
662have already started to run.
663
664```cpp
Gabriel Charette43de5c42020-01-27 22:44:45665auto task_runner = base::ThreadPool::CreateTaskRunner({});
fdoraybacba4a22017-05-10 21:10:00666base::CancelableTaskTracker cancelable_task_tracker;
667cancelable_task_tracker.PostTask(task_runner.get(), FROM_HERE,
Peter Kasting341e1fb2018-02-24 00:03:01668 base::DoNothing());
fdoraybacba4a22017-05-10 21:10:00669// Cancels Task(), only if it hasn't already started running.
670cancelable_task_tracker.TryCancelAll();
671```
672
Etienne Pierre-dorayd3882992020-01-14 20:34:11673## Posting a Job to run in parallel
674
675The [`base::PostJob`](https://cs.chromium.org/chromium/src/base/task/post_job.h)
676is a power user API to be able to schedule a single base::RepeatingCallback
Albert J. Wongf06ff5002021-07-08 20:37:00677worker task and request that ThreadPool workers invoke it in parallel.
Etienne Pierre-dorayd3882992020-01-14 20:34:11678This avoids degenerate cases:
679* Calling `PostTask()` for each work item, causing significant overhead.
680* Fixed number of `PostTask()` calls that split the work and might run for a
681 long time. This is problematic when many components post “num cores” tasks and
682 all expect to use all the cores. In these cases, the scheduler lacks context
683 to be fair to multiple same-priority requests and/or ability to request lower
684 priority work to yield when high priority work comes in.
685
Etienne Pierre-doray6d3cd9192020-04-06 21:10:37686See [`base/task/job_perftest.cc`](https://cs.chromium.org/chromium/src/base/task/job_perftest.cc)
687for a complete example.
688
Etienne Pierre-dorayd3882992020-01-14 20:34:11689```cpp
690// A canonical implementation of |worker_task|.
691void WorkerTask(base::JobDelegate* job_delegate) {
692 while (!job_delegate->ShouldYield()) {
693 auto work_item = TakeWorkItem(); // Smallest unit of work.
694 if (!work_item)
695 return:
696 ProcessWork(work_item);
697 }
698}
699
700// Returns the latest thread-safe number of incomplete work items.
Etienne Pierre-Dorayf91d7a02020-09-11 15:53:27701void NumIncompleteWorkItems(size_t worker_count) {
702 // NumIncompleteWorkItems() may use |worker_count| if it needs to account for
703 // local work lists, which is easier than doing its own accounting, keeping in
704 // mind that the actual number of items may be racily overestimated and thus
705 // WorkerTask() may be called when there's no available work.
706 return GlobalQueueSize() + worker_count;
707}
Etienne Pierre-dorayd3882992020-01-14 20:34:11708
Gabriel Charette1138d602020-01-29 08:51:52709base::PostJob(FROM_HERE, {},
Etienne Pierre-dorayd3882992020-01-14 20:34:11710 base::BindRepeating(&WorkerTask),
711 base::BindRepeating(&NumIncompleteWorkItems));
712```
713
714By doing as much work as possible in a loop when invoked, the worker task avoids
715scheduling overhead. Meanwhile `base::JobDelegate::ShouldYield()` is
716periodically invoked to conditionally exit and let the scheduler prioritize
717other work. This yield-semantic allows, for example, a user-visible job to use
718all cores but get out of the way when a user-blocking task comes in.
719
Jared Saulea867ab2021-07-15 17:39:01720### Adding additional work to a running job
Etienne Pierre-dorayd3882992020-01-14 20:34:11721
722When new work items are added and the API user wants additional threads to
Albert J. Wongf06ff5002021-07-08 20:37:00723invoke the worker task in parallel,
Etienne Pierre-dorayd3882992020-01-14 20:34:11724`JobHandle/JobDelegate::NotifyConcurrencyIncrease()` *must* be invoked shortly
725after max concurrency increases.
726
fdoraybacba4a22017-05-10 21:10:00727## Testing
728
Gabriel Charette0b20ee62019-09-18 14:06:12729For more details see [Testing Components Which Post
730Tasks](threading_and_tasks_testing.md).
731
Sean Maher03efef12022-09-23 22:43:13732To test code that uses `base::SingleThreadTaskRunner::CurrentDefaultHandle`,
733`base::SequencedTaskRunner::CurrentDefaultHandle` or a function in
Gabriel Charette9b6c04072022-04-01 23:22:46734[`base/task/thread_pool.h`](https://cs.chromium.org/chromium/src/base/task/thread_pool.h),
Gabriel Charette39db4c62019-04-29 19:52:38735instantiate a
Gabriel Charette0b20ee62019-09-18 14:06:12736[`base::test::TaskEnvironment`](https://cs.chromium.org/chromium/src/base/test/task_environment.h)
Gabriel Charette39db4c62019-04-29 19:52:38737for the scope of the test. If you need BrowserThreads, use
Gabriel Charette798fde72019-08-20 22:24:04738`content::BrowserTaskEnvironment` instead of
Gabriel Charette694c3c332019-08-19 14:53:05739`base::test::TaskEnvironment`.
fdoraybacba4a22017-05-10 21:10:00740
Gabriel Charette694c3c332019-08-19 14:53:05741Tests can run the `base::test::TaskEnvironment`'s message pump using a
Gabriel Charette39db4c62019-04-29 19:52:38742`base::RunLoop`, which can be made to run until `Quit()` (explicitly or via
743`RunLoop::QuitClosure()`), or to `RunUntilIdle()` ready-to-run tasks and
744immediately return.
Wezd9e4cb772019-01-09 03:07:03745
Wez9d5dd282020-02-10 17:21:22746TaskEnvironment configures RunLoop::Run() to GTEST_FAIL() if it hasn't been
Wezd9e4cb772019-01-09 03:07:03747explicitly quit after TestTimeouts::action_timeout(). This is preferable to
748having the test hang if the code under test fails to trigger the RunLoop to
Wez9d5dd282020-02-10 17:21:22749quit. The timeout can be overridden with base::test::ScopedRunLoopTimeout.
Wezd9e4cb772019-01-09 03:07:03750
fdoraybacba4a22017-05-10 21:10:00751```cpp
752class MyTest : public testing::Test {
753 public:
754 // ...
755 protected:
Gabriel Charette694c3c332019-08-19 14:53:05756 base::test::TaskEnvironment task_environment_;
fdoraybacba4a22017-05-10 21:10:00757};
758
Xiaohan Wang5279ee02022-10-19 22:21:06759TEST_F(MyTest, FirstTest) {
Sean Maher03efef12022-09-23 22:43:13760 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, base::BindOnce(&A));
761 base::SequencedTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE,
fdoraybacba4a22017-05-10 21:10:00762 base::BindOnce(&B));
Sean Maher03efef12022-09-23 22:43:13763 base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
fdoraybacba4a22017-05-10 21:10:00764 FROM_HERE, base::BindOnce(&C), base::TimeDelta::Max());
765
Sean Maher03efef12022-09-23 22:43:13766 // This runs the (SingleThread|Sequenced)TaskRunner::CurrentDefaultHandle queue until it is empty.
fdoraybacba4a22017-05-10 21:10:00767 // Delayed tasks are not added to the queue until they are ripe for execution.
Gabriel Charettebd126bc32022-02-01 18:19:19768 // Prefer explicit exit conditions to RunUntilIdle when possible:
769 // bit.ly/run-until-idle-with-care2.
fdoraybacba4a22017-05-10 21:10:00770 base::RunLoop().RunUntilIdle();
771 // A and B have been executed. C is not ripe for execution yet.
772
773 base::RunLoop run_loop;
Sean Maher03efef12022-09-23 22:43:13774 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, base::BindOnce(&D));
775 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, run_loop.QuitClosure());
776 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(FROM_HERE, base::BindOnce(&E));
fdoraybacba4a22017-05-10 21:10:00777
Sean Maher03efef12022-09-23 22:43:13778 // This runs the (SingleThread|Sequenced)TaskRunner::CurrentDefaultHandle queue until QuitClosure is
fdoraybacba4a22017-05-10 21:10:00779 // invoked.
780 run_loop.Run();
781 // D and run_loop.QuitClosure() have been executed. E is still in the queue.
782
Gabriel Charette52fa3ae2019-04-15 21:44:37783 // Tasks posted to thread pool run asynchronously as they are posted.
Gabriel Charette43de5c42020-01-27 22:44:45784 base::ThreadPool::PostTask(FROM_HERE, {}, base::BindOnce(&F));
fdoraybacba4a22017-05-10 21:10:00785 auto task_runner =
Gabriel Charette43de5c42020-01-27 22:44:45786 base::ThreadPool::CreateSequencedTaskRunner({});
fdoraybacba4a22017-05-10 21:10:00787 task_runner->PostTask(FROM_HERE, base::BindOnce(&G));
788
Gabriel Charette52fa3ae2019-04-15 21:44:37789 // To block until all tasks posted to thread pool are done running:
Gabriel Charette43fd3702019-05-29 16:36:51790 base::ThreadPoolInstance::Get()->FlushForTesting();
fdoraybacba4a22017-05-10 21:10:00791 // F and G have been executed.
792
Gabriel Charette43de5c42020-01-27 22:44:45793 base::ThreadPool::PostTaskAndReplyWithResult(
794 FROM_HERE, {}, base::BindOnce(&H), base::BindOnce(&I));
fdoraybacba4a22017-05-10 21:10:00795
Sean Maher03efef12022-09-23 22:43:13796 // This runs the (SingleThread|Sequenced)TaskRunner::CurrentDefaultHandle queue until both the
797 // (SingleThread|Sequenced)TaskRunner::CurrentDefaultHandle queue and the ThreadPool queue are
Gabriel Charettebd126bc32022-02-01 18:19:19798 // empty. Prefer explicit exit conditions to RunUntilIdle when possible:
799 // bit.ly/run-until-idle-with-care2.
Gabriel Charette694c3c332019-08-19 14:53:05800 task_environment_.RunUntilIdle();
fdoraybacba4a22017-05-10 21:10:00801 // E, H, I have been executed.
802}
803```
804
Gabriel Charette52fa3ae2019-04-15 21:44:37805## Using ThreadPool in a New Process
fdoraybacba4a22017-05-10 21:10:00806
Gabriel Charette43fd3702019-05-29 16:36:51807ThreadPoolInstance needs to be initialized in a process before the functions in
Gabriel Charette9b6c04072022-04-01 23:22:46808[`base/task/thread_pool.h`](https://cs.chromium.org/chromium/src/base/task/thread_pool.h)
Gabriel Charette43fd3702019-05-29 16:36:51809can be used. Initialization of ThreadPoolInstance in the Chrome browser process
810and child processes (renderer, GPU, utility) has already been taken care of. To
811use ThreadPoolInstance in another process, initialize ThreadPoolInstance early
812in the main function:
fdoraybacba4a22017-05-10 21:10:00813
814```cpp
Gabriel Charette43fd3702019-05-29 16:36:51815// This initializes and starts ThreadPoolInstance with default params.
816base::ThreadPoolInstance::CreateAndStartWithDefaultParams(“process_name”);
Gabriel Charette9b6c04072022-04-01 23:22:46817// The base/task/thread_pool.h API can now be used with base::ThreadPool trait.
Jared Saulea867ab2021-07-15 17:39:01818// Tasks will be scheduled as they are posted.
fdoraybacba4a22017-05-10 21:10:00819
Gabriel Charette43fd3702019-05-29 16:36:51820// This initializes ThreadPoolInstance.
821base::ThreadPoolInstance::Create(“process_name”);
Gabriel Charette9b6c04072022-04-01 23:22:46822// The base/task/thread_pool.h API can now be used with base::ThreadPool trait. No
Gabriel Charette43fd3702019-05-29 16:36:51823// threads will be created and no tasks will be scheduled until after Start() is
824// called.
825base::ThreadPoolInstance::Get()->Start(params);
Gabriel Charette52fa3ae2019-04-15 21:44:37826// ThreadPool can now create threads and schedule tasks.
fdoraybacba4a22017-05-10 21:10:00827```
828
Gabriel Charette43fd3702019-05-29 16:36:51829And shutdown ThreadPoolInstance late in the main function:
fdoraybacba4a22017-05-10 21:10:00830
831```cpp
Gabriel Charette43fd3702019-05-29 16:36:51832base::ThreadPoolInstance::Get()->Shutdown();
fdoraybacba4a22017-05-10 21:10:00833// Tasks posted with TaskShutdownBehavior::BLOCK_SHUTDOWN and
834// tasks posted with TaskShutdownBehavior::SKIP_ON_SHUTDOWN that
835// have started to run before the Shutdown() call have now completed their
836// execution. Tasks posted with
837// TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN may still be
838// running.
839```
Gabriel Charetteb86e5fe62017-06-08 19:39:28840## TaskRunner ownership (encourage no dependency injection)
Sebastien Marchandc95489b2017-05-25 16:39:34841
842TaskRunners shouldn't be passed through several components. Instead, the
Jared Saulea867ab2021-07-15 17:39:01843component that uses a TaskRunner should be the one that creates it.
Sebastien Marchandc95489b2017-05-25 16:39:34844
845See [this example](https://codereview.chromium.org/2885173002/) of a
846refactoring where a TaskRunner was passed through a lot of components only to be
847used in an eventual leaf. The leaf can and should now obtain its TaskRunner
848directly from
Gabriel Charette9b6c04072022-04-01 23:22:46849[`base/task/thread_pool.h`](https://cs.chromium.org/chromium/src/base/task/thread_pool.h).
Gabriel Charetteb86e5fe62017-06-08 19:39:28850
Gabriel Charette694c3c332019-08-19 14:53:05851As mentioned above, `base::test::TaskEnvironment` allows unit tests to
Gabriel Charette39db4c62019-04-29 19:52:38852control tasks posted from underlying TaskRunners. In rare cases where a test
853needs to more precisely control task ordering: dependency injection of
854TaskRunners can be useful. For such cases the preferred approach is the
855following:
Gabriel Charetteb86e5fe62017-06-08 19:39:28856
857```cpp
Gabriel Charette39db4c62019-04-29 19:52:38858class Foo {
Gabriel Charetteb86e5fe62017-06-08 19:39:28859 public:
860
Gabriel Charette39db4c62019-04-29 19:52:38861 // Overrides |background_task_runner_| in tests.
Gabriel Charetteb86e5fe62017-06-08 19:39:28862 void SetBackgroundTaskRunnerForTesting(
Gabriel Charette39db4c62019-04-29 19:52:38863 scoped_refptr<base::SequencedTaskRunner> background_task_runner) {
864 background_task_runner_ = std::move(background_task_runner);
865 }
Gabriel Charetteb86e5fe62017-06-08 19:39:28866
867 private:
michaelpg12c04572017-06-26 23:25:06868 scoped_refptr<base::SequencedTaskRunner> background_task_runner_ =
Gabriel Charette43de5c42020-01-27 22:44:45869 base::ThreadPool::CreateSequencedTaskRunner(
Gabriel Charetteb10aeeb2018-07-26 20:15:00870 {base::MayBlock(), base::TaskPriority::BEST_EFFORT});
Gabriel Charetteb86e5fe62017-06-08 19:39:28871}
872```
873
874Note that this still allows removing all layers of plumbing between //chrome and
875that component since unit tests will use the leaf layer directly.
Gabriel Charette8917f4c2018-11-22 15:50:28876
877## FAQ
878See [Threading and Tasks FAQ](threading_and_tasks_faq.md) for more examples.
Gabriel Charette43de5c42020-01-27 22:44:45879
880[task APIs v3]: https://docs.google.com/document/d/1tssusPykvx3g0gvbvU4HxGyn3MjJlIylnsH13-Tv6s4/edit?ts=5de99a52#heading=h.ss4tw38hvh3s
Carlos Caballero40b6d042020-06-16 06:50:25881
882## Internals
883
884### SequenceManager
885
886[SequenceManager](https://cs.chromium.org/chromium/src/base/task/sequence_manager/sequence_manager.h)
887manages TaskQueues which have different properties (e.g. priority, common task
888type) multiplexing all posted tasks into a single backing sequence. This will
889usually be a MessagePump. Depending on the type of message pump used other
890events such as UI messages may be processed as well. On Windows APC calls (as
891time permits) and signals sent to a registered set of HANDLEs may also be
892processed.
893
Carlos Caballero4a050922020-07-02 11:43:38894### MessagePump
Carlos Caballero40b6d042020-06-16 06:50:25895
896[MessagePumps](https://cs.chromium.org/chromium/src/base/message_loop/message_pump.h)
897are responsible for processing native messages as well as for giving cycles to
898their delegate (SequenceManager) periodically. MessagePumps take care to mixing
899delegate callbacks with native message processing so neither type of event
900starves the other of cycles.
901
902There are different [MessagePumpTypes](https://cs.chromium.org/chromium/src/base/message_loop/message_pump_type.h),
903most common are:
904
905* DEFAULT: Supports tasks and timers only
906
907* UI: Supports native UI events (e.g. Windows messages)
908
909* IO: Supports asynchronous IO (not file I/O!)
910
911* CUSTOM: User provided implementation of MessagePump interface
912
Carlos Caballero4a050922020-07-02 11:43:38913### RunLoop
Carlos Caballero40b6d042020-06-16 06:50:25914
Jared Saulea867ab2021-07-15 17:39:01915RunLoop is a helper class to run the RunLoop::Delegate associated with the
Carlos Caballero40b6d042020-06-16 06:50:25916current thread (usually a SequenceManager). Create a RunLoop on the stack and
917call Run/Quit to run a nested RunLoop but please avoid nested loops in
918production code!
919
Carlos Caballero4a050922020-07-02 11:43:38920### Task Reentrancy
Carlos Caballero40b6d042020-06-16 06:50:25921
922SequenceManager has task reentrancy protection. This means that if a
923task is being processed, a second task cannot start until the first task is
924finished. Reentrancy can happen when processing a task, and an inner
925message pump is created. That inner pump then processes native messages
926which could implicitly start an inner task. Inner message pumps are created
927with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions
928(DoDragDrop), printer functions (StartDoc) and *many* others.
929
930```cpp
931Sample workaround when inner task processing is needed:
932 HRESULT hr;
933 {
Francois Doraya06ee172022-11-24 21:09:18934 CurrentThread::ScopedAllowApplicationTasksInNativeNestedLoop allow;
Carlos Caballero40b6d042020-06-16 06:50:25935 hr = DoDragDrop(...); // Implicitly runs a modal message loop.
936 }
937 // Process |hr| (the result returned by DoDragDrop()).
938```
939
940Please be SURE your task is reentrant (nestable) and all global variables
941are stable and accessible before before using
Francois Doraya06ee172022-11-24 21:09:18942CurrentThread::ScopedAllowApplicationTasksInNativeNestedLoop.
Carlos Caballero40b6d042020-06-16 06:50:25943
944## APIs for general use
945
946User code should hardly ever need to access SequenceManager APIs directly as
947these are meant for code that deals with scheduling. Instead you should use the
948following:
949
950* base::RunLoop: Drive the SequenceManager from the thread it's bound to.
951
Sean Maher03efef12022-09-23 22:43:13952* base::Thread/SequencedTaskRunner::CurrentDefaultHandle: Post back to the SequenceManager TaskQueues from a task running on it.
Carlos Caballero40b6d042020-06-16 06:50:25953
954* SequenceLocalStorageSlot : Bind external state to a sequence.
955
Carlos Caballero4a050922020-07-02 11:43:38956* base::CurrentThread : Proxy to a subset of Task related APIs bound to the current thread
Carlos Caballero40b6d042020-06-16 06:50:25957
958* Embedders may provide their own static accessors to post tasks on specific loops (e.g. content::BrowserThreads).
959
960### SingleThreadTaskExecutor and TaskEnvironment
961
962Instead of having to deal with SequenceManager and TaskQueues code that needs a
963simple task posting environment (one default task queue) can use a
964[SingleThreadTaskExecutor](https://cs.chromium.org/chromium/src/base/task/single_thread_task_executor.h).
965
966Unit tests can use [TaskEnvironment](https://cs.chromium.org/chromium/src/base/test/task_environment.h)
967which is highly configurable.
Carlos Caballero4a050922020-07-02 11:43:38968
Wen Fane09439ca2021-03-09 16:50:41969## MessageLoop and MessageLoopCurrent
Carlos Caballero4a050922020-07-02 11:43:38970
Wen Fane09439ca2021-03-09 16:50:41971You might come across references to MessageLoop or MessageLoopCurrent in the
Carlos Caballero4a050922020-07-02 11:43:38972code or documentation. These classes no longer exist and we are in the process
Jared Saulea867ab2021-07-15 17:39:01973or getting rid of all references to them. `base::MessageLoopCurrent` was
974replaced by `base::CurrentThread` and the drop in replacements for
975`base::MessageLoop` are `base::SingleThreadTaskExecutor` and
976`base::Test::TaskEnvironment`.