mirror of
https://github.com/eosswedenorg/antelope-keygen
synced 2026-06-18 04:00:03 +02:00
common/include/eoskeygen/key_search.hpp: add max and count variables to the class.
This is done so that m_max can be set to zero from another method. stopping a find() operation. (for threaded operations)
This commit is contained in:
parent
3cc6a2530e
commit
2a528e0eb0
3 changed files with 31 additions and 31 deletions
|
|
@ -31,39 +31,33 @@
|
|||
|
||||
namespace eoskeygen {
|
||||
|
||||
// Max keys to search for,
|
||||
std::size_t g_max;
|
||||
|
||||
// How many keys we have found so far.
|
||||
std::size_t g_count;
|
||||
|
||||
// Mutex guard for g_count.
|
||||
// Mutex guard for m_count.
|
||||
std::mutex g_count_mtx;
|
||||
|
||||
// Thread process.
|
||||
void KeySearch::_thr_proc() {
|
||||
|
||||
void KeySearch::_thr_proc()
|
||||
{
|
||||
struct libeosio::ec_keypair pair;
|
||||
|
||||
while (g_count < g_max) {
|
||||
while (m_count < m_max) {
|
||||
struct result res;
|
||||
|
||||
libeosio::ec_generate_key(&pair);
|
||||
if (_contains_word(&pair, res)) {
|
||||
|
||||
// Guard output with mutex, so we don't get
|
||||
// interrupted mid write and can write to g_count and res safely.
|
||||
// interrupted mid write and can write to m_count and res safely.
|
||||
const std::lock_guard<std::mutex> lock(g_count_mtx);
|
||||
|
||||
// It is possible g_count was updated by another thread
|
||||
// It is possible m_count was updated by another thread
|
||||
// after we checked it in the while loop.
|
||||
// So while we have the lock, we need to check it again.
|
||||
if (g_count >= g_max) {
|
||||
if (m_count >= m_max) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Update count and call result function.
|
||||
g_count++;
|
||||
m_count++;
|
||||
m_callback->onResult(&pair, res);
|
||||
}
|
||||
}
|
||||
|
|
@ -79,16 +73,12 @@ size_t KeySearch::max_threads()
|
|||
return std::thread::hardware_concurrency();
|
||||
}
|
||||
|
||||
void KeySearch::_search_mt(size_t n)
|
||||
void KeySearch::_search_mt()
|
||||
{
|
||||
std::vector<std::thread> t;
|
||||
|
||||
t.resize(m_threads - 1);
|
||||
|
||||
// Setup global variables for the threads.
|
||||
g_max = n;
|
||||
g_count = 0;
|
||||
|
||||
// Launch them.
|
||||
for(std::size_t i = 0; i < t.size(); i++) {
|
||||
t[i] = std::thread(&KeySearch::_thr_proc, this);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue