Thread synchronization ,anyone can give me algorithm?
My task is to write a encrypted program in C.
There is a source file and a target file.My program need read content from source file, encrypt them and write encrypted content to target file.
There are 7 threads in my program.
Three threads read the source file one line by one line and put contents of file into a shared buffer 1, a thread reads contents from shared buffer 1, encrypts it and puts into a shared buffer 2, and another three threads read encrypted content from shared buffer 2 and write it into target file.
My thinking is to create two mutices to ensure that there is just one thread can read from source file or write to target file at same time. My problem is how can I control 开发者_如何转开发access by multiple threads to a shared buffer 1, shared buffer 2. I know it is quit like a Producer/consumer problem but more complex. Anyone can give me advice? Thanks in advance.
You have a very good example about mutex, shared memory and multi-threading communications in boost examples, here.
This example use a shared memory for storing messages from one tread and another one read them, blocking the shared memory for concurrent access. You can apply for your threads.
精彩评论