I am now dealing with multithreaded programming.
one thread is RS232, another one is User Interface.
The structure will be:
User interface Thread ------> global
variable(s)----> Worker thread
Worker Thread ------> custom window message(s)
----> User interface Thread
I don't know how to implement custom window messages!
PostMessage, GetMessage, DispatchMessage?
=====================================
REFERENCES:
Now let's move to the communication methods between threads.
There are three of them:
* Using global variables
* Windows messages
* Event
Threads share the process's resources including global variables so the
threads can use global varibles to communicate with each other. However this
method must be used with care. Thread synchronization must enter into
consideration. For example, if two threads use the same structure of 10
members , what happens when Windows suddenly yanks the control from one of
the thread when it was in the middle of updating the structure? The other
thread will be left with an inconsistent data in the structure! Don't make
any mistake, multithreading programs are harder to debug and maintain. This
sort of bug seems to happen at random which is very hard to track down.
You can also use Windows messages to communicate between threads. If the
threads are all user interface ones, there's no problem: this method can be
used as a two-way communication. All you have to do is defining one or more
custom windows messages that are meaningful to the threads. You define a
custom message by using WM_USER message as the base value say , you can
define it like this:
WM_MYCUSTOMMSG equ WM_USER+100h
Windows will not use any value from WM_USER upward for its own messages so
you can use the value WM_USER and above as your own custom message value.
If one of the thread is a user interface thread and the other is a worker
one, you cannot use this method as two-way communication since a worker
thread doesn't have its own window so it doesn't have a message queue. You
can use the following scheme:
User interface Thread ------> global
variable(s)----> Worker thread
Worker Thread ------> custom window message(s)
----> User interface Thread
In fact, we will use this method in our example.
The last communication method is an event object. You can view an event
object as a kind of flag. If the event object is in "unsignalled" state, the
thread is dormant or sleeping, in this state, the thread doesn't receive CPU
time slice. When the event object is in "signalled" state,Windows "wakes up"
the thread and it starts performing the assigned task.
Worker Thread ------> custom window message(s)
----> User interface Thread User interface Thread ------> global
variable(s)----> Worker thread
Worker Thread ------> custom window message(s)
----> User interface Thread
|