Reputation: 7513
If I have
1. mainThread: write data A,
2. Thread_1: read A and write it to into a Buffer;
3. Thread_2: read from the Buffer.
how to synchronize these three threads safely, with not much performance loss? Is there any existing solution to use? I use C/C++ on linux.
IMPORTANT: the goal is to know the synchronization mechanism or algorithms for this particular case, not how mutex or semaphore works.
Upvotes: 3
Views: 1388
Reputation: 6844
I would suggest Boost.Thread for this purpose. This is quite good framework with mutexes and semaphores, and it is multiplatform. Here you can find very good tutorial about this.
How exactly synchronize these threads is another problem and needs more information about your problem.
Edit The simplest solution would be to put two mutexes -- one on A
and second on Buffer
. You don't have to worry about deadlocks in this particular case. Just:
mutex_A
from MainThread
; Thread1
waits for mutex to be released.MainThread
; Thread1
enters mutex_A
and mutex_Buffer
, starts reading from A
and writes it to Buffer
.Thread1
releases both mutexes. ThreadMain
can enter mutex_A
and write data, and Thread2
can enter mutex_Buffer
safely read data from Buffer
.This is obviously the simplest solution, and probably can be improved, but without more knowledge about the problem, this is the best I can come up with.
Upvotes: 1
Reputation: 490128
First, I'd consider the possibility of building this as three separate processes, using pipes to connect them. A pipe is (in essence) a small buffer with locking handled automatically by the kernel. If you do end up using threads for this, most of your time/effort will be spent on creating nearly an exact duplicate of the pipes that are already built into the kernel.
Second, if you decide to build this all on your own anyway, I'd give serious consideration to following a similar model anyway. You don't need to be slavish about it, but I'd still think primarily in terms of a data structure to which one thread writes data, and from which another reads the data. By strong preference, all the necessary thread locking necessary would be built into that data structure, so most of the code in the thread is quite simple, reading, processing, and writing data. The main difference from using normal Unix pipes would be that in this case you can maintain the data in a more convenient format, instead of all the reading and writing being in text.
As such, what I think you're looking for is basically a thread-safe queue. With that, nearly everything else involved becomes borders on trivial (at least the threading part of it does -- the processing involved may not be, but at least building it with multiple threads isn't adding much to the complexity).
Upvotes: 4
Reputation: 4598
It's hard to say how much experience with C/C++ threads you have. I hate to just point to a link but have you read up on pthreads
?
https://computing.llnl.gov/tutorials/pthreads/
And for a shorter example with code and simple mutex'es (lock object you need to sync data):
http://students.cs.byu.edu/~cs460ta/cs460/labs/pthreads.html
Upvotes: 1