semaphore

Do semaphores prevent instruction reordering?

烈酒焚心 提交于 2019-12-01 04:09:39
问题 I was looking for an awaitable equivalent of lock statements in C#. Some people suggest using a binary SemaphoreSlim in the following way: await semaphore.WaitAsync().ConfigureAwait(false); try { //inner instructions } finally { semaphore.Release(); } I know it has some issues (e.g. it's not reentrant), but my biggest concern is with the instruction reeordering. In plain old lock statements we have a guarantee that no inner instruction from the lock will be moved outside (before or after) the

sem_post, signal handlers, and undefined behavior

爱⌒轻易说出口 提交于 2019-12-01 03:35:57
问题 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

Shared memory access control mechanism for processes created by MPI

荒凉一梦 提交于 2019-12-01 01:12:05
I have a shared memory used by multiple processes, these processes are created using MPI . Now I need a mechanism to control the access of this shared memory. I know that named semaphore and flock mechanisms can be used to do this but just wanted to know if MPI provides any special locking mechanism for shared memory usage ? I am working on C under Linux. MPI actually does provide support for shared memory now (as of version 3.0). You might try looking at the One-sided communication chapter ( http://www.mpi-forum.org/docs/mpi-3.0/mpi30-report.pdf ) starting with MPI_WIN_ALLOCATE_SHARED (11.2.3

Is semaphore usage in this solution is correct?

↘锁芯ラ 提交于 2019-12-01 00:30:21
Problem: I have to increment x1 and x2 variable which should be done by separate threads and next increment of both variables should not be called until previous increment of both variable is not completed. Proposed Solution: Initialize 4 semaphore and invoke separate threads for separate increment of variable. 2 semaphores for passing message to threads for start incrementing and 2 semaphores for passing message to main thread that incrementation is completed. Main thread will wait for semaphore posting from both child threads showing incrementation of both variable is done, then main thread

Threads and simple Dead lock cure

a 夏天 提交于 2019-12-01 00:08:26
问题 When dealing with threads (specifically in C++) using mutex locks and semaphores is there a simple rule of thumb to avoid Dead Locks and have nice clean Synchronization? 回答1: A good simple rule of thumb is to always obtain your locks in a consistent predictable order from everywhere in your application. For example, if your resources have names, always lock them in alphabetical order. If they have numeric ids, always lock from lowest to highest. The exact order or criteria is arbitrary. The

Java 并发工具包 java.util.concurrent 用户指南

陌路散爱 提交于 2019-12-01 00:08:04
译序 本指南根据 Jakob Jenkov 最新博客翻译, 请随时关注博客更新 本指南已做成中英文对照阅读版的 pdf 文档,有兴趣的朋友可以去 Java并发工具包java.util.concurrent用户指南中英文对照阅读版 进行下载。 1. java.util.concurrent - Java并发工具包 Java 5 添加了一个新的包到 Java 平台,java.util.concurrent 包。这个包包含有一系列能够让 Java 的并发编程变得更加简单轻松的类。在这个包被添加以前,你需要自己去动手实现自己的相关工具类。 本文我将带你一一认识 java.util.concurrent 包里的这些类,然后你可以尝试着如何在项目中使用它们。本文中我将使用 Java 6 版本,我不确定这和 Java 5 版本里的是否有一些差异。 我不会去解释关于 Java 并发的核心问题 - 其背后的原理,也就是说,如果你对那些东西感兴趣,参考 《Java 并发指南》 半成品 本文很大程度上还是个 “半成品”,所以当你发现一些被漏掉的类或接口时,请耐心等待。在我空闲的时候会把它们加进来的。 2. 阻塞队列BlockingQueue java.util.concurrent 包里的 BlockingQueue 接口表示一个线程安放入和提取实例的队列。本小节我将给你演示如何使用这个

Waiting on multiple semaphores without busy-waiting (C/C++ Linux)

白昼怎懂夜的黑 提交于 2019-11-30 22:31:15
If I have more than one semaphore, how can I have a process block until at least one of the semaphores is free? I know I can do this with a busy-wait loop such as: // blocks until one of the semaphores in sems is free, returns // index of semaphore that was available int multiple_sem_wait(sem_t **sems, int num_sems) { while (true) { for (int i = 0; i < num_sems; ++i) { if (sem_trywait(sems[i]) == 0) { return i; } } } } But is there a way to do this without a busy-loop? Perhaps there's some IPC technique other than semaphores that I should be using? Thanks vhallac Here (developers.sun.com, via

Semaphores and Mutex for Thread and Process Synchronization

£可爱£侵袭症+ 提交于 2019-11-30 21:38:38
问题 I am confused with the usage of semaphores and mutexes at thread and process level. Can we use semphores and mutexes for both thread and process synchronization, or do we have different semaphores and mutexes both at thread and process level? My question is with reference to the POSIX API's. 回答1: The answer to both questions is yes. You can create both mutexes and semaphores as either process-shared or not. So you can use them as interprocess or interthread synchronization objects, but you

进程通信4--信号量(Semaphore)

时光怂恿深爱的人放手 提交于 2019-11-30 19:47:05
信号量是什么? (1) 信号量本质上是一个具有原子性的计数器 ,用来描述临界资源的,不能用全局变量count加加减减替换(因为他 没有原子性 )。 (2)信号量以保护临界资源为目的,但他本身也是个临界资源;他控制多个进程对共享资源的访问,通常描述临界资源当中,临界资源的数量,常常被当做锁来使用,防止一个进程访问另外一个进程正在使用的资源 (3)其中最简单的信号量=1,也叫做 二元信号量(互斥锁) ,可控制单个资源,只能取0和1;信号量>=2为 多元信号量 只要理理解上面三句话,应该结合代码理解就不难了。 代码实现相关函数: # include <sys/types.h> # include <sys/ipc.h> key_t ftok ( const char * pathname , int id ) //pathname:路径名 id:项目id,非0整数(只有低8位有效) //2.semget函数 int semget ( key_t key , int nsems , int semflg ) ; //参数:key-->信号集的名字 //nsems:信号集中信号量的个数 //semflg:由九个权限构成,用法和创建文件时使用的mode模式一样 //返回值:成功返回一个非负整数,即该信号集的标识码;失败返回-1 创建成功后,Linux下通过ipcs

Is semaphore usage in this solution is correct?

倾然丶 夕夏残阳落幕 提交于 2019-11-30 19:10:22
问题 Problem: I have to increment x1 and x2 variable which should be done by separate threads and next increment of both variables should not be called until previous increment of both variable is not completed. Proposed Solution: Initialize 4 semaphore and invoke separate threads for separate increment of variable. 2 semaphores for passing message to threads for start incrementing and 2 semaphores for passing message to main thread that incrementation is completed. Main thread will wait for