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
15
votes
3 answers

ReleaseMutex : Object synchronization method was called from an unsynchronized block of code

I have this pretty straightforward piece of code that very rarely throws "System.ApplicationException : Object synchronization method was called from an unsynchronized block of code." when ReleaseMutex() is called. I logically analyzed the flow of…
12
votes
2 answers

std::timed_mutex::try_lock* fail spuriously

By try_lock*, I take to mean try_lock(), try_lock_for(), and try_lock_until(). According to cppreference, all three methods may just fail spuriously. Following is quoted from the description for try_lock_for() As with try_lock(), this function is…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
11
votes
9 answers

Difference Between Monitor & Lock?

What's the difference between a monitor and a lock? If a lock is simply an implementation of mutual exclusion, then is a monitor simply a way of making use of the waiting time inbetween method executions? A good explanation would be really helpful…
Goober
  • 13,146
  • 50
  • 126
  • 195
11
votes
10 answers

How to fix race conditions without using synchronized (Lock free sequence counter implementation)?

Have a scenario where multiple threads have race condition on comparison code. private int volatile maxValue; private AtomicInteger currentValue; public void constructor() { this.current = new AtomicInteger(getNewValue()); } public…
s7vr
  • 73,656
  • 11
  • 106
  • 127
11
votes
6 answers

Alternative for synchronized block in java

I use following code for guarantee startTime variable set once only: public class Processor { private Date startTime; public void doProcess() { if(startTime == null) synchronized(this) { …
10
votes
1 answer

Is Kotlin `?.let` thread-safe?

Is Kotlin ?.let thread-safe? Let's say a variable can be changed in different thread. Is using a?.let { /* */ } thread-safe? If it's equal to if (a != null) { block() } can it happen that in if it's not null and in block it's already null?
4ntoine
  • 19,816
  • 21
  • 96
  • 220
10
votes
2 answers

Implementing a synchronization barrier in Ruby

I'm trying to "replicate" the behaviour of CUDA's __synchtreads() function in Ruby. Specifically, I have a set of N threads that need to execute some code, then all wait on each other at mid-point in execution before continuing with the rest of…
user2398029
  • 6,699
  • 8
  • 48
  • 80
9
votes
1 answer

How can asyncio ever not be thread safe considering the GIL?

The asyncio docs read: Most asyncio objects are not thread safe. You should only worry if you access objects outside the event loop. Could someone explain this or give an example of how misuse of asyncio can cause an unsynchronized write to an…
9
votes
3 answers

Do I need to synchronize access to a List that is only modified by one thread?

Here I have a class that has two threads that have access to a List. One thread periodically replaces the list with an updated copy, and the other thread paints the list's contents onto the screen. public class ThreadSafePainter { private…
Will
  • 171
  • 1
  • 6
9
votes
3 answers

How fast is an atomic/interlocked variable compared to a lock, with or without contention?

And how much faster/slower it is as compared to an uncontested atomic variable (such as std::atomic of C++) operation. Also, how much slower are contested atomic variables relative to the uncontested lock? The architecture I'm working on is…
pythonic
  • 20,589
  • 43
  • 136
  • 219
8
votes
2 answers

Kotlin concurrency for ConcurrentHashMap

I am trying to support concurrency on a hashmap that gets periodically cleared. I have a cache that stores data for a period of time. After every 5 minutes, the data in this cache is sent to the server. Once I flush, I want to clear the cache. The…
8
votes
2 answers

What is the main difference between CRCW and EREW in PRAM model?

In the PRAM model, multiple processors act synchronously to execute the same command on different sets of data. There are two types of read/write mode for each algorithm; Concurrent (Concurrent Read & Concurrent Write) Exclusive (Exclusive Read &…
8
votes
3 answers

How many ABA tag bits are needed in lock-free data structures?

One popular solution to the ABA problem in lock-free data structures is to tag pointers with an additional monotonically incrementing tag. struct aba { void *ptr; uint32_t tag; }; However, this approach has a problem. It is really…
8
votes
6 answers

Do multiple threads Using the same object in java make a copy of it?

How do multiple threads in Java handle working with a single object reference passed to them? Do they make a copy of the object and then use it, or do they use the same one? Any technical explanation is welcome. I am unable to comprehend this in…
user7494723
8
votes
7 answers

How to prevent two instances of an application from doing the same thing at the same time?

If you have two threads within an application, and you don't want them to run a certain piece of code simultaneously, you can just put a lock around the piece of code, like this: lock (someObject) { // ... some code } But how do you do the same…
Timwi
  • 65,159
  • 33
  • 165
  • 230
1
2
3
42 43