Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, am in a MFC project in which I use two worker threads. One is for reading data from COM Port and another is for writing data to COM port.

My reading thread read com port periodically. When my writing thread need to write data to com port I have to stop reading thread and at the end of writing operation I need to start the reading thread.

I am beginner to multithreading tasks if you know explain me how can I achieve this.

Thanks in advance
ganeshIT
Posted
Updated 24-Mar-11 22:39pm
v2
Comments
CPallini 25-Mar-11 4:31am    
Usually you don't need to stop the reading operation.
[no name] 25-Mar-11 4:57am    
Indeed, if the reading and writing threads start and stop each other, the whole thing will run in sequence and the whole effort of running and synchronizing threads is wasted.

Instead, the whole art about this is to synchronize both threads. This synchronisation must be as little as possible (to get the most out of the multithreading) and as much as needed (to avoid unwanted effects). No more, no less.

You must learn how to synchronize threads. Start with this:
Introduction to Multi-threaded Code[^]
 
Share this answer
 
One technique for achieving your aims is called a semaphore.

As luck would have it there is an article about this here on The Code Project. Thread Synchronization for Beginners[^].
 
Share this answer
 
Comments
[no name] 25-Mar-11 4:51am    
Or perhaps also a Mutex?
Henry Minute 25-Mar-11 4:52am    
Indeed!
[no name] 25-Mar-11 5:02am    
I'm writing a GUI for XNA right now. There is a rendering thread and there is a UI thread. Synchronizing the Control class and its subclasses is one hell of a job. At least any errors manifest themselves as graphical UI glitches and are (literally) easily spotted :)
Henry Minute 25-Mar-11 5:07am    
Real 'Visual Programming' :)
[no name] 25-Mar-11 5:38am    
It's not always easy, but it's also a lot of fun. And a UI hovering over an animated 3D scene is something for the eye :) Actually, I may write an article about this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900