Hello,
I am wondering if (possibly how) it would be possible to utilize sf::Event / (sf::Lock || sf::Mutex) to make threads process data at specific intervals. Why? Because it seems to me that sf::RenderWindow::waitEvent() and the mutexes are able to hlt (0xF4) the thread. This means ~0 processor usage - less power, less heat, more for other purposes.
I came up with two possible methods, but none work:
1. I am unable to lock a thread from itself.
#include <SFML/System.hpp>
#include <iostream>
sf::Mutex mutex;
bool runThread = true;
signed int dataToProcess = 0;
void processData()
{
while (runThread)
{
mutex.lock();
mutex.lock(); // Here I would like the thread to lock itself, and thus wait for any external mutex.unlock to complete one iteration.
dataToProcess *= 10;
std::cout << dataToProcess << std::endl;
}
}
int main()
{
sf::Thread dataProcessor(processData);
dataProcessor.launch();
while (runThread)
{
int numberAddition = 0;
std::cin >> numberAddition;
if (numberAddition == 0)
{
runThread = false;
dataProcess.wait();
return dataToProcess;
}
dataToProcess += numberAddition;
mutex.unlock(); // The thread is now iterated through once.
}
return 0;
}
2. I am unable to wait for specific events in another thread.
- Thread_1 & Thread_2 are running in parallel.
- I want to make Thread_2 wait for a boolean to change. No polling.
- If said boolean is changed, iterate over Thread_2's instructions.
- When iteration is complete, wait for a change in boolean again.
This can however be solved by simply launching the thread from the location where it is needed, but what if I have local variables in the thread function that need to be re-initialized (extra processing power)? What if the locals need to contain data from previous iterations (A class can solve this tho...)? What if the function contains smaller parts that we can loop through? Using shared booleans in this context would be messy IMHO.
I'll rephrase my question; can we control a thread - that runs continuously - whilst utilizing the anti-power hogging delight of mutexes/locks or waitEvents? (Or is there something superior?)