synchronization

what is correspoding feature for synchronized in java?

谁都会走 提交于 2019-11-28 18:09:51
synchronized in Java can guarantee safety of thread. What about C++ ? Thank you! ybungalobill Use the following in C++11: mutex _mutex; void f() { unique_lock<mutex> lock(_mutex); // access your resource here. } Use boost if you don't have a C++11 compiler yet. Akira Despite this question has been already answered, by the idea of this article I made my version of synchronized keyword using just standard library (C++11) objects: #include <mutex> #define synchronized(m) \ for(std::unique_lock<std::recursive_mutex> lk(m); lk; lk.unlock()) You can test it like: #include <iostream> #include

Atomic Operations and multithreading

寵の児 提交于 2019-11-28 17:52:11
Recently I was reading a tutorial, in that I came across a statement that says.. "The Java language specification guarantees that reading or writing a variable is an atomic operation(unless the variable is of type long or double ). Operations variables of type long or double are only atomic if they declared with the volatile keyword." AtomicInteger or AtomicLong that provides methods like getAndDecrement() , getAndIncrement() and getAndSet() which are atomic. I got confused a bit with the above statement.. could you please clarify when to use AtomicInteger or AtomicLong classes. JB Nizet Doing

Is mutex needed to synchronize a simple flag between pthreads?

萝らか妹 提交于 2019-11-28 17:41:33
问题 Let's imagine that I have a few worker threads such as follows: while (1) { do_something(); if (flag_isset()) do_something_else(); } We have a couple of helper functions for checking and setting a flag: void flag_set() { global_flag = 1; } void flag_clear() { global_flag = 0; } int flag_isset() { return global_flag; } Thus the threads keep calling do_something() in a busy-loop and in case some other thread sets global_flag the thread also calls do_something_else() (which could for example

wait and notify in C/C++ shared memory

我怕爱的太早我们不能终老 提交于 2019-11-28 17:28:24
How to wait and notify like in Java In C/C++ for shared memory between two or more thread?I use pthread library. Instead of the Java object that you would use to wait/notify, you need two objects: a mutex and a condition variable. These are initialized with pthread_mutex_init and pthread_cond_init . Where you would have synchronized on the Java object, use pthread_mutex_lock and pthread_mutex_unlock (note that in C you have to pair these yourself manually). If you don't need to wait/notify, just lock/unlock, then you don't need the condition variable, just the mutex. Bear in mind that mutexes

Synchronising twice on the same object?

谁说我不能喝 提交于 2019-11-28 17:25:05
I was wondering if in Java I would get any odd behaviour if I synchronise twice on the same object? The scenario is as follows pulbic class SillyClassName { object moo; ... public void method1(){ synchronized(moo) { .... method2(); .... } } public void method2(){ synchronized(moo) { doStuff(); } } } Both methods use the object and are synchronised on it. Will the second method when called by the first method stop because it's locked? I don't think so because it's the same thread but I'm unsure of any other odd results that might occur. Leigh Reentrant Synchronized blocks use reentrant locks,

Does a multiple producer single consumer lock-free queue exist for c++? [closed]

最后都变了- 提交于 2019-11-28 16:43:26
The more I read the more confused I become... I would have thought it trivial to find a formally correct mpsc queue implemented in c++. Every time I find another stab at it, further research seems to suggest there are issues such as ABA or other subtle race conditions. Many talk of the need for garbage collection. This is something I want to avoid. Is there an accepted correct open source implementation out there? bronekk You may want to check disruptor; it's available in C++ here: http://lmax-exchange.github.io/disruptor/ You can also find explanation how it works here on stackoverflow

Automatically keep a secondary repo in sync with a primary repo?

╄→гoц情女王★ 提交于 2019-11-28 16:20:03
问题 We have a two tier setup. We have a primary repository (called 'primary' below). And a secondary repository (called 'secondary' below) that was created like so: $ git clone --bare --shared $REPO_A/primary secondary.git People working on the secondary repository view the branches which originated from the primary repository as read only but base their own branches off these branches. We want to sync up the secondary repository with the primary repository once a day. I.e. we want commits and

Behavior differences between performBlock: and performBlockAndWait:?

て烟熏妆下的殇ゞ 提交于 2019-11-28 16:07:07
I'm creating an NSManagedObjectContext in a private queue to handle data updates I take from files and/or services: NSManagedObjectContext *privateContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; privateContext.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator; Since I'm using a private queue, I don't fully understand the difference between performBlock: and performBlockAndWait: methods... To perform my data updates I'm currently doing this:

What is the difference between lock and Mutex?

风格不统一 提交于 2019-11-28 15:37:06
What is the difference between lock and Mutex? Why can't they be used interchangeably? Darin Dimitrov A lock is specific to the AppDomain, while Mutex to the Operating System allowing you to perform inter-process locking and synchronization (IPC). lock is a compiler keyword, not an actual class or object. It's a wrapper around the functionality of the Monitor class and is designed to make the Monitor easier to work with for the common case. The Monitor (and the lock keyword) are, as Darin said, restricted to the AppDomain . Primarily because a reference to a memory address (in the form of an

pthreads mutex vs semaphore

匆匆过客 提交于 2019-11-28 15:22:42
What is the difference between semaphores and mutex provided by pthread library ? semaphores have a synchronized counter and mutex's are just binary (true / false). A semaphore is often used as a definitive mechanism for answering how many elements of a resource are in use -- e.g., an object that represents n worker threads might use a semaphore to count how many worker threads are available. Truth is you can represent a semaphore by an INT that is synchronized by a mutex. Paxi I am going to talk about Mutex vs Binary-Semaphore. You obviously use mutex to prevent data in one thread from being