c - How to stop and continue a pthread? -


i'm coding in c (actually in ooc compiled c).

how instruct thread wait @ particular checkpoint until other thread tells continue?

i'm using tight loop in thread , polling variable i'm changing main thread, think not performing, right? besides that, when tight loop in thread, should include sleep in loop avoid consuming cpu power looping?

it seems looking pthread_cond_wait , related functions.

here quote manpage example:

consider 2 shared variables x , y, protected mutex mut, , condition variable cond signaled whenever x becomes greater y.

          int x,y;           pthread_mutex_t mut = pthread_mutex_initializer;           pthread_cond_t cond = pthread_cond_initializer; 

waiting until x greater y performed follows:

          pthread_mutex_lock(&mut);           while (x <= y) {                   pthread_cond_wait(&cond, &mut);           }           /* operate on x , y */           pthread_mutex_unlock(&mut); 

modifications on x , y may cause x become greater y should signal condition if needed:

          pthread_mutex_lock(&mut);           /* modify x , y */           if (x > y) pthread_cond_broadcast(&cond);           pthread_mutex_unlock(&mut); 

in end asking for: thread calling pthread_cond_wait(&cond) blocked until other thread (your main thread example) calls pthread_cond_broadcast(&cond). during time in blocked state , doesn't consume cpu cycles.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -