<p><code>LOCK(cs_main)</code> causes the thread to stop execution until a lock can be obtained on cs_main. cs_main is an object representing a type/class/scope of lock. Once the lock is obtained, it lasts until the end of the LOCK statement's scope. (The LOCK macro creates an object on the stack, the destruction of which releases the lock.)</p>

<p>cs_main is already locked in that place, and LOCK uses recursive locks, so your LOCK doesn't do anything.</p>

CC BY-SA 3.0
2015-06-30T21:53:11.247Z
Ok thanks. How do I release lock after completion of following task? Is lock(cs_main) the correct lock to use at this place
Aliakbar Ahmadi
CC BY-SA 3.0
2015-06-30T23:33:31.317Z
It's automatically released at the end of the scope in which the LOCK is declared. It works similar to a boost::lock, but has some extra debugging logic that can be enabled.
Pieter Wuille
CC BY-SA 3.0
2015-06-30T23:34:20.493Z
Theymos: LOCK uses recursive locks, so locking twice should just be a no-op.
Pieter Wuille
CC BY-SA 3.0
2017-12-15T20:59:08.810Z
@PieterWuille so does a lock pause all other tasks / threads? Or just database writes ?
pinhead
CC BY-SA 3.0
2017-12-15T21:03:17.157Z
@pinhead It pauses the thread it was invoked from, until the requested lock can be grabbed.
Pieter Wuille
CC BY-SA 3.0
2017-12-15T21:04:04.670Z
@PieterWuille but what is locked?
pinhead
CC BY-SA 3.0
2017-12-15T21:06:15.867Z
@pinhead I don't know what that means. There are a number of locks (cs_main being one of them), and each lock can be held by at most one thread at a time. If the lock is already held by another thread, LOCK(cs_main) will pause until the lock becomes available. In any case, the code being executed will not progress past the LOCK(cs_main) statement without holding the cs_main lock.
Pieter Wuille
CC BY-SA 3.0
2017-12-15T21:10:16.023Z
@PieterWuille thanks that answers my question
pinhead
CC BY-SA 3.0
2015-06-30T20:16:53.753Z
2015-07-01T03:01:24.387Z
2015-07-01T03:01:24.387Z
theymos
<p>Does <code>LOCK(cs_main)</code> pause and branach the program in order to do some necessary job before going on?</p>

<p>I'm trying to publish blocks reactively (as a selfish miner) upon receipt of inventory message from <em><code>pfrom</code></em> and react to it after <code>*pfrom*</code> catches up with the  height of my secret block.</p>

<p>Following lines didn't help/there was no reaction on selfish node's side:<br/>
added following lines in <a href="https://github.com/bitcoin/bitcoin/blob/v0.10.0/src/main.cpp#L3662" rel="nofollow">main.cpp: after l. 3662</a></p>

<pre><code>   ...
        {
            LOCK(cs_main);
            pfrom-&gt;PushMessage("inv", pfrom-&gt;vPrivateInv);
        }
   ...
</code></pre>

<bitcoin-core-development><c++>
What is the function of LOCK(cs_main)?
Aliakbar Ahmadi
# What is the function of LOCK(cs_main)?

(Asked by: username_0 on 2015-06-30)

Does `LOCK(cs_main)` pause and branach the program in order to do some necessary job before going on?

I'm trying to publish blocks reactively (as a selfish miner) upon receipt of inventory message from *`pfrom`* and react to it after `*pfrom*` catches up with the height of my secret block.

Following lines didn't help/there was no reaction on selfish node's side:  
added following lines in [main.cpp: after l. 3662](https://github.com/bitcoin/bitcoin/blob/v0.10.0/src/main.cpp#L3662)

```
   ...
        {
            LOCK(cs_main);
            pfrom->PushMessage("inv", pfrom->vPrivateInv);
        }
   ...

```

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


---

## 1 answers

### Answer by username_1 on 2015-06-30. Score: 4

`LOCK(cs_main)` causes the thread to stop execution until a lock can be obtained on cs\_main. cs\_main is an object representing a type/class/scope of lock. Once the lock is obtained, it lasts until the end of the LOCK statement's scope. (The LOCK macro creates an object on the stack, the destruction of which releases the lock.)

cs\_main is already locked in that place, and LOCK uses recursive locks, so your LOCK doesn't do anything.

> **Comments:**
> **username_0**: Ok thanks. How do I release lock after completion of following task? Is lock(cs_main) the correct lock to use at this place  
> **username_2**: It's automatically released at the end of the scope in which the LOCK is declared. It works similar to a boost::lock, but has some extra debugging logic that can be enabled.  
> **username_2**: username_1: LOCK uses recursive locks, so locking twice should just be a no-op.  
> **username_3**: @username_2 so does a lock pause all other tasks / threads? Or just database writes ?  
> **username_2**: @username_3 It pauses the thread it was invoked from, until the requested lock can be grabbed.  
> **username_3**: @username_2 but what is locked?  
> **username_2**: @username_3 I don't know what that means. There are a number of locks (cs_main being one of them), and each lock can be held by at most one thread at a time. If the lock is already held by another thread, LOCK(cs_main) will pause until the lock becomes available. In any case, the code being executed will not progress past the LOCK(cs_main) statement without holding the cs_main lock.  
> **username_3**: @username_2 thanks that answers my question  

---

