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

Threads producer and consumer while loop

void producer(char* line){ /* Produtor */ pthread_mutex_lock(&buffer_mutex); while (occupied >= MAX_COMMANDS){ printf("produzi wait\n"); pthread_cond_wait(&less, &buffer_mutex); } printf("insere no…
0
votes
1 answer

Is this the correct way to use pthread?

I am trying to create an HTTP server using multi-threading. main() hands off the client_sock from accept() to one of the worker threads. If no worker threads are available, it waits until one is. I am restricted to not being able to call accept()…
Justin
  • 170
  • 8
0
votes
1 answer

Thread sync for creating word documents

I have a Word template file in which I replace certain tags with my data. I tried to synchronise file creating with Parallel, but as you can see on the picture, after a while I get this File in use window. I looked it up on the internet and I think…
Qwadrox
  • 611
  • 1
  • 6
  • 10
0
votes
2 answers

bounded 'mutex pools' for synchronized entity behaviour

I have a function type Command struct { id Uuid } handleCommand(cmd Command) { entity := lookupEntityInDataBase(cmd.Uuid) entity.handleCommand(cmd) saveEntityInDatabase(entity) } However this function can be called in parallel, and the…
hbogert
  • 4,198
  • 5
  • 24
  • 38
0
votes
1 answer

Why is the code just after wait method not invoked

I have a customer account class with below 2 methods, addBalance and deductBalance. class CustomerAccount { private int balance; int getBalance() { return balance;} CustomerAccount() {balance=0;} boolean deductBalance(final int…
0
votes
0 answers

How to use Google guava cache library to for key alone

I am using Java 11 for my application. I want to store the log lines in Google guava cache with limit as 5000 and write it to file after the limit is reached with synchronized. CacheLoader used key value pair. I don't have both. I have only one log…
Galet
  • 5,853
  • 21
  • 82
  • 148
0
votes
1 answer

Java Multi-threading Deadlock or Starvation?

I am learning for my OCP exam and are trying to understand when we are speaking of a thread deadlock or a thread is in starvation. in case of the scenario below I have doubt. public class ThreadTest { private static int i = 0; public static…
0
votes
1 answer

Multithreading with an unpredictable tree structure

Software written in C++, must use only Standard C++ library. Hi, the problem i'm facing is the following: I have to parallelize a software but the multithreaded version completion time is too much randomly, i mean that 50% of the times is faster…
0
votes
2 answers

Does the volatile keyword guarantee the latest value to be read by a reader thread? If not, what is its use?

Please refer to the following codes. Shared.java class Shared { public volatile int i = 0; } CreateThread.java class CreateThread { public static void main(String[] args) throws InterruptedException { Shared s = new Shared(); …
my name is GYAN
  • 1,269
  • 13
  • 27
0
votes
1 answer

Significance of use of keyword synchronized in the following code

I was reading multi threading in Java from the book Java The Complete Reference by Herbert Schildt. I came across following code [Pg. 252, 7th ed.] that explained the usage of wait() and notify() to suspend and resume threads in modern Java. My…
ATK
  • 358
  • 3
  • 17
0
votes
1 answer

How to count incoming messages from a "stream" in Java?

So I'm receiving numerous messages from a stream - well, it's not a stream per se, it's really a method that's fired off when a message is received - and I would like to count the number of messages received in 10 seconds. Here's what I have so…
Saturnian
  • 1,686
  • 6
  • 39
  • 65
0
votes
1 answer

C++ how one thread can communicate properly that its task is finished?

I'm new to C++ and to threads too. So I'm pretty confused. I'm trying to write a class that wraps a generic queue and provides two methods: push and pop. (thread safe) The push method will use a lock_guard and push the elem received as param into…
0
votes
0 answers

Mutex deadlock in with lock owner set to 0 in both the process

I am using a recursive mutex which is defined in shared memory for synchronization between two processes. I am seeing a deadlock between two processes but when I debugged core file, I found out that neither process A nor process B is owning the…
0
votes
0 answers

How to synchronize processes using semaphores initialized at 1?

I have a problem synchronizing processes using semaphores that are initialized at 1. I have the following example: There are six processes. Process 1 can start any time. Process 2 must wait for process 1 to finish. Process 3 can start any…
0
votes
1 answer

Synchronize array over MPI processes: incorrect use of MPI_Allgather?

I am aware similar questions have been addressed previously, see below why they don't apply to my case. I have a piece of code that looks as follows: int current_rank; MPI_Comm_rank(MPI_COMM_WORLD, ¤t_rank); if (current_rank==ROOT_RANK){ …
Suyama87
  • 85
  • 10