<p>The <code>boost::thread_group</code> class doesn't do very much. It's basically just a container of threads. It doesn't really have any scheduling or dispatch functions. That this thread is part of a thread group doesn't really matter.</p>

<p>The <code>CScheduler</code> code allows a thread to run a task at a particular time. It requires a thread to run the various tasks that it schedules. This code creates a thread to runs those tasks for this <code>CScheduler</code> and adds it to the thread group.</p>

CC BY-SA 3.0
2015-12-24T03:53:19.950Z
Thank you so much for your explanation. Also, can you point me out what those tasks (being run in `CScheduler`) exactly are? The LogPrintf above this code snippet in `src/init.cpp` mentions that it runs script verification, but I am unable to find relevant logic in the source code.
Paarth
CC BY-SA 3.0
2015-12-24T05:43:09.270Z
Also, `GenerateBitcoins` in `sec/miner.cpp` also creates a threadgroup with which it associates the `BitcoinMiner`. As per what I understand from your explanation, that this is just associating the thread in a group. But then how is the miner run in background?
Paarth
CC BY-SA 3.0
2015-12-24T05:46:22.413Z
The bitcoin daemon no longer runs a miner in the background, or at all for that matter. The scheduler is used for periodic tasks such as making outbound connections.
David Schwartz
CC BY-SA 3.0
2015-12-26T10:01:19.630Z
But where is the scheduler for `BitcoinMiner` ? There is no scheduler logic used for running the thread group associated with mining. `miner.cpp` has logic only for thread group creation.
Paarth
CC BY-SA 3.0
2015-12-24T00:29:12.040Z
2015-12-24T00:29:12.040Z
David Schwartz
<p>I am not familiar with boost in c++. Can any expert please explain in simple terms what is happening in the below code ? Is it like the logic in <code>ThreadScriptCheck</code> is scheduled to be run in background or how? </p>

<p>This is taken from bitcoin initialization (<code>src/init.cpp</code>). The classes are defined in <code>src/scheduler.h</code></p>

<p><code>AppInit2()</code>:</p>

<pre><code>// Assume value of nScriptCheckThreads is 4

if (nScriptCheckThreads) {
    for (int i=0; i&lt;nScriptCheckThreads-1; i++)
        threadGroup.create_thread(&amp;ThreadScriptCheck);
}

// Start the lightweight task scheduler thread
CScheduler::Function serviceLoop = boost::bind(&amp;CScheduler::serviceQueue, &amp;scheduler);
threadGroup.create_thread(boost::bind(&amp;TraceThread&lt;CScheduler::Function&gt;, "scheduler", serviceLoop));
</code></pre>

<bitcoind><bitcoin-core>
Understanding Multi-threading (boost) in bitcoin initialization
Paarth
# Understanding Multi-threading (boost) in bitcoin initialization

(Asked by: username_0 on 2015-12-23)

I am not familiar with boost in c++. Can any expert please explain in simple terms what is happening in the below code ? Is it like the logic in ThreadScriptCheck is scheduled to be run in background or how? 
This is taken from bitcoin initialization (src/init.cpp). The classes are defined in src/scheduler.h
AppInit2():
// Assume value of nScriptCheckThreads is 4

if (nScriptCheckThreads) {
    for (int i=0; i, "scheduler", serviceLoop));

**Tags:** bitcoind, bitcoin-core


---

## 1 answers

### Answer by username_1 on 2015-12-24. Score: 2

The `boost::thread_group` class doesn't do very much. It's basically just a container of threads. It doesn't really have any scheduling or dispatch functions. That this thread is part of a thread group doesn't really matter.

The `CScheduler` code allows a thread to run a task at a particular time. It requires a thread to run the various tasks that it schedules. This code creates a thread to runs those tasks for this `CScheduler` and adds it to the thread group.

> **Comments:**
> **username_0**: Thank you so much for your explanation. Also, can you point me out what those tasks (being run in `CScheduler`) exactly are? The LogPrintf above this code snippet in `src/init.cpp` mentions that it runs script verification, but I am unable to find relevant logic in the source code.  
> **username_0**: Also, `GenerateBitcoins` in `sec/miner.cpp` also creates a threadgroup with which it associates the `BitcoinMiner`. As per what I understand from your explanation, that this is just associating the thread in a group. But then how is the miner run in background?  
> **username_1**: The bitcoin daemon no longer runs a miner in the background, or at all for that matter. The scheduler is used for periodic tasks such as making outbound connections.  
> **username_0**: But where is the scheduler for `BitcoinMiner` ? There is no scheduler logic used for running the thread group associated with mining. `miner.cpp` has logic only for thread group creation.  

---

