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
0
votes
2 answers

Scala - Executing every element until they all have finished

I cannot figure out why my function invokeAll does not give out the correct output/work properly. Any solutions? (No futures or parallel collections allowed and the return type needs to be Seq[Int]) def invokeAll(work: Seq[() => Int]): Seq[Int] = { …
Lee
  • 1
  • 2
0
votes
1 answer

C program hanging when executing thread in producer-consumer problem

I'm reproducing the producer-consumer in C using semaphores from semaphore.h and threads and mutexes from pthread.h with a very simple implementation. However, when I compile and run my program I won't see any output and the process is just hanging.…
0
votes
1 answer

Cache coherence for a data race free programs

All examples I've ever seen on when cache coherence is relevant are code examples that are data races (two cores simultaneously write to the same memory location). When it comes to memory consistency, hardware vendors have decided not to provide…
0
votes
1 answer

In this synchronization is not working. Why? I have synchronized all methods. Can anyone please tell and provide solution for the same?

Write a program to print tables with the help of multiple threads create three threads named as T1, T2 and T3. T1 thread prints the table of 11 and 12, T2 prints table of 13 and 14 and T3 prints table of 15 and 16. Any thread can start first but…
0
votes
1 answer

In Java how to synchronize cache read and write operations

I am working on implementing a simple cache using ArrayList in my application. I would like to synchronize cache update operations, while updating the cache I should not allow to perform read operations. So once cache update is completed, then only…
0
votes
1 answer

test_and_set makes the thread deadlock

#include using namespace std; int cnt=0; int locked=0; int test_and_set(int * lock){ int temp=*lock; *lock=1; return temp; } void cntOnes(int t){ while(test_and_set(&locked)); for(int i=0;i<2000;i++){ …
0
votes
1 answer

What if notifyAll is called at the start of critical section

What if notifyAll is called at the start of critical section where lock is not released and released at the end of cs(critical section). What will happen to waiting threads? Do the notifyAll will get heard by all threads and all thread will try to…
0
votes
1 answer

Java Thread Cooperation with Three Threads Prints Extra Numbers

I am trying to print three AP sequences (in increments of 3) by three threads as follows: Thread-1 is printing the sequence 1, 4, 7, 10, ... Thread-2 is printing the sequence 2, 5, 8, ... Thread-3 is printing the sequence 3, 6, 9, ... A thread…
0
votes
1 answer

Order of receiving Win32 Events

I have 3 different threads that use the Windows SetEvent API to set an event each. DWORD thFunc1(LPVOID) { ... SetEvent(event1); ... } DWORD thFunc2(LPVOID) { ... SetEvent(event2); ... } DWORD thFunc3(LPVOID) { ... SetEvent(event3); …
0
votes
2 answers

Why is synchronization for count not working?

I want the final count to be 10000 always but even though I have used synchronized here, Im getting different values other than 1000. Java concurrency newbie. public class test1 { static int count = 0; public static void main(String[] args)…
0
votes
2 answers

Why does this_thread::sleep_for not reduce CPU usage of while loop

I have a while loop as follows: while(true){ //do stuff std::this_thread::sleep_for(std::chrono::milliseconds(100)); } When I look at the CPU usage it is almost 100% ... is there any way to do something like this while preserving CPU…
user491880
  • 4,709
  • 4
  • 28
  • 49
0
votes
1 answer

How the output of following method differs magically if I just remove a print statement and execute using threads

I know about synchronization, my question is not to synchronize thread. I want to ask why is the output so different if I just remove first print statement inside if condition. Suddenly else statement starts to get executed in every case. Why? class…
0
votes
0 answers

Making sure a shared resource is up-to-date

In an application I have a ?DateTime and an int field I need to access (read/write) in multiple threads. I have read up on multiple resources, from which I learned: That some threads may use outdated values of variables lock()ing such a variable…
0
votes
0 answers

OPENMP : OpenMP threads don't respect synchronization

I parallelize a Fortran code with OpenMP. I'm still debugging (there may be some remaining bugs but sequential version is OK) and the behaviour of the OpenMP threads is strange, there are threads that do not wait the others at OMP BARRIER or OMP END…
M_Guy
  • 1
  • 2
0
votes
0 answers

Wait until Class has done its work in Delphi

Hello StackOverflow community, I ran into a problem with one of my Delphi projects. Wrote a sip-phone class to call clients. Works fine so far but I need the program to wait until the call is done. thats how I start a call with client: procedure…
MorrisF
  • 11
  • 4