semaphore

objective-c : @synchronized, how does it work?

浪尽此生 提交于 2019-11-29 06:21:02
问题 i have two methods -(void) a { @synchronized(self) { // critical section 1 } } -(void) b { @synchronized(self) { // critical section 2 } } now my question is if a thread is in critical section 1. will the critical section 2 be locked for other threads or other threads can access critical section 2. 回答1: Critical section 2 will be blocked to other threads, as well, since you're synchronizing on the same object ( self ). 来源: https://stackoverflow.com/questions/2810459/objective-c-synchronized

IOS semaphore_wait_trap on main thread causing hang in UI

半腔热情 提交于 2019-11-29 05:28:43
I have a long running function inside an asynchronous (serial) worker queue. I know that sometimes this function hangs inside a particular openCV call. For some reason this hang is also causing the main thread to hang. When pausing and entering debug mode I see that there is a call to semaphore_wait_trap() on the main thread (Queue) I can suspend the hanging thread (My worker queue) in debug mode and then this trap goes away and the GUI becomes responsive once again on the phone. After unpausing the worker thread the GUI is responsive for 1-2 seconds (I suspect until this thread is activated

[******] java多线程连续打印abc

大城市里の小女人 提交于 2019-11-29 02:38:35
题目描述 建立三个线程A、B、C,A线程打印10次字母A,B线程打印10次字母B,C线程打印10次字母C,但是要求三个线程同时运行,并且实现交替打印,即按照ABCABCABC的顺序打印。 5种方法 使用synchronized, wait和notifyAll 使用Lock->ReentrantLock 和 state标志 使用Lock->ReentrantLock 和Condition(await 、signal、signalAll) 使用Semaphore 使用AtomicInteger 扩展:采用join实现(一次性打印) 5.1 使用synchronized, wait和notifyAll public class ABC7 { private static Object o = new Object();//所对象 private static int state = 0;//控制顺序 private static int PRINT_NUMBER = 10;//打印次数 private static int THREAD_NUM = 3;//线程数量 static class ThreadGenetic implements Runnable { char name; int data; public ThreadGenetic(char name, int data){

Semaphores on Python

好久不见. 提交于 2019-11-29 01:41:08
I've started programming in Python a few weeks ago and was trying to use Semaphores to synchronize two simple threads, for learning purposes. Here is what I've got: import threading sem = threading.Semaphore() def fun1(): while True: sem.acquire() print(1) sem.release() def fun2(): while True: sem.acquire() print(2) sem.release() t = threading.Thread(target = fun1) t.start() t2 = threading.Thread(target = fun2) t2.start() But it keeps printing just 1's. How can I intercale the prints? It is working fine, its just that its printing too fast for you to see . Try putting a time.sleep() in both

.NET Reverse Semaphore?

爱⌒轻易说出口 提交于 2019-11-29 01:12:44
Perhaps it's too late at night, but I can't think of a nice way to do this. I've started a bunch of asynchronous downloads, and I want to wait until they all complete before the program terminates. This leads me to believe I should increment something when a download starts, and decrement it when it finishes. But then how do I wait until the count is 0 again? Semaphores sort of work in the opposite way in that you block when there are no resources available, not when they're all available (blocks when count is 0, rather than non-zero). Check out the CountdownLatch class in this magazine

Linux synchronization with FIFO waiting queue

﹥>﹥吖頭↗ 提交于 2019-11-28 22:04:12
Are there locks in Linux where the waiting queue is FIFO? This seems like such an obvious thing, and yet I just discovered that pthread mutexes aren't FIFO, and semaphores apparently aren't FIFO either (I'm working on kernel 2.4 (homework))... Does Linux have a lock with FIFO waiting queue, or is there an easy way to make one with existing mechanisms? Here is a way to create a simple queueing "ticket lock", built on pthreads primitives. It should give you some ideas: #include <pthread.h> typedef struct ticket_lock { pthread_cond_t cond; pthread_mutex_t mutex; unsigned long queue_head, queue

Shared semaphore between user and kernel spaces

余生颓废 提交于 2019-11-28 21:13:19
问题 Short version Is it possible to share a semaphore (or any other synchronization lock) between user space and kernel space? Named POSIX semaphores have kernel persistence, that's why I was wondering if it is possible to also create, and/or access them from kernel context. Searching the internet didn't help much due to the sea of information on normal usage of POSIX semaphores. Long version I am developing a unified interface to real-time systems in which I have some added book keeping to take

semaphore implementation

試著忘記壹切 提交于 2019-11-28 20:56:07
I am getting error in the following program. I want to demonstrate how two processes can share a variable using semaphore. Can anyone guide me? I am not able to debug the errors... #include<stdlib.h> #include<stdio.h> #include<unistd.h> #include<sys/ipc.h> #include<sys/sem.h> #include<semaphore.h> int main() { int pid,mutex=1; int semid; /* semid of semaphore set */ key_t key = 1234; /* key to pass to semget() */ int nsems = 1; /* nsems to pass to semget() */ semid=semget(key,nsems,IPC_CREAT|0666); if (semid<0) { perror("Semaphore creation failed "); } if ((pid = fork()) < 0) { perror("fork");

Javascript semaphore / test-and-set / lock?

自古美人都是妖i 提交于 2019-11-28 20:03:40
Is there such a thing as an atomic test-and-set, semaphore, or lock in Javascript? I have javascript invoking async background processes via a custom protocol (the background process literally runs in a separate process, unrelated to the browser). I believe I'm running into a race condition; the background process returns between my test and my set, screwing things up on the javascript side. I need a test-and-set operation to make it a real semaphore. Here's the javascript code that attempts to detect background processes and queue them up: Call = function () { var isRunning = true, queue = []

python3 multiprocessing Semaphore

烈酒焚心 提交于 2019-11-28 18:48:19
http://mirror.hust.edu.cn/gnu/glibc/ glibc-2.29.tar.gz glibc: bits/semaphore.h #if __WORDSIZE == 64 # define __SIZEOF_SEM_T 32 #else # define __SIZEOF_SEM_T 16 #endif /* Value returned if `sem_open' failed. */ #define SEM_FAILED ((sem_t *) 0) typedef union { char __size[__SIZEOF_SEM_T]; long int __align; } sem_t; PyModule_AddObject(module, "SemLock", (PyObject*)&_PyMp_SemLockType); Python-3.6.8/Modules/_multiprocessing/multiprocessing.h typedef sem_t *SEM_HANDLE; Python-3.6.8/Modules/_multiprocessing/semaphore.c enum { RECURSIVE_MUTEX, SEMAPHORE }; typedef struct { PyObject_HEAD SEM_HANDLE