I’m encountering a concurrency problem in C++ that I’m struggling to find the best solution for. I have two threads: one is the main program, and the other is responsible for controlling the lights of my device. Here’s the code for my second thread:
void DroneService::setUpLightsMode(LightModes mode){
this->lightMode = mode;
k_work_submit(&work_queue);
}
void DroneService::workHandler(struct k_work* work){
DroneService* droneService = getInstance();
while(droneService->lightMode == Stop){
droneService->rightLed->syncFadeLed(100, 10, 3000);
k_msleep(1000);
droneService->rightLed->syncFadeLed(10, 100, 3000);
k_msleep(1000);
}
if (droneService->lightMode == Running && droneService->pastLightMode != Running){
LOG_DBG("*************Runningn");
droneService->pastLightMode = Running;
droneService->rightLed->syncFadeLed(10, 100, 500);
}
}
You can see that my main thread modifies the mode in which the second thread needs to work through the variable lightMode, but I need to be able to break the loop immediately when lightMode changes to the Running state. Currently, this does not happen because the while loop takes some seconds and only changes to the Running mode when the while loop finishes, can someone help me to find a good solution?
Thank you
I tried breaking the loop in each step of the while but is not fast enough
Rubén Méndez Vallés is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.