std::mutex performance compared to win32 CRITICAL_SECTION

Please see my updates at the end of the answer, the situation has dramatically changed since Visual Studio 2015. The original answer is below. I made a very simple test and according to my measurements the std::mutex is around 50-70x slower than CRITICAL_SECTION. std::mutex: 18140574us CRITICAL_SECTION: 296874us Edit: After some more tests it turned out … Read more

WaitForSingleObject and WaitForMultipleObjects equivalent in Linux?

Stick to pthread_cond_timedwait and use clock_gettime. For example: struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += 10; // ten seconds while (!some_condition && ret == 0) ret = pthread_cond_timedwait(&cond, &mutex, &ts); Wrap it in a function if you wish. UPDATE: complementing the answer based on our comments. POSIX doesn’t have a single API to wait for … Read more

Should you synchronize the run method? Why or why not?

Synchronizing the run() method of a Runnable is completely pointless unless you want to share the Runnable among multiple threads and you want to sequentialize the execution of those threads. Which is basically a contradiction in terms. There is in theory another much more complicated scenario in which you might want to synchronize the run() … Read more

When is a condition variable needed, isn’t a mutex enough?

Even though you can use them in the way you describe, mutexes weren’t designed for use as a notification/synchronization mechanism. They are meant to provide mutually exclusive access to a shared resource. Using mutexes to signal a condition is awkward and I suppose would look something like this (where Thread1 is signaled by Thread2): Thread1: … Read more

Is there a synchronization class that guarantee FIFO order in C#?

You’ll need to write your own class to do this, I found this example (pasted because it looks as though the site’s domain has lapsed): using System.Threading; public sealed class QueuedLock { private object innerLock; private volatile int ticketsCount = 0; private volatile int ticketToRide = 1; public QueuedLock() { innerLock = new Object(); } … Read more

How to make a multiple-read/single-write lock from more basic synchronization primitives?

At first glance I thought I recognized this answer as the same algorithm that Alexander Terekhov introduced. But after studying it I believe that it is flawed. It is possible for two writers to simultaneously wait on m_exclusive_cond. When one of those writers wakes and obtains the exclusive lock, it will set exclusive_waiting_blocked = false … Read more

techhipbettruvabetnorabahisbahis forumu