mutex

Monitor vs Mutex

非 Y 不嫁゛ 提交于 2019-12-03 04:14:30
问题 I read that mutex is a semaphore with value 1 (binary semaphore) which is used for enforcing mutual exclusion. I read this link Semaphore vs. Monitors - what's the difference? which says that monitor helps in achieving mutual exclusion. Can anyone tell me the difference between mutex and monitor as both are actually doing the same thing 回答1: Since you haven't specified which OS or language/library you are talking about, let me answer in a generic way. Conceptually they are the same. But

Cost of mutex,critical section etc on Windows

a 夏天 提交于 2019-12-03 03:11:02
I read somewhere that the overhead of a mutex is not that much, because the context switching only happens in case of contention. Also known Futexes in Linux. Does the same thing hold good in Windows? Is Critical Section a more apt map to mutexes in Linux. From what i gathered, Critical Sections provide better optimal performance compared to Mutex, is this true for every case? Is there a corner case where mutexes are faster than critical section in Windows. Assume only a single process-threads are accessing the mutexes(Just to eliminate the other benefit of Critical Sections) Added Info: OS

std::mutex vs std::recursive_mutex as class member

别来无恙 提交于 2019-12-03 00:53:12
问题 I have seen some people hate on recursive_mutex : http://www.zaval.org/resources/library/butenhof1.html But when thinking about how to implement a class that is thread safe (mutex protected), it seems to me excruciatingly hard to prove that every method that should be mutex protected is mutex protected and that mutex is locked at most once. So for object oriented design, should std::recursive_mutex be default and std::mutex considered as an performance optimization in general case unless it

How to profile pthread mutex in linux?

孤人 提交于 2019-12-03 00:10:42
I would like to know how to profile a pthread mutex to see if there are any locking contention points in my code. (who likes contentious code, right? :) I know how to do a more general profiling of the code, as I mention here . But I would like to know if there are any tools or options available to be able to profile mutex locking that would provide metrics/stats about mutex locking contentions to see if I have any problem areas. Here's some background and context: Recently I worked on an embedded C++ project using a Cavium Octeon CPU. The Octeon SDK implements mutex style synchronization

Implementing mutexes for file writes

╄→尐↘猪︶ㄣ 提交于 2019-12-02 23:39:31
问题 I am trying to use mutexes to avoid multiple writes to the same thread inC/Cpp. Below is the flow of my program. I am confused as to where to include my lock and unlock code. main() { spawn a worker thread } worker_thread() { read the input file name read some content write the content to the given file name } Most of the implementation that I see, seem to have something like this: main() { pthread_mutex_init(&myMutex;,0); *spawn a worker thread* pthread_join(thread1, 0); pthread_mutex

Get a list of mutex?

主宰稳场 提交于 2019-12-02 23:35:04
A program creates a mutex as part of its start-up. I don't know the format of this mutex so I wondered if there is a way to get a list of all non-abandoned mutex, open the program, get a new list and see if I can find the mutex by removing all duplicate entries. Is there a way to get this list? If you're on Windows, WinObj can show you named mutexes. Or you can use Process Explorer to find out which objects a specific process has open. If you have WinObj.exe it is likely that you also have handle.exe which is also from the SysInternals-Suite. Occasionally I found handle -a |findstr /C:Mutant

Implement a high performance mutex similar to Qt's one

旧时模样 提交于 2019-12-02 23:29:56
I have a multi-thread scientific application where several computing threads (one per core) have to store their results in a common buffer. This requires a mutex mechanism. Working threads spend only a small fraction of their time writing to the buffer, so the mutex is unlocked most of the time, and locks have a high probability to succeed immediately without waiting for another thread to unlock. Currently, I have used Qt's QMutex for the task, and it works well : the mutex has a negligible overhead. However, I have to port it to c++11/STL only. When using std::mutex, the performance drops by

custom RAII C++ implementation for scoped mutex locks

心不动则不痛 提交于 2019-12-02 22:16:22
I cannot use boost or the latest std::thread library. The way to go is to create a custom implementation of a scoped mutex. In a few words when a class instance is create a mutex locks. Upon class destruction the mutex is unlocked. Any implementation available? I don't want to re-invent the wheel. I need to use pthreads. resource acquisition is initialization == “RAII” Note This is an old answer. C++11 contains better helpers that are more platform independent: std::lock_guard std::mutex , std::timed_mutex , std::recursive_mutex , std::recursive_timed_mutex And other options like std::unique

Boost Mutex Scoped Lock

白昼怎懂夜的黑 提交于 2019-12-02 20:40:42
I was reading through a Boost Mutex tutorial on drdobbs.com, and found this piece of code: #include <boost/thread/thread.hpp> #include <boost/thread/mutex.hpp> #include <boost/bind.hpp> #include <iostream> boost::mutex io_mutex; void count(int id) { for (int i = 0; i < 10; ++i) { boost::mutex::scoped_lock lock(io_mutex); std::cout << id << ": " << i << std::endl; } } int main(int argc, char* argv[]) { boost::thread thrd1( boost::bind(&count, 1)); boost::thread thrd2( boost::bind(&count, 2)); thrd1.join(); thrd2.join(); return 0; } Now I understand the point of a Mutex is to prevent two threads

What is the Mutex and semaphore In c#? where we need to implement? [closed]

三世轮回 提交于 2019-12-02 20:35:07
What is the Mutex and semaphore in C#? Where we need to implement? How can we work with them in multithreading? You should start at MSDN. System.Threading.Mutex : A synchronization primitive that can also be used for interprocess synchronization. System.Threading.Semaphore : Limits the number of threads that can access a resource or pool of resources concurrently. Generally you only use a Mutex across processes, e.g. if you have a resource that multiple applications must share, or if you want to build a single-instanced app (i.e. only allow 1 copy to be running at one time). A semaphore allows