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);
    }
  • 相关阅读:
    修正了Flex Tree 控件在動態加載節點后 Scrollbar 沒有立即出現的問題
    「51单片机」收发一体超声波测距模块分析+代码
    「51单片机」RS232串口通信代码分析
    「51单片机」蓝牙从机基本使用方法
    「ZigBee模块」基础实验(2)按键
    「ZigBee模块」基础实验(1)点亮LED
    「51单片机」红外接收、发射模块代码
    溢出部分省略号总结
    【学习笔记】《JavaScript DOM 编程艺术》 ——总结
    读《IBatis in action 》 总结
  • 原文地址:https://www.cnblogs.com/feika/p/3614513.html
Copyright © 2011-2022 走看看