<blockquote>
<p>What is <code>cs_main</code>?</p>
</blockquote>
<p><code>cs_main</code> is a recursive mutex which is used to ensure that validation is carried out in an atomic way. It guards access to validation specific variables (such as <code>CChainState</code> and <code>CNode</code>) or mempool variables (in <code>net_processing</code>). The lock of <code>cs_main</code> is in <a href="https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp" rel="nofollow noreferrer">validation.cpp</a>.</p>
<blockquote>
<p>Why is it called <code>cs_main</code>?</p>
</blockquote>
<p><code>cs_main</code> was the mutex (&quot;critical section&quot;) that protected data in Satoshi's original main.cpp file. (main.cpp was entirely removed in <a href="https://github.com/bitcoin/bitcoin/pull/9260" rel="nofollow noreferrer">PR 9260</a> in December 2016.) In theory <code>cs_main</code> could be renamed today as main.cpp has since been refactored into smaller files but every time you rename something in a header file, everyone needs to recompile any file that includes that header.</p>
<p>A long-term goal is to separate out those different bits of data that are protected by <code>cs_main</code>, so that eventually <code>cs_main</code> only protects the data inside validation.</p>
<p>These were answered by John Newbery and other participants of the May 12th 2021 <a href="https://bitcoincore.reviews/21767" rel="nofollow noreferrer">Bitcoin Core PR review club</a>.</p>

CC BY-SA 4.0
2021-05-14T18:09:39.493Z
2021-11-03T17:18:15.910Z
2021-11-03T17:18:15.910Z
Michael Folkson
<p>What is <code>cs_main</code>? Why is it called <code>cs_main</code>?</p>
<p>This question was asked during the May 12th 2021 <a href="https://bitcoincore.reviews/21767" rel="nofollow noreferrer">Bitcoin Core PR review club</a> hosted by John Newbery on a Carl Dong PR to de-globalize <code>ChainstateManager</code>.</p>
<p>(The lock on <code>cs_main</code> was discussed in this StackExchange <a href="https://bitcoin.stackexchange.com/questions/38333/what-is-the-function-of-lockcs-main">question</a>.)</p>

<bitcoin-core-development><validation><c++>
What is cs_main? Why is it called cs_main?
Michael Folkson
# What is cs_main? Why is it called cs_main?

(Asked by: username_0 on 2021-05-14)

What is `cs_main`? Why is it called `cs_main`?

This question was asked during the May 12th 2021 [Bitcoin Core PR review club](https://bitcoincore.reviews/21767) hosted by John Newbery on a Carl Dong PR to de-globalize `ChainstateManager`.

(The lock on `cs_main` was discussed in this StackExchange [question](https://bitcoin.stackexchange.com/questions/38333/what-is-the-function-of-lockcs-main).)

**Tags:** bitcoin-core-development, validation, c++


---

## 1 answers

### Answer by username_0 on 2021-05-14. Score: 2 | Accepted answer

> What is `cs_main`?

`cs_main` is a recursive mutex which is used to ensure that validation is carried out in an atomic way. It guards access to validation specific variables (such as `CChainState` and `CNode`) or mempool variables (in `net_processing`). The lock of `cs_main` is in [validation.cpp](https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp).

> Why is it called `cs_main`?

`cs_main` was the mutex ("critical section") that protected data in Satoshi's original main.cpp file. (main.cpp was entirely removed in [PR 9260](https://github.com/bitcoin/bitcoin/pull/9260) in December 2016.) In theory `cs_main` could be renamed today as main.cpp has since been refactored into smaller files but every time you rename something in a header file, everyone needs to recompile any file that includes that header.

A long-term goal is to separate out those different bits of data that are protected by `cs_main`, so that eventually `cs_main` only protects the data inside validation.

These were answered by John Newbery and other participants of the May 12th 2021 [Bitcoin Core PR review club](https://bitcoincore.reviews/21767).


---

