semaphore

How to properly use PHP5 semaphores?

旧时模样 提交于 2019-11-28 08:44:22
问题 I have this function that tries to read some values from cache. But if value does not exists it should call alternative source API and save new value into the cache. However, server is very overloaded and almost each time when value does not exists more then one requests are created (a lot of API calls) and each of them will store new vale into cache. However, what I want is to be able to call API many times, but only one process/request to be able to store it in cache: function fetch_cache(

Interprocess semaphores sometimes not working as expected

坚强是说给别人听的谎言 提交于 2019-11-28 08:17:03
问题 I have the following C code, where variables prefixed by sm are shared by two processes proc1 and proc2 . Therefore the semaphores are also shared. This code is called repeatedly. So if I say previous value, that means value of a previous iteration. I notice in my program that proc1 sometimes passes sem_wait( sem_f2l ) without proc2 executing sem_post( sem_f2l ) . This I notice because sm_value_proc1 and sm_value_proc2 are supposed to be of same value in my program, which they are, as also

Guaranteed semaphore order?

风流意气都作罢 提交于 2019-11-28 07:28:32
问题 The documentation for the .NET Semaphore class states that: There is no guaranteed order, such as FIFO or LIFO, in which blocked threads enter the semaphore. In this case, if I want a guaranteed order (either FIFO or LIFO), what are my options? Is this something that just isn't easily possible? Would I have to write my own Semaphore? I assume that would be pretty advanced? Thanks, Steve 回答1: See this: The FifoSemaphore works exactly like a normal Semaphore but also guarantees that tokens are

How to create semaphores in shared memory in C?

霸气de小男生 提交于 2019-11-28 07:21:51
问题 My task is to create two different C files and then use the semaphores for process synchronization (I run both C files simultaneously). My main concern is: if I want to access the semaphores in both the processes (executables of the C files), I need to create the semaphores in shared memory. Also I need to create binary semaphores. As it is my first program, can someone suggest how to get started on this? I am able to create and use shared memory, used semaphores within the threads. I watched

JUC

你。 提交于 2019-11-28 06:24:05
JUC 回顾 1 NIO主要内容:Buffer、Channel 2 非阻塞式网络编程 今天任务 1 volatile的使用 2 原子变量和CAS算法 3 Lock接口 4 并发集合 5 同步工具类 第一节 JUC 概述 在 Java 5.0 提供了 java.util.concurrent(简称JUC)包,在此包中增加了在并发编程中很常用的工具类, 用于定义类似于线程的自定义子系统,包括线程池,异步IO和轻量级任务框架;还提供了用于多线程上下文中的 Collection实现等。 第二节 volatile volatile:易变的,不稳定的 在并发编程中的三个特性: (1)互斥性(原子性) (2)内存可见性 (3)指令重排序 int b=20; int a=10; int c=a+b; volatile 关键字: 当多个线程进行操作共享数据时,可以保证内存中的数据是可见的;相较于 synchronized 是一种较为轻量级的同步策略; volatile 不具备"互斥性"; volatile 不能保证变量的"原子性"; synchronized和volatile的区别: (1)synchronized可以实现互斥性和内存可见性,不能禁止指令重排序。 (2)volatile可以实现内存可见性,禁止指令重排序,不能保证原子性。 案例演示:内存可见性 public class

How to implement event listening in PHP

放肆的年华 提交于 2019-11-28 06:06:30
here is my problem: I have a script (let's call it comet.php) whic is requsted by an AJAX client script and wait for a change to happen like this: while(no_changes){ usleep(100000); //check for changes } I don't like this too much, it's not very scalable and it's (imho) "bad practice" I would like to improve this behaviour with a semaphore(?) or anyway concurrent programming technique. Can you please give me some tips on how to handle this? (I know, it's not a short answer, but a starting point would be enough.) Edit : what about LibEvent ? You can solve this problem using ZeroMQ . ZeroMQ is a

Incrementing the value of POSIX semaphores by more than 1

邮差的信 提交于 2019-11-28 05:56:07
问题 I have this requirement wherein I have to increment the value of a POSIX semaphore by more than 1. Apparently, there is no way in POSIX specification to do this. There is no sem_setvalue() similar to sem_getvalue(). I do not want to go back to System V semaphores just because of this constraint. Is there any alternative way to accomplish this? Or will I have to go the System V way? I am programming in C on GNU/Linux. Great thanks in advance. 回答1: I have this requirement wherein I have to

How do I stop sem_open() failing with ENOSYS?

会有一股神秘感。 提交于 2019-11-28 05:05:03
问题 I have two Slackware Linux systems on which the POSIX semaphore sem_open() call fails with errno set to 38. Sample code to reproduce below (the code works fine on CentOS / RedHat). Are there any kernel or system configuration options that could cause this? Other suggestions? Systems with issue are Slackware 10.1.0 kernel 2.6.11 /lib/librt-2.3.4.so /lib/libpthread-0.10.so, but the same code works on the much older RedHat 9 kernel 2.4.20 /lib/librt-2.3.2.so /lib/tls/libpthread-0.29.so. (and

Semaphore回顾

孤街浪徒 提交于 2019-11-28 05:03:53
用途 多线程访问可变变量时,是非线程安全的。可能导致程序崩溃。此时,可以通过使用信号量(semaphore)技术,保证多线程处理某段代码时,后面线程等待前面线程执行,保证了多线程的安全性。使用方法记两个就行了,一个是wait(dispatch_semaphore_wait),一个是signal(dispatch_semaphore_signal)。根据dispatch_semaphore_wait的参数类型提示去创建信号量(dispatch_semaphore_t)即可。 感觉原理跟PV操作如出一辙。 不用semaphore时code: #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); NSMutableArray *lArrM = [NSMutableArray array]; for (int i = 0; i < 100; ++i) { dispatch_async(dispatch_get_global_queue(0, 0), ^{ [lArrM addObject:@(i)]; }); } } return 0; } 运行结果

Using threads in C on Windows. Simple Example? [closed]

左心房为你撑大大i 提交于 2019-11-28 04:34:32
What do I need and how can I use threads in C on Windows Vista? Could you please give me a simple code example? Here is the MSDN sample on how to use CreateThread() on Windows. The basic idea is you call CreateThread() and pass it a pointer to your thread function, which is what will be run on the target thread once it is created. The simplest code to do it is: #include <windows.h> DWORD WINAPI ThreadFunc(void* data) { // Do stuff. This will be the first function called on the new thread. // When this function returns, the thread goes away. See MSDN for more details. return 0; } int main() {