线程相关函数(7)-sem_post(), sem_wait() 信号量
sem_t
sem_init
sem_wait
sem_trywait
sem_timedwait
sem_post
sem_destroy
生产者消费者实例:
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
#define NUM 5
int queue[NUM];
sem_t blank_number, product_number;
void *producer(void *arg)
{
int p = ;
while () {
sem_wait(&blank_number);
queue[p] = rand() % + ;
printf("Produce %d\n", queue[p]);
sem_post(&product_number);
p = (p+)%NUM;
sleep(rand()%);
}
}
void *consumer(void *arg)
{
int c = ;
while () {
sem_wait(&product_number);
printf("Consume %d\n", queue[c]);
queue[c] = ;
sem_post(&blank_number);
c = (c+)%NUM;
sleep(rand()%);
}
}
int main(int argc, char *argv[])
{
pthread_t pid, cid;
sem_init(&blank_number, , NUM);
sem_init(&product_number, , );
pthread_create(&pid, NULL, producer, NULL);
pthread_create(&cid, NULL, consumer, NULL);
pthread_join(pid, NULL);
pthread_join(cid, NULL);
sem_destroy(&blank_number);
sem_destroy(&product_number);
return ;
}
线程相关函数(7)-sem_post(), sem_wait() 信号量的更多相关文章
- 线程同步之信号量(sem_init,sem_post,sem_wait)
信号量和互斥锁(mutex)的区别:互斥锁只允许一个线程进入临界区,而信号量允许多个线程同时进入临界区. 不多做解释,要使用信号量同步,需要包含头文件semaphore.h. 主要用到的函数: int ...
- linux c 线程相关函数
线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程 一. pthread_creat ...
- JAVA线程同步 (三)信号量
一个信号量有且仅有3种操作,且它们全部是原子的:初始化.增加和减少 增加可以为一个进程解除阻塞: 减少可以让一个进程进入阻塞. 信号量维护一个许可集,若有必要,会在获得许可之前阻塞每一个线程: ...
- Python3学习之路~9.3 GIL、线程锁之Lock\Rlock\信号量、Event
一 Python GIL(Global Interpreter Lock) 全局解释器锁 如果一个主机是单核,此时同时启动10个线程,由于CPU执行了上下文的切换,让我们宏观上看上去它们是并行的,但实 ...
- POSIX semaphore: sem_open, sem_close, sem_post, sem_wait
http://www.cnblogs.com/BloodAndBone/archive/2011/01/18/1938552.html 一.Posix有名信号灯 1.posix有名信号灯函数 函数se ...
- 线程间同步之 semaphore(信号量)
原文地址:http://www.cnblogs.com/yuqilin/archive/2011/10/16/2214429.html semaphore 可用于进程间同步也可用于同一个进程间的线程同 ...
- C#线程同步(5)- 信号量 Semaphore
文章原始出处 http://xxinside.blogbus.com/logs/47617134.html 预备知识:C#线程同步(1)- 临界区&Lock,C#线程同步(2)- 临界区&am ...
- python theading线程开发与加锁、信号量、事件等详解
线程有2种调用方式,如下: 直接调用 import threading import time def sayhi(num): #定义每个线程要运行的函数 print("running on ...
- day34 python学习 守护进程,线程,互斥锁,信号量,生产者消费者模型,
六 守护线程 无论是进程还是线程,都遵循:守护xxx会等待主xxx运行完毕后被销毁 需要强调的是:运行完毕并非终止运行 #1.对主进程来说,运行完毕指的是主进程代码运行完毕 #2.对主线程来说,运行完 ...
随机推荐
- CentOS安装mysql*.rpm提示conflicts with file from package的解决的方法
CentOS 6.5下安装MySql 5.6 解压文件:tar xvf MySQL-5.6.19-1.linux_glibc2.5.x86_64.rpm-bundle.tar 释放出下面文件: MyS ...
- jenkins报:反向代理设置有误
1.如图所示: 2.点击更多信息,查看解决办法: https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+says+my+reverse+proxy+s ...
- Proxmark3介绍
Proxmark3介绍 Proxmark3是由Jonathan Westhues设计并且开发的开源硬件,其主要用RFID的嗅探.读取以及克隆等的操作. 其官方网站为:Jonathan Westhues ...
- Nodejs安装使用,以及不错的Nodejs或者JS资料整理
先按照这个教程来学习:Node.js教程 - 菜鸟教程网 在mac上使用brew安装了nodejs,中间还是用到了先下载到cache目录的方法. 但是后来发现这样按照的node,没有安装npm. 找到 ...
- zend studio 13.0.0 安装破解汉化
zend studio 13安装破解汉化步骤 官网原版下载 http://downloads.zend.com/studio-eclipse/13.0.0/ZendStudio-13.0.0-win3 ...
- 用php编写我的第一段代码:hello world
一.php环境的搭建 在编写php前,先搭建php环境,我选择了一站式安装软件WampServer,WampServer的下载地址:http://www.wampserver.com/ WampSer ...
- 教大家如何在word 2007中同时打出对齐上下标以及字母头上有波浪线(非编辑器)
教大家如何在word 2007中打出(非编辑器): 如果要在多个字符串上面加上划线,可以使用一下步骤 按下“Ctrl+F9”组合键,出现“{}”,在{}中输入“EQ \x\to(要加上划线的字符串)” ...
- QtGui.QFontDialog
The QtGui.QFontDialog is a dialog widget for selecting a font. #!/usr/bin/python # -*- coding: utf-8 ...
- synchronized探究
synchronized的加锁方式 synchronized的本质是给对象上锁,对象包括实例对象,也包括类对象.常见的加锁方式有下面几种写法:(1)在非static方法上加synchronized,例 ...
- Python 创建特殊元组tuple
创建1个元素的tuple (1,) 创建单元素tupletuple和list一样,可以包含 0 个.1个和任意多个元素.包含多个元素的 tuple,前面我们已经创建过了.包含 0 个元素的 tupl ...