# Thread Synchronization

##

    extern int job_count;
    extern pthread_cond_t job_cv;
    extern struct job_req *create_job();
    extern pthread_mutex_t job_lock;

    void producer_thread() {
      struct job_req *curr_job;
      for (;;) {
        curr_job == create_job();
        pthread_mutex_lock(&job_lock);
        job_enqueue(curr_job);
        job_count++;
        pthread_cond_signal(&job_cv);
        pthread_mutex_unlock(&job_lock);
      }
    }

    extern struct job_req *job_dequeue();

    void consumer_thread() {
      struct job_req *curr_job;
      for (;;) {
        pthread_mutex_lock(job_lock);
        while (job_count == 0)
          pthread_cond_wait (&job_cv, &job_lock);
        curr_job = job_dequeue();
        job_count--;
        pthread_mutex_unlock(&job_lock);
        process_job(curr_job);
      }
    }

Previous slide | Next slide | Back to first slide | View graphic version