mutex

Using class instance variable for mutex in Ruby

十年热恋 提交于 2019-12-04 03:48:16
问题 Note: The code summary shown below is not a distillation of the code that I had the problem with. I've left this original summary here since someone else already answered, but the actual code is shown in the answer I've provided below. I haven't been able to isolate this to a small failing test case, but I'm getting a failure with the following general construct: class Foo @mutex = Mutex.new .... def self.bar @mutex.synchronize { ... } end end If I create multiple threads invoking Foo.bar ,

clarifications on full memory barriers involved by pthread mutexes

£可爱£侵袭症+ 提交于 2019-12-04 03:43:01
问题 I have heard that when dealing with mutexes, the necessary memory barriers are handled by the pthread API itself. I would like to have more details on this matter. Are these claimings true, at least on the most common architectures around? Does the compiler recognize this implicit barrier, and avoids reordering of operations/read from local registers when generating the code? When is the memory barrier applied: after successfully acquiring a mutex AND after releasing it? 回答1: The POSIX

VB6: Single-instance application across all user sessions

删除回忆录丶 提交于 2019-12-04 03:27:58
问题 I have an application that needs to be a single-instance app across all user sessions on a Windows PC. My research thus far has centered around using a mutex to accomplish this, but I am having an issue that I am not sure is really an issue, this is really a best-practice question I believe. Here's the code first of all: Private Const AppVer = "Global\UNIQUENAME" ' This is not what i am using but the name is unique Public Sub Main() Dim mutexValue As Long mutexValue = CreateMutex(ByVal 0&, 1,

How to use pthread_mutex_trylock?

我怕爱的太早我们不能终老 提交于 2019-12-04 02:55:25
Using trylock: FILE *fp; pthread_mutex_t demoMutex; void * printHello (void* threadId) { pthread_mutex_trylock (&demoMutex); pthread_t writeToFile = pthread_self (); unsigned short iterate; for (iterate = 0; iterate < 10000; iterate++) { fprintf (fp, " %d ", iterate, 4); fprintf (fp, " %lu ", writeToFile, sizeof (pthread_t)); fprintf (fp, "\n", writeToFile, 1); } pthread_mutex_unlock (&demoMutex); pthread_exit (NULL); } and then main (): int main () { pthread_t arrayOfThreadId [5]; int returnValue; unsigned int iterate; fp = fopen ("xyz", "w"); pthread_mutex_init (&demoMutex, NULL); for

AppMutex with Inno Setup: Wait few seconds before prompt

安稳与你 提交于 2019-12-04 02:40:24
问题 Using Inno Setup together with an AppMutex works fine - when the setup is started and the mutex still exits, the user is prompted to close this application. But following question: Is there a way to tell Inno Setup to wait 2-3 seconds if the program closes itself before showing the user this prompt? The reason is that I'm running the Inno Setup from the program itself for auto-update purpose. Directly after the setup file is executed the program closes itself, but obviously that takes too

How can I synchronize three threads?

血红的双手。 提交于 2019-12-04 02:05:56
问题 My app consist of the main-process and two threads, all running concurrently and making use of three fifo-queues: The fifo-q's are Qmain, Q1 and Q2. Internally the queues each use a counter that is incremented when an item is put into the queue, and decremented when an item is 'get'ed from the queue. The processing involve two threads, QMaster, which get from Q1 and Q2, and put into Qmain, Monitor, which put into Q2, and the main process, which get from Qmain and put into Q1. The QMaster

C pthread mutex: Expected expression before `{'

て烟熏妆下的殇ゞ 提交于 2019-12-04 00:52:13
问题 I am using the pthread library to create two threads. I am using two queues to communicate the data between the two threads (producer-consumer) and hence want to have a mutex to sync the push-pops in the queue by the threads. But I get a compile error as follows: $ gcc simple-tun.c simple-tun -lpthread simple-tun.c: In function ‘new_queue’: simple-tun.c:920:13: error: expected expression before ‘{’ token The the function where I get the error is: 908 struct queue * new_queue () { 909 910

Detecting if another instance of the application is already running

老子叫甜甜 提交于 2019-12-04 00:23:57
My application needs to behave slightly differently when it loads if there is already an instance running. I understand how to use a mutex to prevent additional instances loading, but that doesn't quite solve my problem. For example: Instance 1 loads, gets the mutex. Instance 2 loads, can't get the mutex, knows there's another instance. So far, so good. Instance 1 closes, releases the mutex. Instance 3 loads, gets the mutex, doesn't know that Instance 2 is still running. Any ideas? Thankfully it doesn't need to deal with multiple user accounts or anything like that. (C#, desktop application)

What are the practical uses of semaphores?

你离开我真会死。 提交于 2019-12-04 00:21:01
Nonbinary ones.. I have never encountered a problem that required me to use a semaphore instead of mutex. So is this mostly theoretical construct, or real sw like Office, Firefox have places where they use it? If so what are the common use patterns for semaphores? I think you already know what a semaphore is and you are just wondering how it is used on "real software". "real sw like Office, Firefox have places where they use it?" Yes, "real software" use semaphores a lot, it is not just theoretical, e.g. Chromium source, windows semaphore handling code and the same code used by Virtual Box .

Create object in thread A, use in thread B. Mutex required?

十年热恋 提交于 2019-12-04 00:14:41
问题 I have been reading different things on multithreading, C++, proper synchronization and locks to prevent race conditions. One question has not been answered for me, however: Is there a mutex required if I create an object in thread A, but use it exclusively in thread B afterwards? In other words, I know that I don't need a mutex to prevent race conditions - do I need a mutex to serve as a memory barrier (or other potential problems)? A very basic example to visualize what I mean struct Object