semaphore

Lock, mutex, semaphore… what's the difference?

亡梦爱人 提交于 2019-12-17 04:36:14
问题 I've heard these words related to concurrent programming, but what's the difference between them? 回答1: A lock allows only one thread to enter the part that's locked and the lock is not shared with any other processes. A mutex is the same as a lock but it can be system wide (shared by multiple processes). A semaphore does the same as a mutex but allows x number of threads to enter, this can be used for example to limit the number of cpu, io or ram intensive tasks running at the same time. For

Java并发编程:CountDownLatch、CyclicBarrier和 Semaphore

那年仲夏 提交于 2019-12-17 01:31:07
原文出处: 海子 在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法。 以下是本文目录大纲: 一.CountDownLatch用法 二.CyclicBarrier用法 三.Semaphore用法 一.CountDownLatch用法 CountDownLatch类位于java.util.concurrent包下,利用它可以实现类似计数器的功能。比如有一个任务A,它要等待其他4个任务执行完毕之后才能执行,此时就可以利用CountDownLatch来实现这种功能了。 CountDownLatch类只提供了一个构造器: public CountDownLatch(int count) { }; //参数count为计数值 然后下面这3个方法是CountDownLatch类中最重要的方法: public void await() throws InterruptedException { }; //调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行 public boolean await(long timeout, TimeUnit unit) throws InterruptedException { }; /

Semaphore使用场景和详解

痴心易碎 提交于 2019-12-17 00:03:49
Semaphore 信号量主要用于两个目的,一个是用于多个共享资源的互斥使用,另一个用于并发线程数量的控制。 下面我们模拟高并发多个资源对多个资源,假设有3个车位,有六台车子抢占车位如何操作,代码如下: import java . util . concurrent . Semaphore ; import java . util . concurrent . TimeUnit ; public class SemaphoreDemo { public static void main ( String [ ] args ) { //有3个车位当占满时,semaphore为0,当走了一个车时, // semaphore.release()则加1释放出来一个车位 Semaphore semaphore = new Semaphore ( 3 ) ; //型号灯,值是伸缩的,用满了0,空出来加,模拟3个停车位 for ( int i = 1 ; i <= 6 ; i ++ ) { //模拟6部汽车 new Thread ( ( ) - > { try { semaphore . acquire ( ) ; //占到车位 System . out . println ( Thread . currentThread ( ) . getName ( ) + "\t 抢占到车位" ) ;

perl 信号量 Semaphore

大兔子大兔子 提交于 2019-12-16 12:40:57
信号量作为锁使用事例。 #!/usr/bin/perl # use strict; use IPC::Semaphore; use IPC::SysV qw(IPC_PRIVATE S_IRUSR S_IWUSR IPC_CREAT IPC_NOWAIT SEM_UNDO); our $sem = IPC::Semaphore->new(IPC_PRIVATE, 1, S_IRUSR | S_IWUSR | IPC_CREAT); $sem->setval(0,1); my $pid = fork(); die "fork failed" unless defined $pid; if ($pid > 0) { $sem->op(0,-1,SEM_UNDO); my $i = 10; while($i > 0){ print "parent process i= $i\n"; $i--; } $sem->op(0,1,SEM_UNDO); } elsif ($pid == 0){ $sem->op(0, -1, SEM_UNDO); my $i = 10; while($i > 0) { print "child process i = $i\n"; $i--; } } $sem->remove() if defined $sem; -> # ./testsem.pl parent

CountDownLatch、Semaphore等4大并发工具类详解

青春壹個敷衍的年華 提交于 2019-12-14 07:18:31
Java并发工具包 1.并发工具类 提供了比synchronized更加高级的各种同步结构:包括CountDownLatch、CyclicBarrier、Semaphore等,可以实现更加丰富的多线程操作。 2.并发容器 提供各种线程安全的容器:最常见的ConcurrentHashMap、有序的ConcurrentSkipListMap,实现线程安全的动态数组CopyOnWriteArrayList等。 3.并发队列 各种BlockingQueue的实现:常用的ArrayBlockingQueue、SynchorousQueue或针对特定场景的PriorityBlockingQueue。 4.Executor框架 可以创建各种不同类型的线程池,调度任务运行等,绝大部分情况下,不再需要自己从头实现线程池和任务调度器。 Java常用的并发容器 1.ConcurrentHashMap 经常使用的并发容器,JDK 1.7和1.8的底层数据结构发生了变化(后续文章会详解),这里可以建议学习顺序如下:从Java7 HashMap -> Java7 ConcurrentHashMap -> Java8 HashMap -> Java8 ConcurrentHashMap,这样可以更好的掌握这个并发容器,毕竟都是从HashMap进化而来。 2.ConcurrentSkipListMap 在乎顺序

Queuing tasks in asp.net core

孤街浪徒 提交于 2019-12-14 03:52:52
问题 E.g. of functionality There is 20 users and they clicked send button almost in one time, so methods stacking in queue and first user message is sent and response received, after second third and so on. Users wont chat with other people but with device which response is pretty fast So I am trying to queue Task which sends Message. I found code samples that uses Task queuing as shown in Example 1 and Example 2 . Example 1 public class SerialQueue { readonly object _locker = new object();

Uart dma receive interrupt stops receiving data after several minutes

删除回忆录丶 提交于 2019-12-14 03:16:10
问题 I have a project that I have used stm32f746g discovery board. It receives data with fixed size from Uart sequentially and to inform application about each data receive completed, dma callback is used (HAL_UART_RxCpltCallback function). It works fine at the beginning but after several minutes of running, the dma callback stops to be called, and as a result, the specified parameter value doesn't get updated. Because the parameter is used in another thread too (actually a rtos defined timer), I

Java多线程进阶—— J.U.C之synchronizer框架:Semaphore

此生再无相见时 提交于 2019-12-14 00:28:53
一、Semaphore简介 Semaphore ,又名信号量,这个类的作用有点类似于“许可证”。有时,我们因为一些原因需要控制同时访问共享资源的最大线程数量,比如出于系统性能的考虑需要限流,或者共享资源是稀缺资源,我们需要有一种办法能够协调各个线程,以保证合理的使用公共资源。 Semaphore维护了一个许可集,其实就是一定数量的“许可证”。 当有线程想要访问共享资源时,需要先获取(acquire)的许可;如果许可不够了,线程需要一直等待,直到许可可用。当线程使用完共享资源后,可以归还(release)许可,以供其它需要的线程使用。 另外,Semaphore支持公平/非公平策略,这和ReentrantLock类似,后面讲Semaphore原理时会看到,它们的实现本身就是类似的。 二、Semaphore示例 我们来看下Oracle官方给出的示例: class Pool { private static final int MAX_AVAILABLE = 100; // 可同时访问资源的最大线程数 private final Semaphore available = new Semaphore(MAX_AVAILABLE, true); protected Object[] items = new Object[MAX_AVAILABLE]; //共享资源 protected

What are the practical uses of semaphores?

巧了我就是萌 提交于 2019-12-14 00:18:41
问题 Nonbinary ones.. I have never encountered a problem that required me to use a semaphore instead of mutex. So is this mostly theoretical construct, or real sw like Office, Firefox have places where they use it? If so what are the common use patterns for semaphores? 回答1: I think you already know what a semaphore is and you are just wondering how it is used on "real software". "real sw like Office, Firefox have places where they use it?" Yes, "real software" use semaphores a lot, it is not just

SemaphoreSlim and async/await

独自空忆成欢 提交于 2019-12-13 21:48:45
问题 This works: int _counter; readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1); async void Button_Click(object sender, RoutedEventArgs e) { if (_semaphore.Wait(0)) { Title = $"{ ++_counter}"; await Task.Delay(1000); // simulate work Title = $"{ --_counter}"; _semaphore.Release(); } } After first click further buttons clicks are ignored until work is finished. Tittle can be 1 or 0 . And this doesn't work void Button_Click(object sender, RoutedEventArgs e) { if (_semaphore.Wait(0)) {