semaphore

Difference between Semaphore and Condition (ReentrantLock)

瘦欲@ 提交于 2020-01-21 03:52:44
问题 Does anyone know the differences between the methods acquire () and release () ( java.util.concurrent.Semaphore ) and await () and signal (new ReentrantLock().newCondition() ) . Can you expose a pseudo code for each of these methods? 回答1: Superficially the behavior of these method might look similar - acquire()/await() can make threads block in some cirsumstances and release()/signal() can unblock threads in some circumstances. However Semaphore and Condition serve different purposes: java

Java并发之CountDownLatch、CyclicBarrier、Semaphore使用实例

泪湿孤枕 提交于 2020-01-19 02:18:10
最近学习了Java并发编程中的CountDownLatch、CyclicBarrier、Semaphore,做个小总结。 这三个类都是java1.5中提供的一些非常有用的辅助类,用于帮助程序员实现并发编程。 CountDownLatch 背景 CountDownLatch是一个同步工具类,用来协调多个线程之间的同步,或者说起到线程之间的通信(而不是用作互斥的作用)。 CountDownLatch能够使一个线程在等待另外一些线程完成各自工作之后,再继续执行。使用一个计数器进行实现。计数器初始值为线程的数量。当每一个线程完成自己任务后,计数器的值就会减一。当计数器的值为0时,表示所有的线程都已经完成一些任务,然后在CountDownLatch上等待的线程就可以恢复执行接下来的任务。 countDownLatch是在java1.5被引入,跟它一起被引入的工具类还有CyclicBarrier、Semaphore、concurrentHashMap和BlockingQueue。 存在于java.util.cucurrent包下。 概念 countDownLatch这个类使一个线程等待其他线程各自执行完毕后再执行。 是通过一个计数器来实现的,计数器的初始值是线程的数量。每当一个线程执行完毕后,计数器的值就-1,当计数器的值为0时,表示所有线程都执行完毕,然后在闭锁上等待的线程就可以恢复工作了。

Ubuntu Semaphore: Segmentation fault (core dumped)

醉酒当歌 提交于 2020-01-16 21:55:50
问题 I am learning semaphores in C using Ubuntu right now. The professor just throw us this code and ask us to study it and observe. When I compiled I get a warning that ctime(&sem_buf.sem_ctime) returns an int , not a char * but nothing major. When I run it the output is just: Semaphore identifier: 0 Segmentation fault (core dumped) . I am very confused as of what went wrong and I have no idea what is going on in this code. Some help would be very much appreciated. Here is the code: #include <sys

Ubuntu Semaphore: Segmentation fault (core dumped)

狂风中的少年 提交于 2020-01-16 21:55:11
问题 I am learning semaphores in C using Ubuntu right now. The professor just throw us this code and ask us to study it and observe. When I compiled I get a warning that ctime(&sem_buf.sem_ctime) returns an int , not a char * but nothing major. When I run it the output is just: Semaphore identifier: 0 Segmentation fault (core dumped) . I am very confused as of what went wrong and I have no idea what is going on in this code. Some help would be very much appreciated. Here is the code: #include <sys

Semaphore实现原理

删除回忆录丶 提交于 2020-01-16 09:06:37
示例代码 public class TestSemaphore { public static Semaphore semaphore = new Semaphore ( 2 ) ; public static void main ( String [ ] args ) { System . out . println ( "main start" ) ; for ( int i = 0 ; i < 5 ; i ++ ) { new Thread ( new Taskes ( ) ) . start ( ) ; } System . out . println ( "main end" ) ; } } class Taskes implements Runnable { @Override public void run ( ) { try { TestSemaphore . semaphore . acquire ( ) ; LocalTime time = LocalTime . now ( ) ; System . out . println ( Thread . currentThread ( ) . getName ( ) + "任务开始--------" + time . getMinute ( ) + ":" + time . getSecond ( ) ) ;

better way to interrupt my python code when subprocess is running?

有些话、适合烂在心里 提交于 2020-01-15 06:53:10
问题 I recently answered a question here The question was: how to interrupt a python script when a subprocess is running (possibly windows-specific). I suggested to create a thread to run the subprocess on, and wait on the mainloop for CTRL+C / KeyboardInterrupt exception so the child process can be killed. It works, but bit of a hack because I did this: Basically, I make a non-cpu active (but polling) loop which waits 100ms then checks for the shared boolean done . If the subprocess exits, the

Critical Section in JavaScript or jQuery

ⅰ亾dé卋堺 提交于 2020-01-14 07:51:08
问题 I have a webpage, in which a certain Ajax event is triggered asynchronously. This Ajax section could be called once or more than once. I do not have control over the number of times this event is triggered, nor the timing. Also, there is a certain code in that Ajax section that should run as a critical section, meaning, when it is running, no other copy of that code should be running. Here is a pseudo code: Run JavaScript or jQuery code Enter critical section that is Ajax (when a certain

How can semaphores implemented on multi-core systems?

谁都会走 提交于 2020-01-14 07:11:29
问题 I can not understand how Posix allows any thread to unlock (post) on a semaphore. Let's consider following example: // sem1 and sem2 are a Posix semaphore, // properly initialized for single process use // at this point, sem2 is locked, sem1 is unlocked // x and y are global (non-atomic, non-volatile) integer variables // thread 1 - is executing right now rc = sem_wait(&sem1); // succeeded, semaphore is 0 now x = 42; y = 142; sem_post(&sem2); while (true); // thread 2. waits for sem2 to be

多线程(二)

谁都会走 提交于 2020-01-14 02:20:23
简易阻止:Sleep,阻止给定的时间周期;Join,等待另一个线程完成。 一旦被阻止,线程立刻放弃分配给他的CPU资源,并且ThreadState属性值改为WaitSleepJoin。 停止阻止的方式:(1)操作超时(2)通过Thread的Interrupt中断了(3)通过Thread的Abort放弃了. Thread.Sleep(Timeout.Infinite);//休眠直到中断 Thread.Sleep(TimeSpan.FromSeconds(1));//休眠一秒 Thread.Sleep(0);//暂时的放弃CPU,时间刚刚够其他时间片里的线程执行。 Join方法也接收一个使用毫秒或用TimeSpan类的超时参数,当Join超时时返回false,如果线程已终止,则返回true。(在阻止是Join保持信息汲取,sleep停止信息汲取)。 锁系统:lock,确保只有一个线程访问某资源,不能跨进程,速度快; Mutex, ,确保只有一个线程访问某资源,可以跨进程,速度中等; Semaphore,不超过制定数目的线程访问某资源,可以跨进程,速度中等。 任何对所有有关系的线程都可见的对象都可以作为同步对象,但要服从一个硬性规定:它必须是引用类型。也强烈建议同步对象最好私有在类里面(比如一个私有实例字段)防止无意间从外部锁定相同的对象。服从这些规则,同步对象可以兼对象和保护两种作用。

进程通信之IPC通信对象——信号灯

对着背影说爱祢 提交于 2020-01-13 13:58:08
进程间通过信号灯的通信。 信号灯的创建函数semget(): 信号灯的删除函数semctl(): 例子1:信号灯的创建和删除,通过semget函数创建,调用semctl函数删除。 #include"sys/types.h" #include"signal.h" #include"unistd.h" #include"stdio.h" #include"stdlib.h" #include"sys/sem.h" int main() { int semid; semid=semget(IPC_PRIVATE,3,0777); if(semid<0) { printf("creat semaphore failure\n"); return -1; } printf("creat semaphore success\n"); system("ipcs -s"); //delete semaphore semctl(semid,0,IPC_RMID,NULL); system("ipcs -s"); return 0; } 运行结果如图: 成功创建3个信号灯后,又成功删除了。 例子2:一般我们可以用全局变量来实现父子线程之间的同步,如下thread.c的内容:通过定义一个全局变量thread_inter来实现父子线程的同步问题。 #include<pthread.h> #include