Claudio
Claudio

Reputation: 3

Delay the communication between two threads

I have two threads (I am using pthreads on GNU/Linux). Now they are sharing information using global variables (I know, it's not nice). At the end of each cycle they have to send the value of 5 variables (doubles) to the other thread. I would like to introduce a fixed time delay in their communication channel,

i.e.

thread1 (1kHz) <---> 10ms <---> thread2 (1kHz)

I was thinking that, at each cycle, I could create a thread which reads the value, sleep for 10ms, then forward it to the other thread and then dies. This will make the system create 2 threads every cycle, one for each direction of the communication channel (2 threads per millisecond).

Is there any other intelligent way of simulating a delay in the communication?

UPDATE: I do not want to synchronize the communication of the threads but add a delay between them. If thread1 writes something at time 1s the other thread should be able to read it only at time 1s + 10ms.

Upvotes: 0

Views: 592

Answers (4)

Anthony Giorgio
Anthony Giorgio

Reputation: 1864

Why not use a semaphore here? You could have one thread wait on the semaphore, and have the other thread signal it when it's finished with it's critical section.

Upvotes: 0

johnnycrash
johnnycrash

Reputation: 5344

This doesn't sound like a performance problem, so make it simple. No need for complex data structures, extra threads, etc.

Make a struct for your data, add a timestamp field to it.

Make an array to hold at least 10 of your structs. Go for 100 to be safe. Not going to use a lot of memory here, so who cares. Heck you could malloc it whenever it needed to grow.

Each thread has one such array, and a count of items in the array.

When a thread is ready to send data, do two things:

  1. Check the timestamp of the first item in it's array, and if old enough send that data to the other thread. After sending, decrement the count and memmove the rest of the items in the array down one notch.
  2. Add the current/new data to the end of the array with the current time stamp and increment the count.

Repeat for other thread.

Upvotes: 1

gcbenison
gcbenison

Reputation: 11963

I can't quite tell from the question whether communication between the writer and reader threads needs to be reliable (as in: all messages sent by the writer thread guaranteed to be seen by the reader thread, in the correct order). If so, and if you're using a single global structure as the shared data, you're essentially implementing a FIFO with a buffer size of 1. It's easy to get inter-thread communication wrong, so I'd recommend looking at something like glib's asynchronous queues. If you need to implement your own from scratch, looking at that code could be a good place to start.

Upvotes: 0

Attila
Attila

Reputation: 28802

Why not just use mutexes to synchronize the communication between the two threads?

When the first thread needs to write the data, it acquires the mutex (so the other thread cannot read the data while the first updates it), updates the datam, then releases the mutex. The other thread does a similar acquire/release method when it reads the data.

Upvotes: 0

Related Questions