2
From the v0.9.3 miner.cpp source (https://github.com/bitcoin/bitcoin/blob/v0.9.3/src/miner.cpp#L598-L604):
if (GetTimeMillis() - nHPSTimerStart > 4000)
{
static CCriticalSection cs;
{
LOCK(cs);
if (GetTimeMillis() - nHPSTimerStart > 4000)
{
....
Why is the same if statement "if (GetTimeMillis() - nHPSTimerStart > 4000)" executed twice? GetTimeMillis() can only go up, so it seems like if the first one is true, then the second statement can only ever evaluate to true as well, and is useless. I'm assuming this is not entirely correct, though, and am also assuming it has something to do with the CCriticalSelection, but would like to understand the nature of it better.
Thanks!
I agree, I think that's a copy-paste bug. Maybe you should ask
#bitcoin-devto double-check. – Nick ODell – 2014-12-04T22:01:47.8171Can any other thread modify
nHPSTimerStart? – Greg Hewgill – 2014-12-05T00:31:44.843Yes, I think so. There could be quite a few threads working on the mining. – morsecoder – 2014-12-05T00:45:46.497
1I have used something similar before. The first
ifchecks if we should bother to do the potentially expensive lock. If yes, then we lock and now thatnHPSTimerStartis guaranteed not to change we can safely execute theifagain. – Floris – 2014-12-05T13:13:06.233Thanks, Floris, that seems reasonable. If you post as an answer, I could mark it as such. – morsecoder – 2014-12-11T00:17:22.880