zoukankan      html  css  js  c++  java
  • multithread synchronization use mutex and semaphore

    #include <malloc.h>
    #include <pthread.h>
    #include <semaphore.h>
    struct job {
    /* Link field for linked list.
    struct job* next;
    */
    /* Other fields describing work to be done... */
    };
    /* A linked list of pending jobs.
    struct job* job_queue;
    */
    /* A mutex protecting job_queue. */
    pthread_mutex_t job_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
    /* A semaphore counting the number of jobs in the queue.
    sem_t job_queue_count;
    /* Perform one-time initialization of the job queue.
    */
    */
    void initialize_job_queue ()
    {
    /* The queue is initially empty. */
    job_queue = NULL;
    /* Initialize the semaphore which counts jobs in the queue.
    initial value should be zero. */
    sem_init (&job_queue_count, 0, 0);
    }
    /* Process queued jobs until the queue is empty.
    Its
    */
    void* thread_function (void* arg)
    {
    while (1) {
    struct job* next_job;
    sem_wait (&job_queue_count);
    /* Lock the mutex on the job queue. */
    pthread_mutex_lock (&job_queue_mutex);
    /* Because of the semaphore, we know the queue is not empty. Get
    the next available job. */
    next_job = job_queue;
    /* Remove this job from the list. */
    job_queue = job_queue->next;
    /* Unlock the mutex on the job queue because we’re done with the
    queue for now. */
    pthread_mutex_unlock (&job_queue_mutex);
    /* Carry out the work. */
    process_job (next_job);
    /* Clean up. */
    free (next_job);
    }
    return NULL;
    }
    void enqueue_job (/* Pass job-specific data here...*/)
    {
    struct job* new_job;
    /* Allocate a new job object. */
    new_job = (struct job*) malloc (sizeof (struct job));
    /* Set the other fields of the job struct here... */
    /* Lock the mutex on the job queue before accessing it.
    pthread_mutex_lock (&job_queue_mutex);
    /* Place the new job at the head of the queue. */
    new_job->next = job_queue;
    job_queue = new_job;
    */
    /* Post to the semaphore to indicate that another job is available.
    threads are blocked, waiting on the semaphore, one will become
    unblocked so it can process the job. */
    sem_post (&job_queue_count);
    If
    /* Unlock the job queue mutex. */
    pthread_mutex_unlock (&job_queue_mutex);
    }
  • 相关阅读:
    1.认识移动端 、前端工作流程 2019-2-13
    去掉标签默认样式属性 + visibility
    grid 布局:一般用于多行排版、单页排版、......(响应式布局)
    解决 display 和 transition 冲突的问题
    回到顶部效果
    文字溢出 生成 省略号
    【Python】协程实现生产者消费者模型
    【Python】0/1背包、动态规划
    【Python】使用super初始化超类
    【Python】考虑用生成器改写直接返回列表的函数
  • 原文地址:https://www.cnblogs.com/feika/p/3614513.html
Copyright © 2011-2022 走看看