Re: [Dev-C++] how to update data constantly?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jessica C. <jes...@gm...> - 2007-12-06 16:41:55
|
Thanks, Chris.
Your way of "A commonly used technique is to have the GUI thread check the
reader
thread every X miliseconds." works fine in my project.
Do mutex in win32 API is as follows:
thread 1:
// wait_result = WaitForSingleObject(hMutex, 1000);
//
// if (wait_result == WAIT_OBJECT_0)
// {
// // data processing: data need to be copied from local to shared
area. <-----
// ReleaseMutex(hMutex);
// }
// else
// {
// // error
// }
thread 2:
// wait_result = WaitForSingleObject(hMutex, 1000);
//
// if (wait_result == WAIT_OBJECT_0)
// {
// // data processing: data need to be copied from shared data to
local area. <--------
// ReleaseMutex(hMutex);
// }
// else
// {
// // error
// }
On Dec 5, 2007 5:32 PM, Chris Miller <lor...@gm...> wrote:
> On Dec 5, 2007 1:10 PM, Jessica Chen <jes...@gm...> wrote:
> > My whole projects have two thread.
> > one thread is reading RS-232 communication port.
> > another thread is Win32 GUI to display the data got from RS-232
> > communication port.
> >
> > The design I had is:
> > Buttons on Win32 GUI.
> > So when the button is clicked, the corresponding data is read from RS232
> > thread, and display in the GUI thread.
> >
> > BUT I want to have a constantly updated display. That is, there is NO
> > button to trigger the data update.
> > When the data of RS232 thread is updated, my display of GUI thread need
> to
> > be updated.
> > What should I design?
> >
> > My consideration for the design is:
> > add a constant interval timer to trigger the data update.
> > but how to implement it?
> > and do you have any other good idea?
>
> A commonly used technique is to have the GUI thread check the reader
> thread every X miliseconds. This requires that you use a mutex to
> lock the data while it's being read, otherwise you might incur the
> wrath of undefined behavior when reading memory that's currently being
> written to.
>
> What if you had a dual-core CPU, and you managed to send a read
> request exactly when you sent a write request? Something tells me
> that's not possible because of the mutexes in the lower-level
> implementation, but it would be interesting to see which request the
> memory controller disregards.
>
>
> I'm not sure how to implement a mutex in a Win32 thread, however,
> pthreads is only a wrapper on top of Win32, so it can be done (I
> didn't notice any special code in the pthreads sources when I glossed
> over them).
>
> --
> Registered Linux Addict #431495
> http://profile.xfire.com/mrstalinman
> John 3:16!
> If Microsoft is the Wal*Mart of the Software World, then Linux is the Home
> Depot
>
|