i have a multithread application such that I connect sensors, (number of sensors is user depended) these sensors collect data and send them to my program via TCP. I take these data from TCP and do some complex calculations. After that I write my results to a shared memory space(for each sensor result i have different memory space.) For this purpose, I create 3 different class(for TCP, calculation, writing shared memory). And each class task run in parallel(task1 is TCP, task2 is calculation, task3 writing). Each task must be in while(1), because i want do each threads run until user stop program completely. If i have one sensor, i create one TCP object, one calculation object and one object for writing to shared memory, and i create 3 different thread for one TCP, one calculation and one writing. If i have 2 sensors have 2 objects for each and 2 threads for each and so on.
My problem is, because of using while loop, my cpu usage is always 100%. if i have 1 or 2 sensors, i have 3 or 6 different threads and everything works fine. But if i have much more sensors, for example 5, my program start to freeze. How do i prevent from freezing?
My code is basically;
void classTCP::threadTCP(){
while(1){
pthread_mutex_lock(&mutex1);
pthread_cond_wait(&cond1, &mutex1);
//..do_something
pthread_mutex_unlock(&mutex1);
}
}
void classCalculation::threadCalculation(){
while(1){
pthread_mutex_lock(&mutex2);
pthread_cond_wait(&cond2, &mutex2);
//..do_something
pthread_mutex_unlock(&mutex2);
}
}
void class3::thread3(){
while(1){
pthread_mutex_lock(&mutex3);
pthread_cond_wait(&cond3, &mutex3);
//..do_something
pthread_mutex_unlock(&mutex3);
}
}
int main()
{
while(1)
{
pthread_cond_broadcast(&cond1);
pthread_cond_broadcast(&cond2);
pthread_cond_broadcast(&cond3);
//...
}
}
pthread_cond_waitmust only be ever used inwhileloop, e.g.while(!cond) pthread_cond_wait;, lookup spurious wakeups. - Maxim Egorushkin