semaphore

How can I implement a thread-safe list wrapper in Delphi?

拈花ヽ惹草 提交于 2019-12-01 16:37:37
问题 I have a list wrapper that maintains two Tstringlists and a TClassList I need this to be thread safe, such that: Concurrent writes are not allowed (wait state of some sort should be entered) Reading while writing (or vice versa) is not allowed (wait state of some sort should be entered) Concurrent reads are allowed Any ideas on how I can do this? My instinct tells me it needs more than just a critical section, perhaps a semaphore or "usage counter", perhaps one of these in conjunction with a

fork(), problems with multiple children

淺唱寂寞╮ 提交于 2019-12-01 14:52:21
I edited a little bit : for ( ii = 0; ii < nbEnfants; ++ii) { switch (fork()){ case -1 : { printf("\n\nSoucis avec fork() !!! \n\n"); exit(0); }; case 0 : { EEcrireMp(ii); }break; default : { tabPidEnfants[ii] = p; usleep(50000); ELireMp(nbSect, nbEnfants,tabPidEnfants); }; } } My problem : i get to many child, like a bomb of children spawning. How can i stop those child ? the break should stop it no ? Thanks So, when you fork a process, the new process is an identical copy of the parent, so when your child continues from the if ((pid = fork()) == 0) ... , it will continue out into the for

Semaphores and locks in MATLAB

眉间皱痕 提交于 2019-12-01 14:05:10
问题 I am working on a MATLAB project where I would like to have two instances of MATLAB running in parallel and sharing data. I will call these instances MAT_1 and MAT_2 . More specifically, the architecture of the system is: MAT_1 processes images sequentially, reading them one by one using imread , and outputs the result for each image using imwrite . MAT_2 reads the images output by MAT_1 using imread and outputs its result somewhere else. One of the problems I think I need to address is to

Java并发编程:CountDownLatch与CyclicBarrier和Semaphore的实例详解

时间秒杀一切 提交于 2019-12-01 12:56:57
Java并发编程:CountDownLatch与CyclicBarrier和Semaphore的实例详解 在 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

四、线程的并发工具类

[亡魂溺海] 提交于 2019-12-01 12:50:15
线程的并发工具类 一、CountDownLatch 【1】CountDownLatch是什么? CountDownLatch,英文翻译为倒计时锁存器,是一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。 闭锁可以延迟线程的进度直到其到达终止状态,闭锁可以用来确保某些活动直到其他活动都完成才继续执行: 确保某个计算在其需要的所有资源都被初始化之后才继续执行; 确保某个服务在其依赖的所有其他服务都已经启动之后才启动; 等待直到某个操作所有参与者都准备就绪再继续执行; CountDownLatch有一个正数计数器,countDown()方法对计数器做减操作,await()方法等待计数器达到0。所有await的线程都会阻塞直到计数器为0或者等待线程中断或者超时。 闭锁(倒计时锁)主要用来保证完成某个任务的先决条件满足。是一个同步工具类,用来协调多个线程之间的同步。这个工具通常用来控制线程等待,它可以让某一个线程等待直到倒计时结束,再开始执行。 【2】CountDownLatch的两种典型 用法 ① 某一线程在开始运行前等待n个线程执行完毕。 将 CountDownLatch 的计数器初始化为n :new CountDownLatch(n),每当一个任务线程执行完毕,就将计数器减1 countdownlatch.countDown(),当计数器的值变为0时

Java 的信号灯

淺唱寂寞╮ 提交于 2019-12-01 12:38:49
Semaphore可以维护当前访问自身的线程个数,并提供了同步机制。使用Semaphore可以控制同时访问资源的线程个数,例如,实现一个文件允许的并发访问数。 Semaphore实现的功能就类似厕所有5个坑,假如有十个人要上厕所,那么同时能有多少个人去上厕所呢?同时只能有5个人能够占用,当5个人中的任何一个人让开后,其中在等待的另外5个人中又有一个可以占用了。 另外等待的5个人中可以是随机获得优先机会,也可以是按照先来后到的顺序获得机会,这取决于构造Semaphore对象时传入的参数选项。 单个信号量的Semaphore对象可以实现互斥锁的功能,并且可以是由一个线程获得了“锁”,再由另一个线程释放“锁”,这可应用于死锁恢复的一些场合。 来源: https://www.cnblogs.com/Yanss/p/11686162.html

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

大兔子大兔子 提交于 2019-12-01 12:14:04
If I do not use sem_destroy at the last of my programs, what implications it may cause? Can some serious problems occur? 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, an unnamed semaphore must be initialized using sem_init(3). It can then be operated on using sem_post(3) and sem_wait(3). When the semaphore is no longer required, and before the memory in which it is located is deallocated, the semaphore should be destroyed using sem_destroy(3). so you should call sem_destroy when

pthread and semaphore not working for me in osx maverick 10.9

只谈情不闲聊 提交于 2019-12-01 11:11:29
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(&countMutex); pthread_exit( NULL ); } void LaunchThread() { const int kNumThreads = 10; pthread_t tids

Can I implement blocking queue using Semaphore in Java?

核能气质少年 提交于 2019-12-01 11:09:27
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 public BlockingQueue(int limit) { this.limit = limit; this.slots = new Semaphore(limit); // initial

iPhone: How can I implement semaphore?

末鹿安然 提交于 2019-12-01 10:57:26
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. 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)destroy; /* .. and the rest of the interface you wrap and make public here .. */ @end but it's likely that you