Questions tagged [thread-synchronization]

In a multi-threaded environment thread synchronization is used to coordinate access to shared resources such as file handles, network connections, and memory

In a multi-threaded environment thread synchronization is used to coordinate access to shared resources such as file handles, network connections, and memory

References

645 questions
112
votes
4 answers

Program hangs in release mode but works fine in debug mode

The code below works as expected in debug mode, completing after 500 milliseconds, but hangs indefinitely in release mode: public static void Main(string[] args) { bool isComplete = false; var t = new Thread(() => { int…
Max
  • 1,033
  • 1
  • 7
  • 7
69
votes
5 answers

What is progress and bounded waiting in critical section?

I was reading Critical Section Problem from Operating System Concepts by Peter B. Galvin. According to it 1) Progress is : If no process is executing in its critical section and some processes wish to enter their critical sections, then only those…
47
votes
3 answers

Two threads executing synchronized block simultaneously

Below is the code where a Thread enters a synchronized block, waits for 5 seconds and then exits. I have started two Thread instances simultaneously. The expectation was one of the threads will own the lock on the synchronized object, and the other…
46
votes
5 answers

What is the point of making the singleton instance volatile while using double lock?

private volatile static Singleton uniqueInstance In a singleton when using double lock method for synchronization why is the single instance declared as volatile ? Can I achieve the same functionality without declaring it as volatile ?
39
votes
3 answers

Does Thread.yield() do anything if we have enough processors to service all threads?

If we are in a situation with two running threads on a machine with two processors and we call Thread.yield() within one of those threads, does it stand to reason that nothing will happen (the scheduler will essentially ignore the request) because…
Dave
  • 15,639
  • 133
  • 442
  • 830
39
votes
3 answers

Java memory model: volatile variables and happens-before

I'd like to clarify how happens-before relation works with volatile variables. Let we have the following variables: public static int i, iDst, vDst; public static volatile int v; and thread A: i = 1; v = 2; and thread B: vDst = v; iDst = i; Are…
37
votes
3 answers

Spring @Async limit number of threads

My question is very similar to this one : @Async prevent a thread to continue until other thread have finished Basically i need run ~ hundreds of computations in more threads. I want to run only some amount of parallel threads e.g. 5 threads with 5…
Martin V.
  • 3,560
  • 6
  • 31
  • 47
35
votes
2 answers

compare and swap vs test and set

Could someone explain to me the working and differences of above operations in multi-threading?
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
28
votes
2 answers

Difference between Interlocked.Exchange and Volatile.Write?

What is the difference between Interlocked.Exchange and Volatile.Write? Both methods update value of some variable. Can someone summarize when to use each of them? Interlocked.Exchange Volatile.Write In particular I need to update double item of…
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
25
votes
3 answers

example code to show how java synchronized block works

I am learning java multi-threading, I found it's hard to understand how synchronized block works: synchronized(Object o){ // do something } please give some example code that can show me the Object o is blocked. As how I understand…
CaiNiaoCoder
  • 3,269
  • 9
  • 52
  • 82
22
votes
5 answers

Simultaneous mutable access to arbitrary indices of a large vector that are guaranteed to be disjoint

Context I have a case where multiple threads must update objects stored in a shared vector. However, the vector is very large, and the number of elements to update is relatively small. Problem In a minimal example, the set of elements to update can…
Thierry
  • 1,099
  • 9
  • 19
20
votes
3 answers

Synchronization mechanism for an observable object

Let's imagine we have to synchronize read/write access to shared resources. Multiple threads will access that resource both in read and writing (most of times for reading, sometimes for writing). Let's assume also that each write will always trigger…
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
19
votes
8 answers

How Synchronization works in Java?

I have a doubt regarding Java Synchronization . I want to know if I have three Synchronized methods in my class and a thread acquires lock in one synchronized method other two will be locked ? I am asking this question because I am confused with the…
Raj
  • 2,463
  • 10
  • 36
  • 52
17
votes
6 answers

What is the difference between Thread.join and Synchronized?

I am confused when to use Thread.join() and when to use synchronization in multi threading application. According to me, both of them block or wait for the execution to be done by some other thread. This example has to output 10 A's , 10 B's & 10…
JPG
  • 1,247
  • 5
  • 31
  • 64
17
votes
2 answers

understanding of Volatile.Read/Write

I'm trying to understand the C# Volatile class. As i read: The Volatile.Write method forces the value in location to be written to at the point of the call. In addition, any earlier program-order loads and stores must occur before the call to…
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
1
2 3
42 43