semaphore

What may happen if I don't destroy a semaphore? [closed]

一笑奈何 提交于 2019-12-01 10:22:19
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . If I do not use sem_destroy at the last of my programs, what implications it may cause? Can some serious problems occur? 回答1: It is operating system specific. On Linux, read sem_overview(7); actually you are in an unspecified case. However, the documentation says Before being used

pthread and semaphore not working for me in osx maverick 10.9

五迷三道 提交于 2019-12-01 09:01:42
问题 I have the following simple program involving pthread and semaphore. I am in osx Maverck 10.9. I use a makefile to compile the program (rather than xcode). I use c++11. #include <pthread.h> #include <semaphore.h> #include <cassert> #include <iostream> #define ASSERT(a) if(!(a)) abort using namespace std; sem_t countMutex; int myCount=0; void *doThread(void *data) { int *pi = reinterpret_cast<int *>(data); sem_wait(&countMutex); for(int i =0 ;i < 100; ++i) { myCount += 1; } sem_post(

Can I implement blocking queue using Semaphore in Java?

蹲街弑〆低调 提交于 2019-12-01 08:50:36
问题 I wonder if it is possible to use Semaphore to implement blocking queue? In the below codes, I use one Semaphore to protect the critical section, and two more Semaphore objects to track the number of empty slots and filled objects. public class BlockingQueue { private List<Object> queue = new LinkedList<Object>(); private int limit; private Semaphore slots; // semaphore for empty slots private Semaphore objs; // semaphore for filled slots private Semaphore mutex; // for the critical section

Use queue and semaphore for concurrency and property wrapper?

不想你离开。 提交于 2019-12-01 08:34:05
问题 I'm trying to create a thread-safe property wrapper. I could only think of GCD queues and semaphores as being the most Swifty and reliable way. Are semaphore's just more performant (if that's true), or is there another reason to use one over the other for concurrency? Below are two variants of atomic property wrappers: @propertyWrapper struct Atomic<Value> { private var value: Value private let queue = DispatchQueue(label: "Atomic serial queue") var wrappedValue: Value { get { queue.sync {

iPhone: How can I implement semaphore?

柔情痞子 提交于 2019-12-01 07:34:01
问题 Can somebody explain how I can implement semaphore in Objective-C? I did a lot of google searching on the topic, but I haven't found anything understandable. 回答1: C apis can be found in sys/semaphore.h . use these in your objc wrapper/implementation. this basic example is the first result when 'sem_trywait example' is googled. it shows you how you can use these apis. then a minimal interface would take this form: @interface MONSemaphore : NSObject { sem_t semaphore; } - (int)close; - (int

issue with Threadsafe singleton with semaphore

橙三吉。 提交于 2019-12-01 06:22:39
I have written a simple singleton application. The following is my sample main class // ThreadsafeSingletonUsingSemaphore.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <conio.h> #include "MySingleton.h" using namespace std; int i =0; #define THREADCOUNT 100 DWORD WINAPI ThreadProc(LPVOID lParam); HANDLE g_semaphore = NULL; int _tmain(int argc, _TCHAR* argv[]) { g_semaphore = CreateSemaphore(NULL,1,1,_T("TreadOne")); HANDLE hThread[THREADCOUNT]; DWORD aThreadID; for(int iCount = 0; iCount < THREADCOUNT ; iCount++) { hThread[iCount

sem_post, signal handlers, and undefined behavior

纵饮孤独 提交于 2019-12-01 05:20:55
Does this use of sem_post() in a signal handler rely on undefined behavior? /* * excerpted from the 2017-09-15 Linux man page for sem_wait(3) * http://man7.org/linux/man-pages/man3/sem_wait.3.html */ ... sem_t sem; ... static void handler(int sig) { write(STDOUT_FILENO, "sem_post() from handler\n", 24); if (sem_post(&sem) == -1) { write(STDERR_FILENO, "sem_post() failed\n", 18); _exit(EXIT_FAILURE); } } The semaphore sem has static storage duration. While the call to sem_post () is async-signal-safe, the POSIX.1-2008 treatment of signal actions seems to disallow referencing that semaphore

使用Semaphore限制资源并发访问的线程数

限于喜欢 提交于 2019-12-01 05:11:16
从JDK 1.5之后,在java.util.concurrent包下引入了好多的处理多线程的工具类,本文将介绍用来控制资源同时访问个数的Semaphore工具类, 然后采用Semaphore给出一个泊车的实例,最后给出 Semaphore 和 CountDownLatch 的几点比较。 Semaphore工具类介绍 Semaphore类描述 /** * A counting semaphore. Conceptually, a semaphore maintains a set of * permits. Each {@link #acquire} blocks if necessary until a permit is * available, and then takes it. Each {@link #release} adds a permit, * potentially releasing a blocking acquirer. * However, no actual permit objects are used; the <tt>Semaphore</tt> just * keeps a count of the number available and acts accordingly. * * <p>Semaphores are often used

iOS 十种线程锁

我们两清 提交于 2019-12-01 04:34:11
锁 是什么意思? 我们在使用多线程的时候多个线程可能会访问同一块资源,这样就很容易引发数据错乱和数据安全等问题,这时候就需要我们保证每次只有一个线程访问这一块资源,锁 应运而生。 这里顺便提一下,上锁的两种方式trylock和lock使用场景: 当前线程锁失败,也可以继续其它任务,用 trylock 合适 当前线程只有锁成功后,才会做一些有意义的工作,那就 lock,没必要轮询 trylock 注:以下大部分锁都会提供trylock接口,不再作解释 性能图 <准备操作> 测试代码 #define RHTICK NSDate *startTime = [NSDate date]; #define RHTOCK NSLog(@"==========Time: %f", -[startTime timeIntervalSinceNow]); NSUInteger count = 1000*10000;//执行一千万次 RHTICK for(int i=0; i<count; i++) { 加锁 解锁 } RHTOCK 注:测试中执行时间会波动,所以我取的平均值. 一、OSSpinLock (自旋锁) 测试中效率最高的锁, 不过经YYKit作者确认, OSSpinLock已经不再线程安全,OSSpinLock有潜在的优先级反转问题. 不再安全的 OSSpinLock ; 0.097348s

issue with Threadsafe singleton with semaphore

巧了我就是萌 提交于 2019-12-01 04:32:38
问题 I have written a simple singleton application. The following is my sample main class // ThreadsafeSingletonUsingSemaphore.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <conio.h> #include "MySingleton.h" using namespace std; int i =0; #define THREADCOUNT 100 DWORD WINAPI ThreadProc(LPVOID lParam); HANDLE g_semaphore = NULL; int _tmain(int argc, _TCHAR* argv[]) { g_semaphore = CreateSemaphore(NULL,1,1,_T("TreadOne")); HANDLE