Click here to Skip to main content
16,005,038 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: thread communications [modified] Pin
alberthyc10-Aug-07 10:55
alberthyc10-Aug-07 10:55 
GeneralRe: thread communications Pin
Mark Salsbery10-Aug-07 11:06
Mark Salsbery10-Aug-07 11:06 
GeneralRe: thread communications Pin
alberthyc10-Aug-07 11:17
alberthyc10-Aug-07 11:17 
GeneralRe: thread communications Pin
Mark Salsbery10-Aug-07 11:55
Mark Salsbery10-Aug-07 11:55 
GeneralRe: thread communications Pin
alberthyc10-Aug-07 16:04
alberthyc10-Aug-07 16:04 
GeneralRe: thread communications Pin
T-Mac-Oz10-Aug-07 16:44
T-Mac-Oz10-Aug-07 16:44 
GeneralRe: thread communications Pin
alberthyc11-Aug-07 8:01
alberthyc11-Aug-07 8:01 
AnswerRe: thread communications Pin
T-Mac-Oz11-Aug-07 19:37
T-Mac-Oz11-Aug-07 19:37 
Hi Albert,

After having another look at your original posted code, it seems you are using threads to perform what is essentially a sequential task!WTF | :WTF:

Worker thread posts WM_USER_GET_DATA
Main thread receives WM_USER_GET_DATA*
Main thread calculates output
Main thread signals "output" event
Worker thread receives "output" event
Worker thread outputs data
Worker thread posts WM_USER_GET_DATA
Main thread receives WM_USER_GET_DATA*
Main thread calculates output
Main thread signal "output" event
Worker thread receives ...

* Yes, the main thread is processing other messages but given that the WM_USER_GET_DATA is sent to the main thread using PostMessage(...), the worker thread ends up having to wait on processing of other messages before its WM_USER_GET_DATA message is processed anyway!

A much cleaner solution that accomplishes exactly the same thing is:
// CMy61xxMTDlg.h:

//define user message
#define WM_USER_GET_DATA    WM_USER+0x100

class CMy61xxMTDlg : public CDialog
{
    .....
    bool    m_bDoOutput; // assign false in constructor or OnInitDialog
    afx_msg LRESULT OnGetData(WPARAM wParam, LPARAM lParam);
    afx_msg LRESULT OnUserStop(WPARAM wParam, LPARAM lParam);
    DECLARE_MESSAGE_MAP()
    .....

    void OnUserGetData();
    void OnBnClickedButtonStart()
    {
        m_bDoOutput = true;
        PostMessage(WM_USER_GET_DATA);
    };
    void OnBnClickedButtonStop() { m_bDoOutput = false; };
    .....
}


// CMy61xxMTDlg.cpp:
......
BEGIN_MESSAGE_MAP(CMy61xxMTDlg, CDialog)
......
    //user defined message handler
    ON_MESSAGE (WM_USER_GET_DATA, OnUserGetData)
......
END_MESSAGE_MAP()

void CMy61xxMTDlg::OnUserGetData()
{
    if (!m_bDoOutput)
        return;
    ptr8 pbyData;
    for (i=0; i<BUFSIZE; i ++)
    {            
        pbyData[i]=(int8)m_Slider_Data[i].GetPos();
    }
    SpcSetParam(...);
    PostMessage(WM_USER_GET_DATA);
}


Threads are great for their intended purpose, which is to permit semi-independent operations to execute in parallel.
The tight looping and total dependence on event signals should have tipped me off right away that what you were trying to achieve was not really parallel processing at all.
As you've already found out, threads and thread synchronisation are tricky beasts, so it's always worth exploring alternative implementations (such as timers and/or user message events) before resorting to a multi-threaded solution to a problem.
Threading sounds cool & it is cool but don't leap into trying to use threads just because you think you can, you will most often come up with a sub-optimal solution. Cool | :cool:

BTW. I do understand the FIFO concept & that is precisely why using a timer is a better idea than continually looping in a background thread (or perhaps even the self-propagating event solution above).
If, as you say, SpcGetParam(...) adds the supplied data in the on-board hardware buffer (i.e. the end of the FIFO queue), then a system running at 2Gz (forget FSB, the small volume of data we're talking about here can almost be guaranteed to always be cached), with the other minimal processing involved, feeding a FIFO queue being read at 100MHz will sooner or later (probably sooner) fill & overflow (or maybe wrap around, or block, or drop values, depends on the firmware) the hardware buffer. Using a timer gives you the opportunity to match your output frequency to the oscilloscope frequency. If the standard system timer resolution is not fine-grained enough for you, there are other timers available in windows, search Code Project for "timers" & you'll find plenty of articles. If you insist on using threads, protecting access to m_pbyData[] as a member of CMy61xxMTDlg is easily accomplished using mutexes.

Regards,



T-Mac-Oz

AnswerRe: thread communications Pin
David Crow10-Aug-07 10:50
David Crow10-Aug-07 10:50 
QuestionDisplaying an Image(JPG,GIF) etc Pin
simon alec smith10-Aug-07 9:30
simon alec smith10-Aug-07 9:30 
AnswerRe: Displaying an Image(JPG,GIF) etc Pin
Mark Salsbery10-Aug-07 9:41
Mark Salsbery10-Aug-07 9:41 
GeneralRe: Displaying an Image(JPG,GIF) etc Pin
simon alec smith10-Aug-07 10:02
simon alec smith10-Aug-07 10:02 
QuestionRe: Displaying an Image(JPG,GIF) etc Pin
Mark Salsbery10-Aug-07 10:18
Mark Salsbery10-Aug-07 10:18 
AnswerRe: Displaying an Image(JPG,GIF) etc Pin
simon alec smith11-Aug-07 2:53
simon alec smith11-Aug-07 2:53 
GeneralRe: Displaying an Image(JPG,GIF) etc Pin
Mark Salsbery11-Aug-07 8:59
Mark Salsbery11-Aug-07 8:59 
AnswerRe: Displaying an Image(JPG,GIF) etc Pin
Hamid_RT10-Aug-07 19:00
Hamid_RT10-Aug-07 19:00 
GeneralRe: Displaying an Image(JPG,GIF) etc Pin
simon alec smith11-Aug-07 3:38
simon alec smith11-Aug-07 3:38 
QuestionHow to print from .bmp file image in memory Pin
bions10-Aug-07 9:03
bions10-Aug-07 9:03 
AnswerRe: How to print from .bmp file image in memory Pin
Mark Salsbery10-Aug-07 9:37
Mark Salsbery10-Aug-07 9:37 
AnswerRe: How to print from .bmp file image in memory Pin
bions13-Aug-07 7:58
bions13-Aug-07 7:58 
GeneralRe: How to print from .bmp file image in memory Pin
Mark Salsbery13-Aug-07 10:02
Mark Salsbery13-Aug-07 10:02 
Questionneed quick help Pin
klutez12310-Aug-07 8:15
klutez12310-Aug-07 8:15 
AnswerRe: need quick help Pin
Mark Salsbery10-Aug-07 8:18
Mark Salsbery10-Aug-07 8:18 
JokeRe: need quick help Pin
David Crow10-Aug-07 8:48
David Crow10-Aug-07 8:48 
GeneralRe: need quick help Pin
Mark Salsbery10-Aug-07 10:01
Mark Salsbery10-Aug-07 10:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.