Say I have two threads P and C. I want the execution to follow the P C P C P C pattern. In order words, the execution of the two threads should alternate and no such instance of P P or C C should occur. Is that doable?
helios, my program consists of two threads (if you feel like looking at the program, it is contained in the previous question that I asked). "Producer" thread copies data from a file (say input) into a circular buffer. "Consumer" thread copies that data from the buffer into another file (say output). For every iteration, the producer copies some of the data from input and the consumer copies that data to output. However, if the "Consumer" runs twice in a row then that iteration of the Consumer thread would copy the data (contained in the buffer) from the previous iteration. Thus output file would have incomplete data.
As an example, in iteration 1, Producer copied "1 2" into buffer and "Consumer" copied "1 2" into the output file. In iteration 2, however, if "Consumer" runs first, then it would again copy "1 2" into the output file. "Producer", then, will copy "3 4 5" into the buffer. Thus output has incomplete data.
If you want only one or other of P and C to run, then I concur with helios/ne555; threads cost, so if you're only running one of them at a time it's a bit pointless. You might as well call P C P C ... from the same thread.
I am a bit unclear what the aim of the program it, aside from an execise in threading.
A high speed multi-threaded copy would solve part of your problem by double-buffering. Thread 1 fills a buffer and gives it to Thread 2 and immediately start on another buffer. The second thread process the first buffer, returning it as soon as it's finished. There will prob. be a slight wait during the buffer swapping, but that an be minimalised.
It's simple, then. All you need is to use synchronization objects. Look into mutexes and condition variables. Your program will look something like this:
You'll need some way to tell consumer_thread() that all data has been produced. Personally, I prefer using a queue of buffers, rather than a simple circular buffer. That way, I can put all the necessary information into a single object.
When you said you wanted the threads to run in ABAB order, I thought you were talking at the timeslice level. That is impossible. There's no way to tell the operating system how to schedule threads.
Fibers are what I thougt of at first too. But the reason that andywestken doesn't have experiance with them is because nobody uses Fibers, it's more trouble then it's worth and there is no gain in performance.
The way you presented your problem is what is throwing everyone off; I can tell by your requirements that you are dealing with real time input not with your first thread reading data from a static file. The generally excepted way to do this would be to have your First thread recieve the data from the connection and copy it to a shared buffer. Your second thread would use something like WaitForSingleObject() to idle while that buffer is empty. The important bit is to empty the buffer not just copy the data from it, this will prevent repeating data entries like you mentioned here.
so what you are suggesting is I synchronize the threads using semaphores, and not the Peterson's solution which my program is currently implementing. As I am just finding out, Peterson's solution only helps with the critical-section problem (i.e. the two threads don't execute at the same time), and not with the ordering of the execution of the threads.
Furthermore, would I need to implement a semaphore class in order to implement semaphores. Since semaphores are merely integers, what would be an easy implementation of accomplishing synchronization using semaphores. I search through the website and someone is working on more or less the same problem and is synchronizing his/her threads using semaphores. But his/her implementation is utterly complicated. Here's a link if you were curious. http://www.cplusplus.com/forum/general/23444/
Is there an easier way?
Use the operating system's synchronization objects, don't roll your own.
The post you linked to uses the ones available on POSIX systems, not a custom implementation (at least AFAICT).