synchronization

Intel Inspector reports a data race in my spinlock implementation

蓝咒 提交于 2019-11-30 09:16:54
I made a very simple spinlock using the Interlocked functions in Windows and tested it on a dual-core CPU (two threads that increment a variable); The program seems to work OK (it gives the same result every time, which is not the case when no synchronization is used), but Intel Parallel Inspector says that there is a race condition at value += j (see the code below). The warning disappears when using Critical Sections instead of my SpinLock. Is my implementation of SpinLock correct or not ? It's really strange, because all the used operations are atomic and have the proper memory barriers and

When to use synchronized in Java

懵懂的女人 提交于 2019-11-30 09:14:13
I hope this is going to be enough information, so here it goes. If you need more info, lemme know in the comments. I have a class that has two inner classes. The inner classes each have two methods that call a method in the outer class. So, it looks like this: public OuterClass { private boolean outerMethodHasBeenCalled = false; private void outerMethod() { if(!outerMethodHasBeenCalled) { // do stuff } outerMethodHasBeenCalled = true; } private FirstInnerClass { public void someMethod() { outerMethod(); } } private SecondInnerClass { public void someOtherMethod() { outerMethod(); } } } It's

Windows Event implementation in Linux using conditional variables?

…衆ロ難τιáo~ 提交于 2019-11-30 09:07:50
I am trying to implement very simple Windows events in Linux. Only for my scenario - 3 threads, 1 main and 2 secondary. Each of secondary threads raise 1 event by SetEvent and main thread wait it. Example: int main() { void* Events[2]; Events[0] = CreateEvent(); Events[1] = CreateEvent(); pthread_start(Thread, Events[0]); pthread_start(Thread, Events[1]); WaitForMultipleObjects(2, Events, 30000) // 30 seconds timeout return 0; } int* thread(void* Event) { // Do something SetEvent(Event); // Do something } So, to implement it, i use conditional variables. But my question is - is this a right

Atomic Instruction

天大地大妈咪最大 提交于 2019-11-30 09:02:17
问题 What do you mean by Atomic instructions? How does the following become Atomic? TestAndSet int TestAndSet(int *x){ register int temp = *x; *x = 1; return temp; } From a software perspective, if one does not want to use non-blocking synchronization primitives, how can one ensure Atomicity of instruction? is it possible only at Hardware or some assembly level directive optimization can be used? 回答1: Some machine instructions are intrinsically atomic - for example, reading and writing properly

Strategy on synchronizing database from multiple locations to a central database and vice versa

a 夏天 提交于 2019-11-30 08:39:23
I have several databases located in different locations and a central database which is in a data center. All have the same schema. All of them are changed(insert/update/delete) in each location with different data including the central database. I would like to synchronise all the data in the central database. I would also like all data in the central database synchronise to all locations. What I mean is that database change in location 1 should also be reflected in location 2 database. Any ideas on how to go about this? Just look at SymmetricDS . It is a data replication software that

AutoResetEvent, ManualResetEvent vs Monitor

若如初见. 提交于 2019-11-30 08:26:17
Lets say I have to orchestrate a synchronization algorithm in .Net 3.5 SP1 and any of the synchronization primitives listed in the title fit perfectly for the task. From a performance perspective, is any single one of those more performant than the others? I ask this because I have been coding for a while now, but without proper knowledge on the subject. If you can, go with Monitor. It's similar to a CRITICAL_SECTION. AutoResetEvent/ManualResetEvent might have slightly more overhead, since these can be shared by different processes, whereas a Monitor belongs to a single process. WaitHandles

Why did Java and C# add intrinsic lock to every object?

谁说胖子不能爱 提交于 2019-11-30 08:17:31
Making every object lockable looks like a design mistake: You add extra cost for every object created, even though you'll actually use it only in a tiny fraction of the objects. Lock usage become implicit, having lockMap.get(key).lock() is more readable than synchronization on arbitrary objects, eg, synchronize (key) {...} . Synchronized methods can cause subtle error of users locking the object with the synchronized methods You can be sure that when passing an object to a 3rd parting API, it's lock is not being used. eg class Syncer { synchronized void foo(){} } ... Syncer s = new Syncer();

Recursive / nested locking in C# with the lock statement [duplicate]

♀尐吖头ヾ 提交于 2019-11-30 07:56:47
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Re-entrant locks in C# I've looked here on StackOverflow and on MSDN, and can't believe that I couldn't find this question lingering out there on the internets. Let's say I have a class with a private member that I want to access in several public methods. These public methods will be called by different threads, hence the need for synchronization. public class MyClass { private Object SomeSharedData = new

What is condition synchronization?

混江龙づ霸主 提交于 2019-11-30 07:49:46
Could someone explain condition synchronization to me? An example (preferably in C#) would be greatly appreciated also. It sounds like your professor is talking about threading. Threading enables computer programs to do more than one thing at a time. The act of starting a new thread while one is already running is called "spinning up a thread" by computer programmers. Threads can share the same memory space. Condition Synchronization (or merely synchronization) is any mechanism that protects areas of memory from being modified by two different threads at the same time. Let's say you are out

What are synchronizing strategies for Prevayler?

人盡茶涼 提交于 2019-11-30 07:39:25
问题 Prevayler guarantees that all the writes ( through its transactions) are synchronized. But what about reads? Is it right that dirty reads are possible if no explicit synchronizing is used (in user code)? Are they possible if a business object is read as: // get the 3rd account Accont account = (Bank)prevayler.prevalentSystem().getAccounts().get(2); ? If so what synchronizing strategies are good for a user code? (Consider a business object A contains a collection of business objects Bs), using