#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. The type or namespace name '****' could not be found (are you missing a using directive or an assembly reference

    错误的提升内容:

  2. java实现多继承

    方法:  接口+组合 理由:通过接口实现客户端的使用时多继承类的多类, 通过组合实现客户端内部类的实现相关功能(而且有些共用的功能可以不总是多次实现). public interface GMapOb ...

  3. 使用struts的同步令牌避免form的重复提交

    struts1避免重复提交 一.使用方法 1.  假如你要提交的页面为toSubmit.jsp: 2.  在打开toSubmit.jsp的Action1中加入:saveToken(request),例 ...

  4. devexpress 中Grid 的使用:为零不显示

    如果要让为0的列不显示: this.gridColumn_FAmount.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric; ...

  5. 《Oracle Database 12c DBA指南》第二章 - 安装Oracle和创建数据库(2.2 安装数据库软件)

    当前关于12c的中文资料比较少,本人将关于DBA的一部分官方文档翻译为中文,很多地方为了帮助中国网友看懂文章,没有按照原文句式翻译,翻译不足之处难免,望多多指正. 2.2 安装数据库软件 这部分简短讲 ...

  6. leetcode@ [84/85] Largest Rectangle in Histogram & Maximal Rectangle

    https://leetcode.com/problems/largest-rectangle-in-histogram/ https://leetcode.com/problems/maximal- ...

  7. BNUOJ-26580 Software Bugs KMP匹配,维护

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26580 题意:给一个模式串,然后m个匹配串,要求删掉匹配串中的所有存在的模式串,使得余下的 ...

  8. 2-SAT模板

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAABcAAAAXCAYAAADgKtSgAAAFCUlEQVR42n2VeVCUdRzGf3n0T/80ld

  9. ubuntu源码安装R语言

    下载后解压完,进入开始配置: ./configure --enable-R-shlib 报错: configure: error: con--with-readline=yes (default) a ...

  10. ios开发-确定/自适应textView的高度

    昨天在做学院客户端的时候,随手clean了下项目. 不过xcode又闹脾气了,textview里面的字体大小居然在真机运行的时候普遍小了2号.. 这下蛋疼了.应该我项目里面textview的frame ...