Hi Ondrej,

"Conditional variables" are one of the most powerful mechanisms for 
synchronization of pthreads.

Below is the code for the usage of condition variables for a typical 
Producer-Consumer problem. You may google it for complete understanding.

pthread_cond_t cond_queue_empty, cond_queue_full;
pthread_mutex_t task_queue_cond_lock;
int task_available;
/* other data structures here */
main() {
   /* declarations and initializations */
   task_available = 0;
   pthread_init();
   pthread_cond_init(&cond_queue_empty, NULL);
   pthread_cond_init(&cond_queue_full, NULL);
   pthread_mutex_init(&task_queue_cond_lock, NULL);
   /* create and join producer and consumer threads */
}


void *producer(void *producer_thread_data) {
   int inserted;
   while (!done()) {
       create_task();
       pthread_mutex_lock(&task_queue_cond_lock);
       while (task_available == 1)
           pthread_cond_wait(&cond_queue_empty,
               &task_queue_cond_lock);
       insert_into_queue();
       task_available = 1;
       pthread_cond_signal(&cond_queue_full);
       pthread_mutex_unlock(&task_queue_cond_lock);
   }
}

void *consumer(void *consumer_thread_data) {
   while (!done()) {
   pthread_mutex_lock(&task_queue_cond_lock);
   while (task_available == 0)
       pthread_cond_wait(&cond_queue_full,
           &task_queue_cond_lock);
       my_task = extract_from_queue();
       task_available = 0;
       pthread_cond_signal(&cond_queue_empty);
       pthread_mutex_unlock(&task_queue_cond_lock);
       process_task(my_task);
   }
}


By the way, are you using POSIX threads?

~ Gopal Sukumar.

Ondrej Pindroch wrote:
Hi

I have been studying how to use threads. I have successful made one 
application, with two threads. I have tied to make something like semaphore. 
Now I need to make application with 3 or 4 threads 1. will do capture, 2. 
preview, 3. resize and 4. display. I have no idea how to synchronize this all. 
I know that it should be something with semaphores. Could some one send me some 
example created for davinci, or other hints.

Thanks

Ondrej Pindroch
SoftHard Technology ltd.



________________________________

_______________________________________________
Davinci-linux-open-source mailing list
Davinci-linux-open-source@linux.davincidsp.com<mailto:Davinci-linux-open-source@linux.davincidsp.com>
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source


________________________________
http://www.mindtree.com/email/disclaimer.html
_______________________________________________
Davinci-linux-open-source mailing list
Davinci-linux-open-source@linux.davincidsp.com
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source

Reply via email to