mutex

In dotnet core how can I ensure only one copy of my application is running?

↘锁芯ラ 提交于 2019-12-20 04:15:08
问题 In the past I have done something like this private static bool AlreadyRunning() { var processes = Process.GetProcesses(); var currentProc = Process.GetCurrentProcess(); logger.Info($"Current proccess: {currentProc.ProcessName}"); foreach (var process in processes) { if (currentProc.ProcessName == process.ProcessName && currentProc.Id != process.Id) { logger.Info($"Another instance of this process is already running: {process.Id}"); return true; } } return false; } Which has worked well. In

Linux pthread mutex and kernel scheduler

妖精的绣舞 提交于 2019-12-20 02:18:06
问题 With a friend of mine, we disagree on how synchronization is handled at userspace level (in the pthread library). a. I think that during a pthread_mutex_lock, the thread actively waits. Meaning the linux scheduler rises this thread, let it execute his code, which should looks like: while (mutex_resource->locked); Then, another thread is scheduled which potentially free the locked field, etc. So this means that the scheduler waits for the thread to complete its schedule time before switching

Linux pthread mutex and kernel scheduler

不打扰是莪最后的温柔 提交于 2019-12-20 02:18:05
问题 With a friend of mine, we disagree on how synchronization is handled at userspace level (in the pthread library). a. I think that during a pthread_mutex_lock, the thread actively waits. Meaning the linux scheduler rises this thread, let it execute his code, which should looks like: while (mutex_resource->locked); Then, another thread is scheduled which potentially free the locked field, etc. So this means that the scheduler waits for the thread to complete its schedule time before switching

How can I return an iterator over a locked struct member in Rust?

帅比萌擦擦* 提交于 2019-12-20 01:59:41
问题 Here is as far as I could get, using rental, partly based on How can I store a Chars iterator in the same struct as the String it is iterating on?. The difference here is that the get_iter method of the locked member has to take a mutable self reference. I'm not tied to using rental: I'd be just as happy with a solution using reffers or owning_ref. The PhantomData is present here just so that MyIter bears the normal lifetime relationship to MyIterable , the thing being iterated over. I also

WaitForSingleObject - do threads waiting form a queue?

ε祈祈猫儿з 提交于 2019-12-19 17:36:19
问题 If I set 3 threads to wait for a mutex to be release, do they form a queue based on the order they requested it in or is it undefined behaviour (i.e. we don't know which one will pick it up first)? 回答1: It is explicitly documented in the SDK article: If more than one thread is waiting on a mutex, a waiting thread is selected. Do not assume a first-in, first-out (FIFO) order. External events such as kernel-mode APCs can change the wait order. These kind of events are entirely out of your

How To Mutex Across a Network?

六月ゝ 毕业季﹏ 提交于 2019-12-19 17:15:33
问题 I have a desktop application that runs on a network and every instance connects to the same database. So, in this situation, how can I implement a mutex that works across all running instances that are connected to the same database? In other words, I don't wan't that two+ instances to run the same function at the same time. If one is already running the function, the other instances shouldn't have access to it. PS: Database transaction won't solve, because the function I wan't to mutex doesn

Can it be assumed that `pthread_cond_signal` will wake the signaled thread atomically with regards to the mutex bond to the condition variable?

╄→尐↘猪︶ㄣ 提交于 2019-12-19 11:53:37
问题 Quoting POSIX: The pthread_cond_broadcast() or pthread_cond_signal() functions may be called by a thread whether or not it currently owns the mutex that threads calling pthread_cond_wait() or pthread_cond_timedwait() have associated with the condition variable during their waits; however, if predictable scheduling behavior is required, then that mutex shall be locked by the thread calling pthread_cond_broadcast() or pthread_cond_signal() . "If predictable scheduling behavior is required".

Compiler says that data cannot be shared between threads safely even though the data is wrapped within a Mutex

拜拜、爱过 提交于 2019-12-19 09:45:17
问题 I'm using Rocket which has a State that it passes to the HTTP requests. This struct contains a Mutex<DatastoreInstance> which gives access to a SQLite database and is locked with a mutex to make read and writes safe. pub struct DatastoreInstance { conn: Connection, } When the DatastoreInstance struct looked like this, with only a SQLite connection everything worked fine, but I then also wanted to add a transaction object within this struct: pub struct DatastoreInstance { conn: Connection,

Why would WaitForSingleObject return WAIT_FAILED

放肆的年华 提交于 2019-12-19 09:21:10
问题 MSDN says If the function fails, the return value is WAIT_FAILED. To get extended error information, call GetLastError. The code is: HANDLE m_mutex_handle; /**< m_mutex_handle. The handle to the created mutex. */ m_mutex_handle = ::CreateMutex( 0, false, NULL ); ::WaitForSingleObject( m_mutex_handle, INFINITE ); But what are the reasons that could happen? 回答1: If you lack the SYNCHRONIZE privilege on the object, then you cannot wait. WAIT_FAILED will be returned. 回答2: Passing in a bogus

Can someone Explain Mutex and how it is used?

爷,独闯天下 提交于 2019-12-19 07:27:09
问题 I read a few documents about Mutex and still the only Idea I have got is that it helps preventing threads from accessing a resource that is already being used by another resource. I got from Code snippet and executed which works fine: #include <windows.h> #include <process.h> #include <iostream> using namespace std; BOOL FunctionToWriteToDatabase(HANDLE hMutex) { DWORD dwWaitResult; // Request ownership of mutex. dwWaitResult = WaitForSingleObject( hMutex, // handle to mutex 5000L); // five