#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, , );
}
/* Process queued jobs until the queue is empty.
Its
*/
void* thread_function (void* arg)
{
while () {
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);
}

multithread synchronization use mutex and semaphore的更多相关文章

  1. Mutex vs Semaphore

    What are the differences between Mutex vs Semaphore? When to use mutex and when to use semaphore? Co ...

  2. mutex与semaphore的区别

    网摘1:Mutex 的发音是 /mjuteks/ ,其含义为互斥(体),这个词是Mutual Exclude的缩写.Mutex在计算机中是互斥也就是排他持有的一种方式,和信号量-Semaphore有可 ...

  3. 内核必看: spinlock、 mutex 以及 semaphore

    linux 内核的几种锁介绍 http://wenku.baidu.com/link?url=RdvuOpN3RPiC5aY0fKi2Xqw2MyTnpZwZbE07JriN7raJ_L6Ss8Ru1 ...

  4. 线程同步 –Mutex和Semaphore

    上一篇介绍了同步事件EventWaitHandle,以及它的两个子类型AutoResetEvent和ManualResetEvent.下面接着介绍WaitHandle的另外两个子类型Mutex和Sem ...

  5. Mutex vs Semaphore vs Monitor vs SemaphoreSlim

    C#开发者(面试者)都会遇到Mutex,Semaphore,Monitor,SemaphoreSlim这四个与锁相关的C#类型,本文期望以最简洁明了的方式阐述四种对象的区别. 线程安全 教条式理解 如 ...

  6. 线程同步之mutex和Semaphore

    表示之前对semaphore信号量木有神码概念. 比较纳闷这玩意要干嘛,好吧继续stackflow: Mutex can be released only by thread that had acq ...

  7. 多线程同步与并发访问共享资源工具—Lock、Monitor、Mutex、Semaphore

    “线程同步”的含义   当一个进程启动了多个线程时,如果需要控制这些线程的推进顺序(比如A线程必须等待B和C线程执行完毕之后才能继续执行),则称这些线程需要进行“线程同步(thread synchro ...

  8. 【转】深层次探讨mutex与semaphore之间的区别(下)

    原文网址:http://blog.chinaunix.net/uid-23769728-id-3173282.html 这篇博文很长,虽然这是下篇,但还没结束,benchmark方面的东西正在进行中, ...

  9. java开发中的Mutex vs Semaphore

    先看一下stackoverflow上是怎么说的吧 原文地址:http://stackoverflow.com/questions/771347/what-is-mutex-and-semaphore- ...

随机推荐

  1. 刑事案件的构成要素 zt

    论刑事案件的构成要素   马忠红 2013-03-22 14:05:33 来源:<中国人民公安大学学报:社会科学版>(京)2012年5期 [内容提要]刑事案件是由诸多要素构成的一个系 统. ...

  2. 【JMeter】Jmeter引入第三方jar包

    Jmeter做remoteService,里面用到一个实体:clickEntity,是在一个第三方jar包定义的:com.bj58.opt.ad_logparser-0.0.18-SNAPSHOT.j ...

  3. 【JMeter】Jmeter-完成一个http压力测试

    一 新建一个jmeter项目 1 进入/jmeter/bin路径,双击jmeter.bat,在win环境下启动jmeter. 2 点击"编辑->添加->TreadUsers-&g ...

  4. HDFS体系结构:(Distributed File System)

    分布式系统的大概图 服务器越来越多,客户端对服务器的管理就会越来越复杂,客户端如果是我们用户,就要去记住大量的ip. 对用户而言访问透明的就是分布式文件系统. 分布式文件系统最大的特点:数据存储在多台 ...

  5. Excel中输入√背景显示蓝色,输入×背景显示红色

    实现效果,如下图所示: 步骤:以office2013为例 1.点击"开始->条件格式" 2.点击"突出显示单元格规则->等于" 3.设置对应的规则, ...

  6. HDU-4716 A Computer Graphics Problem 水题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4716 直接搞.. //STATUS:C++_AC_0MS_288KB #include <fun ...

  7. HDU-4675 GCD of Sequence 数学

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4675 题意:给一个大小为N的数列a[i],然后一个数M以及一个数K,要你求得一个数列b[i],其中b[ ...

  8. HDU 3488--Tour(KM or 费用流)

    因为每个点只能经过一次 所以考虑拆点 这题有坑,有重边.. KM算法 把一个点拆成入点和出点 入点在X部,出点在Y步. 如果u,v之间有路径,就在X部的u点连接Y部的v点 求完美匹配. 当完美匹配的时 ...

  9. DELPHI 数学函数+字符处理函数

    System单元 Trunc(1234.5678);{1234} Trunc(-1234.5678);{-1234} Round(1234.5678);{1235} Round(-1234.5678) ...

  10. svn访问权限控制

    [customer:/]qa = rwreadonly = ryinqixian = r@haowu_partner_dev = r@admin = rw[customer:/branches/v1. ...