mutex

How to implement single instance per machine application?

点点圈 提交于 2020-01-09 07:57:18
问题 I have to restrict my .net 4 WPF application so that it can be run only once per machine. Note that I said per machine, not per session. I implemented single instance applications using a simple mutex until now, but unfortunately such a mutex is per session. Is there a way to create a machine wide mutex or is there any other solution to implement a single instance per machine application? 回答1: I would do this with a global Mutex object that must be kept for the life of your application.

【记录复查】mutex | critical section

允我心安 提交于 2020-01-07 11:28:35
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> ==========================blogI============================ 1、临界区只能用于对象在同一进程里线程间的互斥访问;互斥体可以用于对象进程间或线程间的互斥访问。 2、临界区是非内核对象,只在用户态进行锁操作,速度快;互斥体是内核对象,在核心态进行锁操作,速度慢。 3、临界区和互斥体在Windows平台都下可用;Linux下只有互斥体可用。 1、临界区:通过对多线程的串行化来访问公共资源或一段代码,速度快,适合控制数据访问。 2、互斥量:为协调共同对一个共享资源的单独访问而设计的。 临界区(Critical Section)   保证在某一时刻只有一个线程能访问数据的简便办法。在任意时刻只允许一个线程对共享资源进行访问。如果有多个线程试图同时访问临界区,那么在有一个线程进入后其他所有试图访问此临界区的线程将被挂起,并一直持续到进入临界区的线程离开。临界区在被释放后,其他线程可以继续抢占,并以此达到用原子方式操作共享资源的目的。   临界区包含两个操作原语:   EnterCriticalSection() 进入临界区   LeaveCriticalSection() 离开临界区   EnterCriticalSection(

Windows API mutex re-entry issue despite making threads Sleep()

倾然丶 夕夏残阳落幕 提交于 2020-01-07 04:54:05
问题 I'm trying to get my head around Windows API threads and thread control. I briefly worked with threads in Java so I know the basic concepts but something that has worked in Java seems to only work halfway in C++. What I am trying to do is as follows: 2 threads in one process, sharing a common resource(for this case, the common resource is a pair of two global variables int a, b; ). The first thread should acquire a mutex, use rand() to generate pairs of numbers from 0 to 100 until it gets a

What to do with interprocess communication between two processes?

戏子无情 提交于 2020-01-07 02:56:05
问题 I need some help regarding interprocess communication. I have an Application A and Application B. Application B purpose is to update Application A. As Application A can't update himself, there must be some dll's need to be updated that is why Applicaiton B is used. Appication A launches App B and App B closes App A and start updating A. The updater process is two step 1) Copies the msi bits 2) Install the bits If the user cancels Application B in first step while App A is waiting, is there

Why do pthreads’ condition variable functions require a mutex?

ε祈祈猫儿з 提交于 2020-01-07 01:50:10
问题 I’m reading up on pthread.h ; the condition variable related functions (like pthread_cond_wait(3) ) require a mutex as an argument. Why? As far as I can tell, I’m going to be creating a mutex just to use as that argument? What is that mutex supposed to do? 回答1: It's just the way that condition variables are (or were originally) implemented. The mutex is used to protect the condition variable itself . That's why you need it locked before you do a wait. The wait will "atomically" unlock the

Why do pthreads’ condition variable functions require a mutex?

蹲街弑〆低调 提交于 2020-01-07 01:49:09
问题 I’m reading up on pthread.h ; the condition variable related functions (like pthread_cond_wait(3) ) require a mutex as an argument. Why? As far as I can tell, I’m going to be creating a mutex just to use as that argument? What is that mutex supposed to do? 回答1: It's just the way that condition variables are (or were originally) implemented. The mutex is used to protect the condition variable itself . That's why you need it locked before you do a wait. The wait will "atomically" unlock the

thread_cancel and blocking function as cond_wait

[亡魂溺海] 提交于 2020-01-06 14:13:56
问题 I have my main process send pthread_cancel to another thread which is waiting for a condition to happen with cond_wait(&condition) . On the pthread_cancel they are saying : Deferred cancel ability means that cancellation will be delayed until the thread next calls a function that is a cancellation point. But often those function are blocking function. Then my question is the thread cancelled only after that thread has been unblock (in my example by a broadcast or a signal) or it would see

thread_cancel and blocking function as cond_wait

帅比萌擦擦* 提交于 2020-01-06 14:13:39
问题 I have my main process send pthread_cancel to another thread which is waiting for a condition to happen with cond_wait(&condition) . On the pthread_cancel they are saying : Deferred cancel ability means that cancellation will be delayed until the thread next calls a function that is a cancellation point. But often those function are blocking function. Then my question is the thread cancelled only after that thread has been unblock (in my example by a broadcast or a signal) or it would see

My recursive mutex vs pthread_mutex_t (type: recursive) (repost, push)

≯℡__Kan透↙ 提交于 2020-01-06 08:21:21
问题 I was wondering if I could make a recursive mutex type on my own with a PTHREAD_MUTEX_ERRORCHECK mutex, this is the result: typedef struct { pthread_mutex_t mutex; uint32_t deadlocks; } pthread_recursivemutex_t; int pthread_recursivemutex_init(pthread_recursivemutex_t *mutex) { int ret; pthread_mutexattr_t attr; mutex->deadlocks = 0; ret = pthread_mutexattr_init(&attr); if (ret != 0) { return ret; } (void)pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); ret = pthread_mutex_init(

Do we need mutex to perform multithreading file IO

时光怂恿深爱的人放手 提交于 2020-01-06 07:27:12
问题 I'm trying to do random write (Benchmark test) to a file using multiple threads (pthread). Looks like if I comment out mutex lock the created file size is less than actual as if Some writes are getting lost (always in some multiple of chunk size). But if I keep the mutex it's always exact size. Is my code have a problem in other place and mutex is not really required (as suggested by @evan ) or mutex is necessary here void *DiskWorker(void *threadarg) { FILE *theFile = fopen(fileToWrite, "a+"