Commit 6cf08db8 authored by Eric Seckler's avatar Eric Seckler Committed by Commit Bot

docs: Update threading_and_tasks.md for BrowserThread task traits.

Bug: 878356, 867421
Change-Id: I0991a840a0787b6863bea79ee4079e5db7fdfe0b
Reviewed-on: https://chromium-review.googlesource.com/1194074
Commit-Queue: Eric Seckler <eseckler@chromium.org>
Reviewed-by: default avatarSami Kyöstilä <skyostil@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#587525}
parent c2d2ff88
...@@ -254,14 +254,16 @@ posting order. ...@@ -254,14 +254,16 @@ posting order.
### Posting to the Main Thread or to the IO Thread in the Browser Process ### Posting to the Main Thread or to the IO Thread in the Browser Process
To post tasks to the main thread or to the IO thread, get the appropriate To post tasks to the main thread or to the IO thread, use
SingleThreadTaskRunner using `content::BrowserThread::GetTaskRunnerForThread`. `base::PostTaskWithTraits()` or get the appropriate SingleThreadTaskRunner using
`base::CreateSingleThreadTaskRunnerWithTraits`, supplying a `BrowserThread::ID`
as trait. For this, you'll also need to include
[`content/public/browser/browser_task_traits.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_task_traits.h).
```cpp ```cpp
content::BrowserThread::GetTaskRunnerForThread(content::BrowserThread::UI) base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::UI}, ...);
->PostTask(FROM_HERE, ...);
content::BrowserThread::GetTaskRunnerForThread(content::BrowserThread::IO) base::CreateSingleThreadTaskRunnerWithTraits({content::BrowserThread::IO})
->PostTask(FROM_HERE, ...); ->PostTask(FROM_HERE, ...);
``` ```
...@@ -374,8 +376,12 @@ that: ...@@ -374,8 +376,12 @@ that:
Tasks that don’t match this description must be posted with explicit TaskTraits. Tasks that don’t match this description must be posted with explicit TaskTraits.
[`base/task/task_traits.h`](https://cs.chromium.org/chromium/src/base/task/task_traits.h) [`base/task/task_traits.h`](https://cs.chromium.org/chromium/src/base/task/task_traits.h)
provides exhaustive documentation of available traits. Below are some examples provides exhaustive documentation of available traits. The content layer also
of how to specify `TaskTraits`. provides additional traits in
[`content/public/browser/browser_task_traits.h`](https://cs.chromium.org/chromium/src/content/public/browser/browser_task_traits.h)
to facilitate posting a task onto a BrowserThread.
Below are some examples of how to specify `TaskTraits`.
```cpp ```cpp
// This task has no explicit TaskTraits. It cannot block. Its priority // This task has no explicit TaskTraits. It cannot block. Its priority
...@@ -401,6 +407,11 @@ base::PostTaskWithTraits( ...@@ -401,6 +407,11 @@ base::PostTaskWithTraits(
base::PostTaskWithTraits( base::PostTaskWithTraits(
FROM_HERE, {base::TaskShutdownBehavior::BLOCK_SHUTDOWN}, FROM_HERE, {base::TaskShutdownBehavior::BLOCK_SHUTDOWN},
base::BindOnce(...)); base::BindOnce(...));
// This task will run on the Browser UI thread.
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(...));
``` ```
## Keeping the Browser Responsive ## Keeping the Browser Responsive
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment