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
5
votes
1 answer

Condition variable usage pattern in C/C++ and other languages

If you look at documentation describing the usage of condition variables (cv), you'll see that e.g. in PThreads and C++ you don't need to hold the mutex of a cv to call notify on this cv. Whereas e.g. in Java and Python, you must lock the mutex to…
5
votes
3 answers

Thread synchronization- When does a thread release the lock on an object

public class MyStack2 { private int[] values = new int[10]; private int index = 0; public synchronized void push(int x) { if (index <= 9) { values[index] = x; Thread.yield(); index++; …
sam
  • 75
  • 4
5
votes
3 answers

What is different between method synchronized vs object synchronized ?

Possible Duplicate: synchronized block vs synchronized method? If anyone can help me with real example about what is different between method synchronized vs object synchronized?, it would be nice. Method Synchronized Example public class…
R H
  • 387
  • 3
  • 13
5
votes
3 answers

How to wait on all tasks (created task and subtask) without using TaskCreationOptions.AttachedToParent

I will have to create a concurrent software which create several Task, and every Task could generate another task(that could also generate another Task, ...). I need that the call to the method which launch task is blocking: no return BEFORE all…
J4N
  • 19,480
  • 39
  • 187
  • 340
4
votes
3 answers

Java Thread Pool Synchronization

I would like to perform the following algorithm - this must be done in Java for(int i = 0; i< 100; i++){ create 8 threads which perform a task wait for all threads to finish } It is desirable that threads are not continuously created and…
ET13
  • 427
  • 6
  • 23
4
votes
1 answer

why C++ 20 does not introduce an isyncstream

C++ 20 introduces a osyncstream (https://en.cppreference.com/w/cpp/io/basic_osyncstream) to synchronize threads writing to the same stream. But why there is no isyncstream to synchronize threads reading? Does this mean that reading are always be…
Wei Li
  • 1,847
  • 2
  • 10
  • 10
4
votes
0 answers

How do "wait" and "notify" work for std::atomic in C++20?

C++20 added std::atomic-based thread synchronization methods using wait (std::atomic::wait) and notify (std::atomic::notify_one or std::atomic::notify_all) functions for them. The link…
4
votes
1 answer

Volatile vars and multi-core thread synchronization!

I have several threads executing concurrently and checking a value of a field in their own object. The field is set by the launch thread like this: for (i = 0; i < ThreadCount; i++) { ThreadContext[i].MyField = 1; } Within each thread then I…
JAM
  • 51
  • 2
4
votes
2 answers

Librarian resource allocation problem with Semaphore in Java

Please help me with this two-part question. Here is the first part: (Part 2: I have updated the code since - requirements have changed a bit.) I am trying to implement the Librarian problem in Java. The Semaphore page on Wikipedia gives the…
Tom
  • 510
  • 1
  • 7
  • 24
4
votes
1 answer

Different different threads modifying different indices of a shared list

Is it a good design to have X different threads modify X different indices of a list or basically X different threads modifying X different properties of a shared object. I considered using synchronised/synchronisedList(any concurrent datastructure)…
SHB
  • 589
  • 1
  • 6
  • 18
4
votes
2 answers

Do I need a lock if I have EventWaitHandle

Studying multi-threading from albahari atricle. Do I need a lock _locker in the sample below? I suppose no, since _message is protected by EventWaitHandle. Am I right? class TwoWaySignaling { static EventWaitHandle _ready = new AutoResetEvent…
vico
  • 17,051
  • 45
  • 159
  • 315
4
votes
3 answers

Interview Q: Java Synchronization

I encountered this questions a couple of months ago while interviewing via Skype for a German company. Given the following code: private static DateFormat DATE_FORMAT = new SimpleDateFormat(); public void doSomething() { for (int i = 0; i…
Santiago
  • 379
  • 3
  • 14
4
votes
1 answer

interest case in multithreading with volatile int

Ok. I wrote the code and get unexpected result of it and I don't know how to explain this result. Can someone help me with this? public class JMM { static volatile Boolean ready = false; static volatile int data = 0; public static void main() { …
4
votes
2 answers

Segmentation fault when creating a thread

I'm trying to implement a class that creates a thread, increments a value and sends it to another thread, which number is defined as (value * value) % number of threads #include #include #include #include…
4
votes
3 answers

Execution not switching between thread (c++11)

I am a beginner in C++11 multithreading. I am working with small codes and came into this problem. Here is the code: #include #include #include #include std::mutex print_mutex; void function1() { std::cout…
jeldikk
  • 353
  • 2
  • 12