Questions tagged [synchronization]

Synchronization refers to using controls to maintain a coherent representation, either a group of processes running the same program (process synchronization), or representations of data (data synchronization).

In computer science, synchronization refers to one of two distinct but related concepts: synchronization of processes, and synchronization of data. Process synchronization refers to the idea that multiple processes are to join up or handshake at a certain point, so as to reach an agreement or commit to a certain sequence of action. Data synchronization refers to the idea of keeping multiple copies of a dataset in coherence with one another, or to maintain data integrity. Process synchronization primitives are commonly used to implement data synchronization.

-from Synchronization (computer science) - Wikipedia

12446 questions
185
votes
7 answers

What are the differences between various threading synchronization options in C#?

Can someone explain the difference between: lock (someobject) {} Using Mutex Using Semaphore Using Monitor Using Other .Net synchronization classes I just can't figure it out. It seems to me the first two are the same?
user38834
  • 1,859
  • 3
  • 12
  • 3
168
votes
5 answers

How to wait for all goroutines to finish without using time.Sleep?

This code selects all xml files in the same folder, as the invoked executable and asynchronously applies processing to each result in the callback method (in the example below, just the name of the file is printed out). How do I avoid using the…
Dante
  • 10,722
  • 16
  • 51
  • 63
163
votes
8 answers

What is the difference between lock and Mutex?

What is the difference between lock and Mutex? Why can't they be used interchangeably?
Ram
  • 11,404
  • 15
  • 62
  • 93
157
votes
8 answers

Conditional Variable vs Semaphore

When to use a semaphore and when to use a conditional variable?
151
votes
12 answers

C++0x has no semaphores? How to synchronize threads?

Is it true that C++0x will come without semaphores? There are already some questions on Stack Overflow regarding the use of semaphores. I use them (posix semaphores) all the time to let a thread wait for some event in another thread: void…
tauran
  • 7,986
  • 6
  • 41
  • 48
148
votes
13 answers

When should we use mutex and when should we use semaphore

When should we use mutex and when should we use semaphore ?
Karthik Balaguru
  • 7,424
  • 7
  • 48
  • 65
145
votes
25 answers

How to wait until a predicate condition becomes true in JavaScript?

I have javascript function like this: function myFunction(number) { var x=number; ... ... more initializations //here need to wait until flag==true while(flag==false) {} ... ... do something } The problem is that…
144
votes
6 answers

Are Java static initializers thread safe?

I'm using a static code block to initialize some controllers in a registry I have. My question is therefore, can I guarantee that this static code block will only absolutely be called once when the class is first loaded? I understand I cannot…
simon622
  • 3,248
  • 3
  • 19
  • 12
134
votes
8 answers

Java Singleton and Synchronization

Please clarify my queries regarding Singleton and Multithreading: What is the best way to implement Singleton in Java, in a multithreaded environment? What happens when multiple threads try to access getInstance() method at the same time? Can we…
RickDavis
  • 2,276
  • 6
  • 25
  • 31
128
votes
5 answers

How to synchronize a static variable among threads running different instances of a class in Java?

I know that using the synchronize keyword before a method brings synchronization to that object. That is, 2 threads running the same instance of the object will be synchronized. However, since the synchronization is at the object level, 2 threads…
Anonymous
  • 4,133
  • 10
  • 31
  • 38
123
votes
12 answers

fs.writeFile in a promise, asynchronous-synchronous stuff

I need some help with my code. I'm new at Node.js and have a lot of trouble with it. What I'm trying to do: Fetch a .txt with Amazon products (ASINs) ; Fetch all products using the amazon-product-api package; Save each product in a .json…
121
votes
8 answers

How do I make my ArrayList Thread-Safe? Another approach to problem in Java?

I have an ArrayList that I want to use to hold RaceCar objects that extend the Thread class as soon as they are finished executing. A class, called Race, handles this ArrayList using a callback method that the RaceCar object calls when it is…
ericso
  • 3,218
  • 7
  • 29
  • 36
120
votes
6 answers

How to keep two folders automatically synchronized?

I would like to have a synchronized copy of one folder with all its subtree. It should work automatically in this way: whenever I create, modify, or delete stuff from the original folder those changes should be automatically applied to the…
Luca Borrione
  • 16,324
  • 8
  • 52
  • 66
119
votes
4 answers

Java Synchronized Block for .class

What does this java code mean? Will it gain lock on all objects of MyClass? synchronized(MyClass.class) { //is all objects of MyClass are thread-safe now ?? } And how the above code differs from this one: synchronized(this) { //is all objects…
user249739
113
votes
9 answers

Laravel, sync() - how to sync an array and also pass additional pivot fields?

Official Laravel documentation has this on sync() function: $user->roles()->sync( array( 1, 2, 3 ) ); You may also associate other pivot table values with the given IDs: $user->roles()->sync( array( 1 => array( 'expires' => true ) ) ); In the…
Томица Кораћ
  • 2,542
  • 7
  • 35
  • 57