<commit_before>---
layout: article
title: SequencedWorkerPool in chrome
category: chrome
description: SequencedWorkerPool is a worker thread pool that allow you to post unordered tasks, and it also can enforce ordering between sets of tasks. What's more, It allows you to specify what should happen to your tasks on shutdown.
---

*SequencedWorkerPool is a worker thread pool that allow you to post unordered tasks, and it also can enforce ordering between sets of tasks. What's more, It allows you to specify what should happen to your tasks on shutdown.*


## Ordered Tasks

**To enforce ordering, get a unique sequence token from the pool and post all tasks you want to order with the token.** All tasks with the same token are guaranteed to execute serially, though not necessarily on the same thread.

This means that:

- No two tasks with the same token will run at the same time.
- Given two tasks T1 and T2 with the same token such that T2 will run after T1, then T2 will start after T1 is destroyed.
- If T2 will run after T1, then all memory changes in T1 and T1's destruction will be visible to T2.

### Example:  

{% highlight c++ %}
SequencedWorkerPool::SequenceToken token = pool.GetSequenceToken();
pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN,
                           FROM_HERE, base::Bind(...));
pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN,
                           FROM_HERE, base::Bind(...));
{% endhighlight %}

### Notes

- You can **make named sequence tokens** to make it easier to share a token across different components.
- You can also post tasks to the pool **without ordering using PostWorkerTask**. These will be executed in an unspecified order. The order of execution between tasks with different sequence tokens is also unspecified.
- This class may be leaked on shutdown to facilitate fast shutdown. **The expected usage, however, is to call Shutdown()**, which correctly accounts for CONTINUE_ON_SHUTDOWN behavior and is required for BLOCK_SHUTDOWN behavior.
- {::}**When constructing a SequencedWorkerPool, there must be a MessageLoop on the current thread**{: style="color: red;"} unless you plan to deliberately leak it.


## Shotdown Behavior

{% highlight c++ %}
enum WorkerShutdown {<commit_msg>span level attr<commit_after>---
layout: article
title: SequencedWorkerPool in chrome
category: chrome
description: SequencedWorkerPool is a worker thread pool that allow you to post unordered tasks, and it also can enforce ordering between sets of tasks. What's more, It allows you to specify what should happen to your tasks on shutdown.
---

*SequencedWorkerPool is a worker thread pool that allow you to post unordered tasks, and it also can enforce ordering between sets of tasks. What's more, It allows you to specify what should happen to your tasks on shutdown.*


## Ordered Tasks

**To enforce ordering, get a unique sequence token from the pool and post all tasks you want to order with the token.** All tasks with the same token are guaranteed to execute serially, though not necessarily on the same thread.

This means that:

- No two tasks with the same token will run at the same time.
- Given two tasks T1 and T2 with the same token such that T2 will run after T1, then T2 will start after T1 is destroyed.
- If T2 will run after T1, then all memory changes in T1 and T1's destruction will be visible to T2.

### Example:  

{% highlight c++ %}
SequencedWorkerPool::SequenceToken token = pool.GetSequenceToken();
pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN,
                           FROM_HERE, base::Bind(...));
pool.PostSequencedWorkerTask(token, SequencedWorkerPool::SKIP_ON_SHUTDOWN,
                           FROM_HERE, base::Bind(...));
{% endhighlight %}

### Notes

- You can **make named sequence tokens** to make it easier to share a token across different components.
- You can also post tasks to the pool **without ordering using PostWorkerTask**. These will be executed in an unspecified order. The order of execution between tasks with different sequence tokens is also unspecified.
- This class may be leaked on shutdown to facilitate fast shutdown. **The expected usage, however, is to call Shutdown()**, which correctly accounts for CONTINUE_ON_SHUTDOWN behavior and is required for BLOCK_SHUTDOWN behavior.
- **When constructing a SequencedWorkerPool, there must be a MessageLoop on the current thread**{: style="color: red;"} unless you plan to deliberately leak it.


## Shotdown Behavior

{% highlight c++ %}
enum WorkerShutdown {
{'fraction_non_alphanumeric': 0.06652173913043478, 'fraction_numerical': 0.004782608695652174, 'mean_word_length': 4.210645526613817, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6607670', 'n_tokens_mistral': 1184, 'n_tokens_neox': 1131, 'n_words': 630}