What is the function of LOCK(cs_main)?

2

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

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

Aliakbar Ahmadi

Posted 2015-06-30T17:58:15.937

Reputation: 1 335

Answers

2

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.

theymos

Posted 2015-06-30T17:58:15.937

Reputation: 8 228

Ok thanks. How do I release lock after completion of following task? Is lock(cs_main) the correct lock to use at this placeAliakbar Ahmadi 2015-06-30T21:53:11.247

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 2015-06-30T23:33:31.317

Theymos: LOCK uses recursive locks, so locking twice should just be a no-op.Pieter Wuille 2015-06-30T23:34:20.493

@PieterWuille so does a lock pause all other tasks / threads? Or just database writes ?pinhead 2017-12-15T20:59:08.810

@pinhead It pauses the thread it was invoked from, until the requested lock can be grabbed.Pieter Wuille 2017-12-15T21:03:17.157

@PieterWuille but what is locked?pinhead 2017-12-15T21:04:04.670

@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 2017-12-15T21:06:15.867

@PieterWuille thanks that answers my questionpinhead 2017-12-15T21:10:16.023