#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. 《Python基础教程(第二版)》学习笔记 -> 第二章 列表和元组

    本章将引入一个新的概念:数据结构. 数据结构是通过某种方式阻止在一起的数据元素的集合,这些数据元素可以是数字或者字符,设置可以是其他数据结构. Python中,最基本的数据结构是序列(Sequence ...

  2. 使用appium做安卓手机web自动化测试 真机demo

    一:appium相关环境搭建过程略. 二:连接真机: 1.手机(andriod 4.2.2)连接电脑,打开USB调试模式. 2.运行cmd 输入 adb devices -l 查看UDID,如图: 3 ...

  3. flash player 版本对照

  4. js基础第六天

    获取节点属性 getAttribute("属性") 获取属性 setAttribute("属性","值"); 设置节点属性 删除某个属性 r ...

  5. C++设计模式——策略模式

    策略模式 在GOF的<设计模式:可复用面向对象软件的基础>一书中对策略模式是这样说的:定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.该模式使得算法可独立于使用它的客户而变化 ...

  6. jquery chart plugin

    jquery flot http://www.jqueryflottutorial.com/ jquery jqplot http://www.jqplot.com/ highcharts中文网 : ...

  7. 【noip2011】Mayan游戏

    题解: 刷了一天的noip啊 做了10题! 突然找回了做马拉松的感觉- - 我中午竟然放弃治疗去看视频 做到晚上累得都快挂了 用电脑放一些rock 把音乐当咖啡硬撑下来 但是还是没能刷3届 唉 显然速 ...

  8. [iOS基础控件 - bugs]

    1.不能呼出iOS模拟器键盘 Can't find keyplane that supports type 4 for keyboard iPhone-Portrait-NumberPad 解决:Ha ...

  9. [OC Foundation框架 - 8] NSArray排序

    1.派生 voidarrayNew() { NSArray*array = [NSArrayarrayWithObjects:",nil]; NSArray*array2 = [arraya ...

  10. C/C++中的变量作用域

    #include <iostream> using namespace std; int i = 1;int j = 2; int main(){     int i = 9;  //C/ ...