mutex

Mutual Exclusion Problem

…衆ロ難τιáo~ 提交于 2019-12-01 06:09:34
问题 Please take a look on the following pseudo-code: boolean blocked[2]; int turn; void P(int id) { while(true) { blocked[id] = true; while(turn != id) { while(blocked[1-id]) /* do nothing */; turn = id; } /* critical section */ blocked[id] = false; /* remainder */ } } void main() { blocked[0] = false; blocked[1] = false; turn = 0; parbegin(P(0), P(1)); //RUN P0 and P1 parallel } I thought that a could implement a simple Mutual - Exclution solution using the code above. But it's not working. Has

Why does MSDN sample from Threading Tutorial crash?

懵懂的女人 提交于 2019-12-01 05:49:52
From sample example 4 of MSDN "Threading Tutorial" Following code errors out at the line commented with "---errors is here---". What is wrong? using System; using System.Threading; public class MutexSample { static Mutex gM1; static Mutex gM2; const int ITERS = 100; static AutoResetEvent Event1 = new AutoResetEvent(false); static AutoResetEvent Event2 = new AutoResetEvent(false); static AutoResetEvent Event3 = new AutoResetEvent(false); static AutoResetEvent Event4 = new AutoResetEvent(false); public static void Main(String[] args) { Console.WriteLine("Mutex Sample ..."); // Create Mutex

cygwin pthread_mutex_timedlock surrogate

我的梦境 提交于 2019-12-01 05:29:34
问题 Unfortunately the cygwin GCC 4.5.3 pthread library implementation doesn't support the POSIX standard function int pthread_mutex_timedlock(pthread_mutex_t* mutex, struct timespec* abstime); Has anyone a good idea how to implement a good workaround for this method in a mutex wrapper class? May be using pthread_mutex_trylock() with a (milliseconds based) nanosleep() call? I don't have a good feeling about the latter idea, but anyway the C++ implementation could look like this: bool

Unexpected behavior using std::try_to_lock

本秂侑毒 提交于 2019-12-01 05:23:40
问题 I get surprising and conflicting behavior when I try to run the following code. #include <iostream> #include <mutex> int main() { std::mutex mtx; std::unique_lock<std::mutex> lock1(mtx); std::unique_lock<std::mutex> lock2(mtx, std::try_to_lock); std::cout << "lock1 owns lock: " << lock1.owns_lock() << std::endl; std::cout << "lock2 owns lock: " << lock2.owns_lock() << std::endl; } When I run this on my computer (linux with either clang++ 4.0.1 or g++ 7.3.0) it prints out that both lock1 and

Assignment via copy-and-swap vs two locks

随声附和 提交于 2019-12-01 05:06:13
Borrowing Howard Hinnant's example and modifying it to use copy-and-swap, is this op= thread-safe? struct A { A() = default; A(A const &x); // Assume implements correct locking and copying. A& operator=(A x) { std::lock_guard<std::mutex> lock_data (_mut); using std::swap; swap(_data, x._data); return *this; } private: mutable std::mutex _mut; std::vector<double> _data; }; I believe this thread-safe (remember op='s parameter is passed by value), and the only problem I can find is the one swept under the rug: the copy ctor. However, it would be a rare class that allows copy-assignment but not

Can someone Explain Mutex and how it is used?

大憨熊 提交于 2019-12-01 04:44:44
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-second time-out interval switch (dwWaitResult) { // The thread got mutex ownership. case WAIT_OBJECT_0:

Releasing a Mutex

假如想象 提交于 2019-12-01 04:04:46
I have a web application that needs to utilise an application cache to store data (due to the high overhead of obtaining that data ona request by request basis). See previous post at https://stackoverflow.com/a/16961962/236860 This approach seems to work well, but I am seeing the following occasional errors in the web site's error: System.ApplicationException: Object synchronization method was called from an unsynchronized block of code. at System.Threading.Mutex.ReleaseMutex() at InboxInsight.Web.Web_Controls.Twitter.TwitterFeed.GetTwitterData(HttpContext context) at InboxInsight.Web.Web

Why does MSDN sample from Threading Tutorial crash?

时光总嘲笑我的痴心妄想 提交于 2019-12-01 03:40:45
问题 From sample example 4 of MSDN "Threading Tutorial" Following code errors out at the line commented with "---errors is here---". What is wrong? using System; using System.Threading; public class MutexSample { static Mutex gM1; static Mutex gM2; const int ITERS = 100; static AutoResetEvent Event1 = new AutoResetEvent(false); static AutoResetEvent Event2 = new AutoResetEvent(false); static AutoResetEvent Event3 = new AutoResetEvent(false); static AutoResetEvent Event4 = new AutoResetEvent(false)

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

不想你离开。 提交于 2019-12-01 03:04:11
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 { void do_stuff(); }; Object o; std::thread worker_thread([&o](){ while (alive) o.do_stuff(); }).join(

Difference between std::mutex lock function and std::lock_guard<std::mutex>?

妖精的绣舞 提交于 2019-12-01 02:52:34
问题 Basically, the title is self-explanatory. I use it in following way: The code is in Objective-C++. Objective-C classes make concurrent calls to different purpose functions. I use std::mutex to lock and unlock std::vector<T> editing option across entire class, as C++ std containers are not thread safe. 回答1: Using lock_guard automatically unlocks the mutex again when it goes out of scope. That makes it impossible to forget to unlock it, when returning, or when an exception is thrown. You should