semaphore

How to wait until buffered channel (semaphore) is empty?

早过忘川 提交于 2019-11-28 04:21:15
问题 I have a slice of integers, which are manipulated concurrently: ints := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} I'm using a buffered channel as semaphore in order to have a an upper bound of concurrently running go routines: sem := make(chan struct{}, 2) for _, i := range ints { // acquire semaphore sem <- struct{}{} // start long running go routine go func(id int, sem chan struct{}) { // do something // release semaphore <- sem }(i, sem) } The code above works pretty well until the last or last

Semaphore vs. Monitors - what's the difference?

我的未来我决定 提交于 2019-11-28 02:31:11
What are the major differences between a Monitor and a Semaphore ? A Monitor is an object designed to be accessed from multiple threads. The member functions or methods of a monitor object will enforce mutual exclusion, so only one thread may be performing any action on the object at a given time. If one thread is currently executing a member function of the object then any other thread that tries to call a member function of that object will have to wait until the first has finished. A Semaphore is a lower-level object. You might well use a semaphore to implement a monitor. A semaphore

Python asyncio.semaphore in async-await function

冷暖自知 提交于 2019-11-28 02:04:35
I am trying to teach myself Python's async functionality. To do so I have built an async web scraper. I would like to limit the total number of connections I have open at once to be a good citizen on servers. I know that semaphore's are a good solution, and the asyncio library has a semaphore class built in. My issue is that Python complains when using yield from in an async function as you are combining yield and await syntax. Below is the exact syntax I am using... import asyncio import aiohttp sema = asyncio.BoundedSemaphore(5) async def get_page_text(url): with (yield from sema): try: resp

sem_getvalue() dysfunctionality in Mac OS X - C++

我是研究僧i 提交于 2019-11-28 02:01:57
I'm trying to implement synchronized use of a shared memory for bunch of threads in Mac OS X by semaphores. (I just overlook the fact that Mac users have got lots of issues with initializing a semaphore and destroying it...,which can be fixed by sem_open() and sem_unlink()):D But apparently for getting semaphore's current value there's nothing but sem_getvalue() which hasn't been implemented in mac os x. Any suggestion for someone that doesn't have linux OS running and should upload his homework in hours??!:) Thanks I think you are asking, "how can I work around the absence of sem_getvalue()

Is there any way to “wait here…” in code - just like an empty loop?

早过忘川 提交于 2019-11-28 01:30:18
问题 Consider this code: [self otherStuff]; // "wait here..." until something finishes while(!self.someFlag){} [self moreStuff]; Note that this all happens ON THE SAME THREAD - we do not want to go to another thread. otherStuff could do things like connect to the cloud, get input from the user, etc. so it would take a lot of time and could follow many possible paths. otherStuff would set self.someFlag to true, when otherStuff is finally finished. This works perfectly and there's no problem with it

How to protect a global variable shared by isr and regular function?

∥☆過路亽.° 提交于 2019-11-28 00:51:16
问题 Let's say I have function 1 and an isr routine , both share and update the same flag without any lock between them. the system is single threaded. the while will be a 3 arm assembly instructions, which means it is not atomic operation, is it ok to share a global variable between non isr and isr functions without any lock or protection? function 1: while (flag == false); flag = false; isr routine: do something flag=true I don't remember there is a linux kernel mechanism for locking between

How to initialise a binary semaphore in C

怎甘沉沦 提交于 2019-11-28 00:14:02
问题 In the man page it appears that even if you initialise a semaphore to a value of one: sem_init(&mySem, 0, 1); It could still be incremented to a value greater than 1 with multiple calls to sem_post(&mySem); But in this code example the comment seems to think differently: sem_init(&mutex, 0, 1); /* initialize mutex to 1 - binary semaphore */ Is it possible to initialise a strictly binary semaphore in C? Note: The reason for doing this instead of using a mutex in this case is the sem_post and

计算机操作系统 - 进程管理

牧云@^-^@ 提交于 2019-11-27 23:54:11
进程与线程 1. 进程 2. 线程 3. 区别 进程状态的切换 进程调度算法 1. 批处理系统 2. 交互式系统 3. 实时系统 进程同步 1. 临界区 2. 同步与互斥 3. 信号量 4. 管程 经典同步问题 1. 读者-写者问题 2. 哲学家进餐问题 进程通信 1. 管道 2. FIFO 3. 消息队列 4. 信号量 5. 共享存储 6. 套接字 进程与线程 1. 进程 进程是资源分配的基本单位。 进程控制块 (Process Control Block, PCB) 描述进程的基本信息和运行状态,所谓的创建进程和撤销进程,都是指对 PCB 的操作。 下图显示了 4 个程序创建了 4 个进程,这 4 个进程可以并发地执行。 2. 线程 线程是独立调度的基本单位。 一个进程中可以有多个线程,它们共享进程资源。 QQ 和浏览器是两个进程,浏览器进程里面有很多线程,例如 HTTP 请求线程、事件响应线程、渲染线程等等,线程的并发执行使得在浏览器中点击一个新链接从而发起 HTTP 请求时,浏览器还可以响应用户的其它事件。 3. 区别 Ⅰ 拥有资源 进程是资源分配的基本单位,但是线程不拥有资源,线程可以访问隶属进程的资源。 Ⅱ 调度 线程是独立调度的基本单位,在同一进程中,线程的切换不会引起进程切换,从一个进程中的线程切换到另一个进程中的线程时,会引起进程切换。 Ⅲ 系统开销

Program using Semaphores runs fine on Linux…unexpected results on Mac osX

痴心易碎 提交于 2019-11-27 23:06:40
I wrote a simple program solving the Readers-Writers problem using semaphores. It runs perfectly on Linux os, but when I run it on my Mac osX I get unexpected results and I can't figure out why. My Program: #include <semaphore.h> #include <sys/types.h> #include <stdio.h> #include <pthread.h> #include <unistd.h> void* function1(void* val); void* function2(void* val); // shared values volatile int X; volatile int Y; // declare semaphores sem_t s1; sem_t s2; main() { void* status; pthread_t thread1; pthread_t thread2; srand(time(NULL)); // initialize semaphores to zero sem_init(&s1, 0, 0); sem

GCD的使用总结

为君一笑 提交于 2019-11-27 21:40:50
什么是多线程? 计算机在运行一段程序的时候,会把该程序的CPU命令列配置到内存中,然后按照顺序一个一个执行命令列,这样1个CPU执行的CPU命令列为一条无分叉路径就是线程。 而有多条这样的执行指令列的路径存在时即为多线程。 iOS实现多线程有4种方法: pthreads NSThread GCD NSOperation & NSOperationQueuef 这里我们主要讲GCD 一、Dispatch Queue和线程的关系 什么是Dispatch Queue? 如其名称,是执行处理的等待队列。当我们通过dispatch_async等函数把Block加入Dispatch Queue后,Dispatch Queue按照追加的顺序(FIFO)执行处理。 通过Dispatch Queue执行处理 Dispatch Queue的种类 Serial Dispatch Queue(串行队列) ——等待现在执行中处理结束再加入队列 Concurrent Dispatch Queue(并发队列) ——不等待现在执行中处理结束,直接加入队列 Serial Dispatch Queue Concurrent Dispatch Queue 用代码说明: Serial Dispatch Queue dispatch_queue_t serial_queue = dispatch_queue_create(