I'm working on a multi-threaded project. I am using CMake to compile. I have one file/function that sets a bool to true every so often
#include <chrono>
void mainloop_click(int *cpm, bool *click, bool *end) {
auto start_time = std::chrono::system_clock::now();
while (!*end) {
*click = false;
while (std::chrono::duration<double>(std::chrono::system_clock::now() - start_time).count() < (60.0 / (double) *cpm));
*click = true;
start_time = std::chrono::system_clock::now();
}
}
My test function that is having problems is
void counter(int *count, bool *click, bool *end) {
while (!*end) {
if (*click) { // TODO Fix Release testing error. This always evaluates to false in release
(*count)++;
while (*click) {}
}
}
}
The basic outline of my test for this is:
- Start mainloop_click in its own thread
- Start counter in its own thread, passing the same click pointer.
- Test if the counter found as many clicks as would be expected for whatever the speed was set to (by
cpm
) after a set period of time.
As far as I can tell, in Debug mode, the compiler actually has the if statement evaluating in the executable, but not when compiled as a Release executable, it automatically evaluates the bool, click
, as false (since that's what it is before the thread starts), and doesn't actually check it, but "knows" it's false. Is there anyway to make the Release not do this? I have added print statements, and know that the third line in the test ( With the // TODO
) is where the problem occurs.