semaphore

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

二次信任 提交于 2019-11-27 00:23:29
问题 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 4 years ago . What do I need and how can I use threads in C on Windows Vista? Could you please give me a simple code example? 回答1: 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

Semaphore vs. Monitors - what's the difference?

南楼画角 提交于 2019-11-26 23:44:42
问题 What are the major differences between a Monitor and a Semaphore ? 回答1: A Monitor is an object designed to be accessed from multiple threads. The member functions or methods of a monitor object will enforce mutual exclusion, so only one thread may be performing any action on the object at a given time. If one thread is currently executing a member function of the object then any other thread that tries to call a member function of that object will have to wait until the first has finished. A

CountDownLatch vs. Semaphore

馋奶兔 提交于 2019-11-26 23:23:01
Is there any advantage of using java.util.concurrent.CountdownLatch instead of java.util.concurrent.Semaphore ? As far as I can tell the following fragments are almost equivalent: 1. Semaphore final Semaphore sem = new Semaphore(0); for (int i = 0; i < num_threads; ++ i) { Thread t = new Thread() { public void run() { try { doStuff(); } finally { sem.release(); } } }; t.start(); } sem.acquire(num_threads); 2: CountDownLatch final CountDownLatch latch = new CountDownLatch(num_threads); for (int i = 0; i < num_threads; ++ i) { Thread t = new Thread() { public void run() { try { doStuff(); }

Program using Semaphores runs fine on Linux…unexpected results on Mac osX

心不动则不痛 提交于 2019-11-26 23:17:04
问题 I wrote a simple program solving the Readers-Writers problem using semaphores. It runs perfectly on Linux os, but when I run it on my Mac osX I get unexpected results and I can't figure out why. My Program: #include <semaphore.h> #include <sys/types.h> #include <stdio.h> #include <pthread.h> #include <unistd.h> void* function1(void* val); void* function2(void* val); // shared values volatile int X; volatile int Y; // declare semaphores sem_t s1; sem_t s2; main() { void* status; pthread_t

java信号量

╄→尐↘猪︶ㄣ 提交于 2019-11-26 23:16:52
维基百科 解释的信号量概念如下 信号量 ( 英语: semaphore )又称为 信号标 ,是一个同步对象,用于保持在0至指定最大值之间的一个计数值。当线程完成一次对该 semaphore对象的等待( wait)时,该计数值减一;当线程完成一次对 semaphore对象的释放( release)时,计数值加一。当计数值为0,则线程等待该 semaphore对象不再能成功直至该 semaphore对象变成 signaled状态。 semaphore对象的计数值大于0,为 signaled状态;计数值等于0,为 nonsignaled状态. semaphore对象适用于控制一个仅支持有限个用户的共享资源,是一种不需要使用 忙碌等待 ( busy waiting)的方法。 信号量的概念是由 荷兰 计算机科学家 艾兹赫尔·戴克斯特拉 ( Edsger W. Dijkstra)发明的,广泛的应用于不同的 操作系统 中。在系统中,给予每一个 行程 一个信号量,代表每个行程目前的状态,未得到控制权的行程会在特定地方被强迫停下来,等待可以继续进行的讯号到来。如果信号量是一个任意的整数,通常被称为计数讯号量( Counting semaphore),或一般讯号量( general semaphore);如果信号量只有二进位的0或1,称为二进位讯号量( binary semaphore)。在

Do forked child processes use the same semaphore?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 22:55:34
Let's say I create a semaphore. If I fork a bunch of child processes, will they all still use that same semaphore? Also, suppose I create a struct with semaphores inside and forked. Do all the child processes still use that same semaphore? If not, would storing that struct+semaphores in shared memory allow the child processes to use the same semaphores? I'm really confused about how my forked child processes can use the same semaphores. bdonlan Let's say I create a semaphore. If I fork a bunch of child processes, will they all still use that same semaphore? If you are using a SysV IPC

sem_getvalue() dysfunctionality in Mac OS X - C++

╄→尐↘猪︶ㄣ 提交于 2019-11-26 22:04:59
问题 I'm trying to implement synchronized use of a shared memory for bunch of threads in Mac OS X by semaphores. (I just overlook the fact that Mac users have got lots of issues with initializing a semaphore and destroying it...,which can be fixed by sem_open() and sem_unlink()):D But apparently for getting semaphore's current value there's nothing but sem_getvalue() which hasn't been implemented in mac os x. Any suggestion for someone that doesn't have linux OS running and should upload his

Conditional Variable vs Semaphore

痞子三分冷 提交于 2019-11-26 21:11:38
When should one use a semaphore and when should one use a conditional variable (CondVar) ? Locks are used for mutual exclusion. When you want to ensure that a piece of code is atomic, put a lock around it. You could theoretically use a binary semaphore to do this, but that's a special case. Semaphores and condition variables build on top of the mutual exclusion provide by locks and are used for providing synchronized access to shared resources. They can be used for similar purposes. A condition variable is generally used to avoid busy waiting (looping repeatedly while checking a condition)

How to use POSIX semaphores on forked processes in C?

一个人想着一个人 提交于 2019-11-26 19:40:15
I want to fork multiple processes and then use a semaphore on them. Here is what I tried: sem_init(&sem, 1, 1); /* semaphore*, pshared, value */ . . . if(pid != 0){ /* parent process */ wait(NULL); /* wait all child processes */ printf("\nParent: All children have exited.\n"); . . /* cleanup semaphores */ sem_destroy(&sem); exit(0); } else{ /* child process */ sem_wait(&sem); /* P operation */ printf(" Child(%d) is in critical section.\n",i); sleep(1); *p += i%3; /* increment *p by 0, 1 or 2 based on i */ printf(" Child(%d) new value of *p=%d.\n",i,*p); sem_post(&sem); /* V operation */ exit(0

【JDK】JDK源码分析-Semaphore

拜拜、爱过 提交于 2019-11-26 19:30:25
概述 Semaphore 是并发包中的一个工具类,可理解为信号量。通常可以作为限流器使用,即限制访问某个资源的线程个数,比如用于限制连接池的连接数。 打个通俗的比方,可以把 Semaphore 理解为一辆公交车:车上的座位数(初始的“许可” permits 数量)是固定的,行驶期间如果有人上车(获取许可),座位数(许可数量)就会减少,当人满的时候不能再继续上车了(获取许可失败);而有人下车(释放许可)后就空出了一些座位,其他人就可以继续上车了。 下面具体分析其代码实现。 代码分析 Semaphore 的方法如下: 其中主要方法是 acquire() 和 release() 相关的一系列方法,它们的作用类似。我们先从构造器开始分析。 构造器 private final Sync sync; // 初始化 Semaphore,传入指定的许可数量,非公平 public Semaphore(int permits) { sync = new NonfairSync(permits); } // 初始化 Semaphore,传入指定的许可数量,指定是否公平 public Semaphore(int permits, boolean fair) { sync = fair ? new FairSync(permits) : new NonfairSync(permits); } 构造器初始化了