semaphore

Semaphore solution to reader-writer: order between updating reader count and waiting or signaling on read/write binary semaphore?

↘锁芯ラ 提交于 2019-12-02 12:51:16
From Operating System Concepts In the solution to the first readers–writers problem, the reader processes share the following data structures: semaphore rw mutex = 1; semaphore mutex = 1; int read_count = 0; do { wait(rw_mutex); . . . /* writing is performed */ . . . signal(rw_mutex); } while (true); Figure 5.11 The structure of a writer process. do { wait(mutex); read count++; if (read_count == 1) wait(rw mutex); signal(mutex); . . . /* reading is performed */ . . . wait(mutex); read count--; if (read_count == 0) signal(rw_mutex); signal(mutex); } while (true); Figure 5.12 The structure of a

How can I get the UI thread to wait on a semaphore, but process additional dispatcher requests? (like what MessageBox.Show does natively)

扶醉桌前 提交于 2019-12-02 11:04:28
Normally, when the UI thread calls something like MessageBox.Show() , the current code execution doesn't continue until the user clicks OK, but the program will continue to run other code dispatched on the UI thread. In this question , I had a problem with too many delegates dispatched on the UI Thread being called at once. I wanted to pause at certain points before continuing execution. In my new Error handler, I use semaphores to make sure no more than one error is being handled at once. I dispatch a MessageBox to alert the user, and when they click "OK", I release the semaphore, allowing

C++11 Multithreading - The Readers-Writers Problem

早过忘川 提交于 2019-12-02 10:07:02
The reader and writer processes share the following data structures: semaphore rw_mutex = 1; semaphore mutex = 1; int read_count = 0; The semaphores mutex and rw_mutex are initialized to 1; read_count is initialized to 0. The semaphore rw_mutex is common to both reader and writer processes. The mutex semaphore is used to ensure mutual exclusion when the variable read count is updated. The read_count variable keeps track of how many processes are currently reading the object. The semaphore rw_mutex functions as a mutual exclusion semaphore for the writers. It is also used by the first or last

并发 信号量 Semaphore

独自空忆成欢 提交于 2019-12-02 08:51:14
what: Semaphore是用来控制同时访问特定资源的线程数量,他通过协调各个线程,以保证合理的使用公共资源。 why: 每个资源所能承受的并发负载是有限的。 how: Semaphore可以用于做流量控制,特别是公用资源有限的应用场景,比如数据库连接。假如有一个需求,要读取几万个文件的数据,因为都是IO密集型任务,我们可以启动几十个线程并发的读取,但是如果读到内存后,还需要存储到数据库中,而数据库的连接数只有10个,这时我们必须控制只有10个线程同时获取数据库连接保存数据,否则会报错无法获取数据库连接。这个时候,就可以使用Semaphore来做流量控制,如下所示。 public class SemaphoreTest { private static final int THREAD_COUNT=30; private static ExecutorService threadPool =Executors.newFixedThreadPool(THREAD_COUNT); private static Semaphore s = new Semaphore(10); public static void main(String[] args) { for (int i = 0; i < THREAD_COUNT; i++) { threadPool.execute(new

多线程学习(二)控制并发线程数的Semaphore

微笑、不失礼 提交于 2019-12-02 08:50:50
并发工具类(二)控制并发线程数的Semaphore 简介 Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源。很多年以来,我都觉得从字面上很难理解Semaphore所表达的含义,只能把它比作是控制流量的红绿灯,比如XX马路要限制流量,只允许同时有一百辆车在这条路上行使,其他的都必须在路口等待,所以前一百辆车会看到绿灯,可以开进这条马路,后面的车会看到红灯,不能驶入XX马路,但是如果前一百辆中有五辆车已经离开了XX马路,那么后面就允许有5辆车驶入马路,这个例子里说的车就是线程,驶入马路就表示线程在执行,离开马路就表示线程执行完成,看见红灯就表示线程被阻塞,不能执行。 应用场景 Semaphore可以用于做流量控制,特别公用资源有限的应用场景,比如数据库连接。假如有一个需求,要读取几万个文件的数据,因为都是IO密集型任务,我们可以启动几十个线程并发的读取,但是如果读到内存后,还需要存储到数据库中,而数据库的连接数只有10个,这时我们必须控制只有十个线程同时获取数据库连接保存数据,否则会报错无法获取数据库连接。这个时候,我们就可以使用Semaphore来做流控,代码如下: 01 public class SemaphoreTest { 02 03 private static final int THREAD_COUNT =

控制并发线程数的Semaphore

随声附和 提交于 2019-12-02 08:50:36
Semaphore被用于控制特定资源在同一个时间被访问的线程数量,它通过协调各个线程,以保证资源可以被合理的使用。 做个比喻,把Semaphore比作是控制流量的红绿灯,比如xx马路要现在流量,只允许同时有一百辆车在马路上行驶,其他的都必须在路口等待,所以前一百辆会看到绿灯,可以开进马路,后面的车会看到红灯,不能开进马路,但是如果前面一百辆车中有5辆已经离开了马路,那后面就允许有5辆车驶入马路,这里例子里说的车就是线程,驶入马路就代表线程正在执行,离开马路就表示线程执行完成,看到红灯就代表线程被阻塞,不能执行。 应用场景 Semaph可以用来做流量限制,特别是公共资源有限的应用场景,比如说数据库连接。 假如有一个需求,要读取几万个文件的数据,因为都是IO密集型人物,我们可以启动几十个线程并发的读取,但是如果读取到内存后,还需要储存到数据库,而数据库的连接数只有10个,这时候我们就必须控制只有10个线程同时获取到数据库连接,否则会抛出异常提示无法连接数据库。针对这种情况,我们就可以使用Semaphore来做流量控制。代码如下: package com.dreyer.javadoc.thread; import java.util.concurrent.*; /** * @description * @author: Dreyer * @date: 16/5/15 上午11:59 */

Is there a built-in semaphore structure that allows for waiting on more than one resource?

谁说胖子不能爱 提交于 2019-12-02 08:46:08
I would like to be able wait/post more than one resource at a time. Is there a built-in c structure and interface that allows for this? Currently I am using semaphore.h however this interface has the limitation that it can only request a single resource at a time. I could do something like this: for (int i = 0; i < resources; i++) sem_wait(my_sem); But this would be time consuming if resources is large and I would also need to add another lock before this so that I am guaranteed that the requester has priority over other threads that may be requesting resources. It would end up looking

Counting distinct words with Threads

独自空忆成欢 提交于 2019-12-02 07:42:48
The objective is to count distinct words from a file. UPDATE: Previous Code was successfully finished. Now I have to do the same but using threads (Oh man, I hate them...) and in addition I want to make it with semaphores for better flow. Code contains some extra stuff left out from previous attempts, I'm trying to figure out what can be used.. I can read one word at a time but mostly I get a "null" in the container. So until I get anything from the container all the time I can't test the Sorter class and so on... The new addition to the program is WordContainer class to store one word to pass

iOS: 线程中那些常见的锁

拟墨画扇 提交于 2019-12-02 06:13:15
一、介绍 在多线程开发中,锁的使用基本必不可少,主要是为了解决资源共享时出现争夺而导致数据不一致的问题,也就是线程安全问题。锁的种类很多,在实际开发中,需要根据情况选择性的选取使用,毕竟使用锁也是消耗CPU的。 本人虽然一直有使用多线程进行开发,但是对于锁的使用和理解并不是特别的深入,这不看到一篇挺mark的博客: https://www.jianshu.com/p/a236130bf7a2 ,在此基础上稍添加点东西转载过来(尊重原创),一是为了记录便于随时翻阅,而是为了写一遍加深印象,知识都是一个copy和attract的过程。 二、种类 1、互斥锁 概念:对共享数据进行锁定,保证同一时刻只能有一个线程去操作。 抢到锁的线程先执行,没有抢到锁的线程就会被挂起等待。 等锁用完后需要释放,然后其它等待的线程再去抢这个锁,那个线程抢到就让那个线程再执行。 具体哪个线程抢到这个锁是由cpu调度决定的。 常用: @synchronized:同步代码块 example:执行操作 /** *设置属性值 */ -(void)setMyTestString:(NSString *)myTestString{ @synchronized(self) { // todo something _myTestString = myTestString; } } example:创建单例 //注意

C semaphores: sem_wait throwing inexplicable error

蹲街弑〆低调 提交于 2019-12-02 05:39:56
I'm working on a problem which we have to use semaphores to solve. I have an array which contains two semaphores, gsem , and given certain conditions call sem_wait(&(gsem[me])) , which is supposed to waiting until that particular process is woken up. However, for some reason it gives me the error Bad file descriptor . I looked up sem_wait and the Open Group spec says this is not an error sem_wait can cause. This is making my whole program crazy and I have no idea why this is failing. EDIT: Offending code, as requested. 120 sem_wait(&mutex); 121 if (inside[opp] > 0 || waiting[opp] > 0) { 122