0

I'm creating boost threads inside a function with

while(trueNonceQueue.empty() && block.nNonce < std::numeric_limits<uint64_t>::max()){
    if ( block.nNonce % 100000 == 0 )
    {
        cout << block.nNonce << endl;
    }
    boost::thread t(CheckNonce, block);
    t.detach();
    block.nNonce++;
}
uint64 trueNonce;
while (trueNonceQueue.pop(trueNonce))
        block.nNonce = trueNonce;

trueNonceQueue was created with boost::lockfree::queue<uint64> trueNonceQueue(128); in the global scope.

This is the function being threaded

void CheckNonce(CBlock block){
    if(block.CheckBlockSilently()){
        while (!trueNonceQueue.push(block.nNonce))
            ;
    }
}

I noticed that after it crashed, my swap had grown marginally which never happens unless if I use poor technique like this after leaking memory; otherwise, my memory usage stays frequently below 2 gigs. I'm running cinnamon on ubuntu desktop with chrome and a few other small programs open. I was not using the computer at the time this was running.

The segfault occurred after the 949900000th iteration. How can this be corrected?


CheckNonce execution time

I added the same modulus to CheckNonce to see if there was any lag. So far, there is none.

I will update if the detached threads start to lag behind the spawning while.

4

1 回答 1

1

您应该改用线程池。这意味着生成足够多的线程来完成工作而不会发生过度争用(例如,您可能会在 N 核机器上生成 N-2 个线程,但如果某些工作可能会阻塞 I/O,则可能会更多)。

Boost 中并不完全有一个线程池,但是您需要构建一个线程池。在这里查看一些想法:boost::threadpool::pool vs.boost::thread_group

或者您可以使用像这样的更现成的解决方案(虽然它有点过时并且可能没有维护,不确定):http ://threadpool.sourceforge.net/

然后想法是产生 N 个线程,然后在每个任务的循环中,只需将任务“发布”到线程池,下一个可用的工作线程将在线程池中获取它。

通过这样做,您将避免许多问题,例如线程堆栈空间不足,避免低效的资源争用(查找“雷声从众问题”),并且您将能够轻松调整使用多核的侵略性在任何系统上。

于 2013-12-21T10:52:59.243 回答