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. 织梦(DEDE)CMS V5.3 覆盖任意变量导致远程包含漏洞

    漏洞版本: 织梦(DEDE)CMS V5.3 漏洞描述: 织梦内容管理系统,最强大的中文开源CMS网站管理项目,使用PHP+MySQL架构. 在文件include/common.inc.php中: f ...

  2. Python 爬虫(1)基础知识和简单爬虫

    Python上手很容易,免费开源,跨平台不受限制,面向对象,框架和库很丰富. Python :Monty Python's Flying Circus (Python的名字来源,和蟒蛇其实无关). 通 ...

  3. [Functional Programming] Unbox types with foldMap

    Previously we have seen how to use Concat with reduce: ), Sum(), Sum()] .reduce((acc, x) => acc.c ...

  4. [Functional Programming] Pointy Functor Factory

    A pointed functor is a functor with an of method class IO { // The value we take for IO is always a ...

  5. C# 实现对微博短网址的重定向还原

    新浪微博中,为了节省输入字数,通过短网址对发布微博中链接进行重定向.我们可以通过代码实现对短网址进行还原,代码如下: private string GetOrignalLink(string link ...

  6. Python编程-基础知识-python项目包和文件的管理以及如何引用相对路径的包和模块

    目录 结构: core |____ __init__.py |____ basic |____ __init__.py |____ database           |____ __init__. ...

  7. 微信小程序 - 贝塞尔曲线(购物车效果)

    转载来源于:https://segmentfault.com/a/1190000011710786 简化了一下,发出来吧 示例源码:点击下载

  8. Git 提示fatal: remote origin already exists

    Git 提示fatal: remote origin already exists 错误解决办法 最后找到解决办法如下: 1.先删除远程 Git 仓库 $ git remote rm origin 2 ...

  9. web前端开发文档

  10. Nginx如何保留真实IP和获取前端IP

    原理: squid,varnish以及nginx等,在做反向代理的时候,因为要代替客户端去访问服务器,所以,当请求包经过反向代理后,在代理服务器这里这个IP数据包的IP包头做了修改,最终后端web服务 ...