| OLD | NEW |
| 1 # Threading and Tasks in Chrome | 1 # Threading and Tasks in Chrome |
| 2 | 2 |
| 3 [TOC] | 3 [TOC] |
| 4 | 4 |
| 5 ## Overview | 5 ## Overview |
| 6 | 6 |
| 7 ### Threads | 7 ### Threads |
| 8 | 8 |
| 9 Every Chrome process has | 9 Every Chrome process has |
| 10 | 10 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 The `base::PostTask*WithTraits()` functions allow the caller to provide | 68 The `base::PostTask*WithTraits()` functions allow the caller to provide |
| 69 additional details about the task via TaskTraits (ref. | 69 additional details about the task via TaskTraits (ref. |
| 70 [Annotating Tasks with TaskTraits](#Annotating-Tasks-with-TaskTraits)). | 70 [Annotating Tasks with TaskTraits](#Annotating-Tasks-with-TaskTraits)). |
| 71 | 71 |
| 72 ```cpp | 72 ```cpp |
| 73 base::PostTaskWithTraits( | 73 base::PostTaskWithTraits( |
| 74 FROM_HERE, {base::TaskPriority::BACKGROUND, MayBlock()}, | 74 FROM_HERE, {base::TaskPriority::BACKGROUND, MayBlock()}, |
| 75 base::BindOnce(&Task)); | 75 base::BindOnce(&Task)); |
| 76 ``` | 76 ``` |
| 77 | 77 |
| 78 ## Posting via a TaskRunner | 78 ### Posting via a TaskRunner |
| 79 | 79 |
| 80 A parallel | 80 A parallel |
| 81 [`TaskRunner`](https://cs.chromium.org/chromium/src/base/task_runner.h) is an | 81 [`TaskRunner`](https://cs.chromium.org/chromium/src/base/task_runner.h) is an |
| 82 alternative to calling `base::PostTask*()` directly. This is mainly useful when | 82 alternative to calling `base::PostTask*()` directly. This is mainly useful when |
| 83 it isn’t known in advance whether tasks will be posted in parallel, in sequence, | 83 it isn’t known in advance whether tasks will be posted in parallel, in sequence, |
| 84 or to a single-thread (ref. [Posting a Sequenced Task](#Posting-a-Sequenced- | 84 or to a single-thread (ref. |
| 85 Task), [Posting Multiple Tasks to the Same Thread](#Posting-Multiple-Tasks-to- | 85 [Posting a Sequenced Task](#Posting-a-Sequenced-Task), |
| 86 the-Same-Thread)). Since `TaskRunner` is the base class of `SequencedTaskRunner` | 86 [Posting Multiple Tasks to the Same Thread](#Posting-Multiple-Tasks-to-the-Same-
Thread)). |
| 87 and `SingleThreadTaskRunner`, a `scoped_refptr<TaskRunner>` member can hold a | 87 Since `TaskRunner` is the base class of `SequencedTaskRunner` and |
| 88 `SingleThreadTaskRunner`, a `scoped_refptr<TaskRunner>` member can hold a |
| 88 `TaskRunner`, a `SequencedTaskRunner` or a `SingleThreadTaskRunner`. | 89 `TaskRunner`, a `SequencedTaskRunner` or a `SingleThreadTaskRunner`. |
| 89 | 90 |
| 90 ```cpp | 91 ```cpp |
| 91 class A { | 92 class A { |
| 92 public: | 93 public: |
| 93 A() = default; | 94 A() = default; |
| 94 | 95 |
| 95 void set_task_runner_for_testing( | 96 void set_task_runner_for_testing( |
| 96 scoped_refptr<base::TaskRunner> task_runner) { | 97 scoped_refptr<base::TaskRunner> task_runner) { |
| 97 task_runner_ = std::move(task_runner); | 98 task_runner_ = std::move(task_runner); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 | 211 |
| 211 ```cpp | 212 ```cpp |
| 212 content::BrowserThread::GetTaskRunnerForThread(content::BrowserThread::UI) | 213 content::BrowserThread::GetTaskRunnerForThread(content::BrowserThread::UI) |
| 213 ->PostTask(FROM_HERE, ...); | 214 ->PostTask(FROM_HERE, ...); |
| 214 | 215 |
| 215 content::BrowserThread::GetTaskRunnerForThread(content::BrowserThread::IO) | 216 content::BrowserThread::GetTaskRunnerForThread(content::BrowserThread::IO) |
| 216 ->PostTask(FROM_HERE, ...); | 217 ->PostTask(FROM_HERE, ...); |
| 217 ``` | 218 ``` |
| 218 | 219 |
| 219 The main thread and the IO thread are already super busy. Therefore, prefer | 220 The main thread and the IO thread are already super busy. Therefore, prefer |
| 220 posting to a general purpose thread when possible (ref. [Posting a Parallel | 221 posting to a general purpose thread when possible (ref. |
| 221 Task](#Posting-a-Parallel-Task), [Posting a Sequenced task](#Posting-a | 222 [Posting a Parallel Task](#Posting-a-Parallel-Task), |
| 222 -Sequenced-Task)). Good reasons to post to the main thread are to update the UI | 223 [Posting a Sequenced task](#Posting-a-Sequenced-Task)). |
| 223 or access objects that are bound to it (e.g. `Profile`). A good reason to post | 224 Good reasons to post to the main thread are to update the UI or access objects |
| 224 to the IO thread is to access the internals of components that are bound to it | 225 that are bound to it (e.g. `Profile`). A good reason to post to the IO thread is |
| 225 (e.g. IPCs, network). Note: It is not necessary to have an explicit post task to | 226 to access the internals of components that are bound to it (e.g. IPCs, network). |
| 226 the IO thread to send/receive an IPC or send/receive data on the network. | 227 Note: It is not necessary to have an explicit post task to the IO thread to |
| 228 send/receive an IPC or send/receive data on the network. |
| 227 | 229 |
| 228 ### Posting to the Main Thread in a Renderer Process | 230 ### Posting to the Main Thread in a Renderer Process |
| 229 TODO | 231 TODO |
| 230 | 232 |
| 231 ### Posting to a Custom SingleThreadTaskRunner | 233 ### Posting to a Custom SingleThreadTaskRunner |
| 232 | 234 |
| 233 If multiple tasks need to run on the same thread and that thread doesn’t have to | 235 If multiple tasks need to run on the same thread and that thread doesn’t have to |
| 234 be the main thread or the IO thread, post them to a `SingleThreadTaskRunner` | 236 be the main thread or the IO thread, post them to a `SingleThreadTaskRunner` |
| 235 created by `base::CreateSingleThreadTaskRunnerWithTraits`. | 237 created by `base::CreateSingleThreadTaskRunnerWithTraits`. |
| 236 | 238 |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 | 607 |
| 606 ```cpp | 608 ```cpp |
| 607 base::TaskScheduler::GetInstance()->Shutdown(); | 609 base::TaskScheduler::GetInstance()->Shutdown(); |
| 608 // Tasks posted with TaskShutdownBehavior::BLOCK_SHUTDOWN and | 610 // Tasks posted with TaskShutdownBehavior::BLOCK_SHUTDOWN and |
| 609 // tasks posted with TaskShutdownBehavior::SKIP_ON_SHUTDOWN that | 611 // tasks posted with TaskShutdownBehavior::SKIP_ON_SHUTDOWN that |
| 610 // have started to run before the Shutdown() call have now completed their | 612 // have started to run before the Shutdown() call have now completed their |
| 611 // execution. Tasks posted with | 613 // execution. Tasks posted with |
| 612 // TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN may still be | 614 // TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN may still be |
| 613 // running. | 615 // running. |
| 614 ``` | 616 ``` |
| OLD | NEW |