Files
salmanoff/libspinscale/include/spinscale/asynchronousLoop.h
T

70 lines
1.3 KiB
C++
Raw Normal View History

2025-09-10 11:06:29 -04:00
#ifndef ASYNCHRONOUS_LOOP_H
#define ASYNCHRONOUS_LOOP_H
#include <atomic>
2025-12-27 16:21:22 -04:00
namespace sscl {
2025-09-10 11:06:29 -04:00
class AsynchronousLoop
{
public:
AsynchronousLoop(
2025-09-10 15:13:02 -04:00
const unsigned int nTotal,
unsigned int nSucceeded=0, unsigned int nFailed=0)
2025-09-10 11:06:29 -04:00
: nTotal(nTotal), nSucceeded(nSucceeded), nFailed(nFailed)
2025-09-10 11:07:42 -04:00
{}
2025-09-10 11:06:29 -04:00
2025-09-16 18:20:08 -04:00
AsynchronousLoop(const AsynchronousLoop& other)
: nTotal(other.nTotal),
nSucceeded(other.nSucceeded.load()), nFailed(other.nFailed.load())
{}
AsynchronousLoop& operator=(const AsynchronousLoop& other)
{
if (this != &other)
{
nTotal = other.nTotal;
nSucceeded.store(other.nSucceeded.load());
nFailed.store(other.nFailed.load());
}
return *this;
}
2025-09-10 11:06:29 -04:00
bool isComplete(void) const
{
return nSucceeded + nFailed == nTotal;
}
void incrementSuccessOrFailureDueTo(bool success)
{
if (success)
{ ++nSucceeded; }
else
{ ++nFailed; }
}
bool incrementSuccessOrFailureAndTestForCompletionDueTo(bool success)
{
incrementSuccessOrFailureDueTo(success);
return isComplete();
}
bool nTotalIsZero(void) const
{
return nTotal == 0;
}
void setRemainingIterationsToFailure()
{
nFailed.store(nTotal - nSucceeded.load());
}
2025-09-10 11:06:29 -04:00
public:
2025-09-16 18:20:08 -04:00
unsigned int nTotal;
2025-09-10 15:13:02 -04:00
std::atomic<unsigned int> nSucceeded, nFailed;
2025-09-10 11:06:29 -04:00
};
2025-12-27 16:21:22 -04:00
} // namespace sscl
2025-09-10 11:06:29 -04:00
2025-09-10 11:07:42 -04:00
#endif // ASYNCHRONOUS_LOOP_H