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, , );
}
/* 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的更多相关文章
- Mutex vs Semaphore
What are the differences between Mutex vs Semaphore? When to use mutex and when to use semaphore? Co ...
- mutex与semaphore的区别
网摘1:Mutex 的发音是 /mjuteks/ ,其含义为互斥(体),这个词是Mutual Exclude的缩写.Mutex在计算机中是互斥也就是排他持有的一种方式,和信号量-Semaphore有可 ...
- 内核必看: spinlock、 mutex 以及 semaphore
linux 内核的几种锁介绍 http://wenku.baidu.com/link?url=RdvuOpN3RPiC5aY0fKi2Xqw2MyTnpZwZbE07JriN7raJ_L6Ss8Ru1 ...
- 线程同步 –Mutex和Semaphore
上一篇介绍了同步事件EventWaitHandle,以及它的两个子类型AutoResetEvent和ManualResetEvent.下面接着介绍WaitHandle的另外两个子类型Mutex和Sem ...
- Mutex vs Semaphore vs Monitor vs SemaphoreSlim
C#开发者(面试者)都会遇到Mutex,Semaphore,Monitor,SemaphoreSlim这四个与锁相关的C#类型,本文期望以最简洁明了的方式阐述四种对象的区别. 线程安全 教条式理解 如 ...
- 线程同步之mutex和Semaphore
表示之前对semaphore信号量木有神码概念. 比较纳闷这玩意要干嘛,好吧继续stackflow: Mutex can be released only by thread that had acq ...
- 多线程同步与并发访问共享资源工具—Lock、Monitor、Mutex、Semaphore
“线程同步”的含义 当一个进程启动了多个线程时,如果需要控制这些线程的推进顺序(比如A线程必须等待B和C线程执行完毕之后才能继续执行),则称这些线程需要进行“线程同步(thread synchro ...
- 【转】深层次探讨mutex与semaphore之间的区别(下)
原文网址:http://blog.chinaunix.net/uid-23769728-id-3173282.html 这篇博文很长,虽然这是下篇,但还没结束,benchmark方面的东西正在进行中, ...
- java开发中的Mutex vs Semaphore
先看一下stackoverflow上是怎么说的吧 原文地址:http://stackoverflow.com/questions/771347/what-is-mutex-and-semaphore- ...
随机推荐
- activemq 一个不错的ppt
http://people.apache.org/~jstrachan/talks/ActiveMQ-Dublin07.pdf
- 【整理】C++虚函数及其继承、虚继承类大小
参考文章: http://blog.chinaunix.net/uid-25132162-id-1564955.html http://blog.csdn.net/haoel/article/deta ...
- Ajax解决缓存的5种方法
原文:http://www.ido321.com/129.html 1.在ajax发送请求前加上 anyAjaxObj.setRequestHeader(“If-Modified-Since”,”0″ ...
- Magento 头部的演示信息去除
进入后台: 系统-配置, 然后选择左栏的“设计”, 选择右栏的HTML头信息里面有个“Display Demo Store Notice”, 默认是yes,设置为“no”就可以了.
- Android 依赖注入 ButterKnife 基本使用
ButterKnife 是一个快速 Android View 注入框架,开发者是Jake Wharton,简单的来说,ButterKnife 是用注解的方式替代findViewById和setXXXL ...
- C++ 虚函数表与内存模型
1.虚函数 虚函数是c++实现多态的有力武器,声明虚函数只需在函数前加上virtual关键字,虚函数的定义不用加virtual关键字. 2.虚函数要点 (1) 静态成员函数不能声明为虚函数 可以这么理 ...
- Java常用知识点
1. java不支持默认参数,需要用重载来实现 2. java中要比较字符串是否相等,不能用等号,要用equals函数来比较内容 3. 尽量避免使用try catch来捕获异常,可以使用if语句判断以 ...
- Redis+MongoDB 最佳实践 做到读写分离 -摘自网络
方案1. (被否定) 加上Redis,做到MongoDB的读写分离,单一进程从MongoDB及时把任务同步到Redis中. 看起来很完美,但是上线后出现了各种各样的问题,列举一下: 1.Redis队列 ...
- POJ 3648-Wedding(2-SAT)
题面很邪恶啊... 一对新人请n-1对夫妻吃饭,人们坐在一张桌子的两侧,每一对互为夫妻关系的人必须坐在桌子的两侧.而且有些人两两之间会存在“通奸”关系,通奸关系不仅在男女之间,同性之间也有.新娘对面不 ...
- HDU5739-Fantasia(tarjan求割点)
题意:给一个无向图n个点1~n,m条边,sigma(i*zi)%(1e9+7).zi是这个图删掉i点之后的价值.一个图的价值是所有连通子图的价值之和,连通图的价值是每个点的乘积. 题解:讲道理这题不算 ...