Skip to content

Commit d98c452

Browse files
Fix exit condition after G.E.
1 parent bbb98b8 commit d98c452

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

FindAFactor/_find_a_factor.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ struct Factorizer {
943943
auto ikit = smoothNumberKeys.begin();
944944
auto ivit = smoothNumberValues.begin();
945945
BigInteger result = 1U;
946+
batchMutex.lock();
946947
for (size_t i = 0U; i < smoothNumberKeys.size(); ++i) {
947948
dispatch.dispatch([this, i, ikit, ivit, &result]() -> bool {
948949
auto jkit = ikit;
@@ -988,12 +989,17 @@ struct Factorizer {
988989
return false;
989990
});
990991

992+
// If we all still dispatching items in the queue,
993+
// they probably won't have completed, but let
994+
// them take a turn with the mutex.
995+
batchMutex.unlock();
996+
991997
// Next outer-loop row (all dispatched at once).
992998
++ikit;
993999
++ivit;
9941000

9951001
// If this actually contends, we'll exit now.
996-
std::lock_guard<std::mutex> lock(batchMutex);
1002+
batchMutex.lock();
9971003
if (isIncomplete) {
9981004
break;
9991005
}
@@ -1002,10 +1008,12 @@ struct Factorizer {
10021008
if (result == 1U) {
10031009
// The result has not yet been found.
10041010
// A succesful item will dump the queue.
1011+
batchMutex.unlock();
10051012
dispatch.finish();
10061013
} else {
10071014
// The result has been found.
10081015
// If any work remains, dump it.
1016+
batchMutex.unlock();
10091017
dispatch.dump();
10101018
}
10111019

0 commit comments

Comments
 (0)