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() 信号量的更多相关文章

  1. 线程同步之信号量(sem_init,sem_post,sem_wait)

    信号量和互斥锁(mutex)的区别:互斥锁只允许一个线程进入临界区,而信号量允许多个线程同时进入临界区. 不多做解释,要使用信号量同步,需要包含头文件semaphore.h. 主要用到的函数: int ...

  2. linux c 线程相关函数

    线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程 一. pthread_creat ...

  3. JAVA线程同步 (三)信号量

    一个信号量有且仅有3种操作,且它们全部是原子的:初始化.增加和减少 增加可以为一个进程解除阻塞: 减少可以让一个进程进入阻塞. 信号量维护一个许可集,若有必要,会在获得许可之前阻塞每一个线程:     ...

  4. Python3学习之路~9.3 GIL、线程锁之Lock\Rlock\信号量、Event

    一 Python GIL(Global Interpreter Lock) 全局解释器锁 如果一个主机是单核,此时同时启动10个线程,由于CPU执行了上下文的切换,让我们宏观上看上去它们是并行的,但实 ...

  5. 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 ...

  6. 线程间同步之 semaphore(信号量)

    原文地址:http://www.cnblogs.com/yuqilin/archive/2011/10/16/2214429.html semaphore 可用于进程间同步也可用于同一个进程间的线程同 ...

  7. C#线程同步(5)- 信号量 Semaphore

    文章原始出处 http://xxinside.blogbus.com/logs/47617134.html 预备知识:C#线程同步(1)- 临界区&Lock,C#线程同步(2)- 临界区&am ...

  8. python theading线程开发与加锁、信号量、事件等详解

    线程有2种调用方式,如下: 直接调用 import threading import time def sayhi(num): #定义每个线程要运行的函数 print("running on ...

  9. day34 python学习 守护进程,线程,互斥锁,信号量,生产者消费者模型,

    六 守护线程 无论是进程还是线程,都遵循:守护xxx会等待主xxx运行完毕后被销毁 需要强调的是:运行完毕并非终止运行 #1.对主进程来说,运行完毕指的是主进程代码运行完毕 #2.对主线程来说,运行完 ...

随机推荐

  1. 简单的后台管理系统vue-cli3.0+element-ui

    前段时间在研究一个分发系统,发现vue-cli+element-ui好像是挺不错的,然后自己根据那个分发系统尝试搭建了一下 1.首先安装vue和vue-cli // 全局安装vue npm insta ...

  2. Integer IntegerCache源码

    先看一段测试结果: /*public static void main(String[] args) { Integer a = 128, b = 128; Integer c = 127, d = ...

  3. Maven配置浅析

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  4. Flume的安装配置

    flume是一个分布式.可靠.和高可用的海量日志采集.聚合和传输的系统.支持在日志系统中定制各类数据发送方,用于收集数据;同时,Flume提供对数据进行简单处理,并写到各种数据接受方(比如文本.HDF ...

  5. 开源 免费 java CMS - FreeCMS1.5-数据对象-info

    下载地址:http://code.google.com/p/freecms/ info 在信息页静态化时,系统会自动向模板文件传递currInfo对象,此对象存储当前信息.在使用信息相关标签时,标签会 ...

  6. Iometer教程

    Iometer Tutorial and Introduction http://www.itechstorm.com/iometer-tutorial-introduction

  7. FatSecret Platform API

    在现阶段饮食类的APP发展的非常迅猛,尤其在校园中,学生只需要凭借一个手机就能买到自己想要的食物,真正做到了足不出户.可是如果我们想独立完成一个app就需要有相应的数据支持,这里给大家介绍一个国外的开 ...

  8. ScriptableObject 对象化的运用

    http://www.cnblogs.com/oldman/articles/2409554.html using UnityEngine; using UnityEditor; using Syst ...

  9. 算法笔记_129:计数排序(Java)

    目录 1 问题描述 2 解决方案 2.1比较计数排序 2.2 分布计数排序   1 问题描述 给定一组数据,请使用计数排序,得到这组数据从小到大的排序序列. 2 解决方案 2.1比较计数排序 下面算法 ...

  10. Win8多平台引用配置

    之前移植过DLNA的库,这个库是C++写的,然后我们的项目是C#的.接着很郁闷的事情发生了,主项目引用一个C#的DLL,然后这个DLL引用这个C++/CX封装的库.如果有C++的源代码的话,做项目依赖 ...