Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Non-Global Threads

Posted on 1999-07-21
9
Medium Priority
?
257 Views
Last Modified: 2013-11-20
I'm trying to get some threads to work in my program, and I'm successful if I make the thread a global function. But if I do this, I'm not able to call any functions within the classes I've defined. What is the best way to tackle this situation? What I want specifically is a thread that is monitoring the serial port for incoming data. When data is present, I want to run a function (CMyGUI::ParseData()). I also want to create additional threads that open modeless dialog boxes and preform various task, such as one box that will display verbatium, everything in the serial port buffer.

Help please.
0
Comment
Question by:DBH21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1310877
In the thread function, you are allowed a 32bit value as the parameter. You're probably not using it. In this case, you can pass the this pointer from your class into the thread function and have the thread function cast the 32bit value back to the class pointer.

If you are using the 32bit value for something, then you'll need to create a temporary structure that contains two 32bit values: one for the current 32bit value and another for the class pointer. Then pass the address of this structure to the thread function. In the thread function, cast the 32bit function parameter into structure pointer and from there you have access to the old 32bit value you were using and the class pointer.
0
 

Author Comment

by:DBH21
ID: 1310878
The Real problem comes when I try to call:

m_pThread = AfxBeginThread(ComWatchProc, (LPVOID)
                           &CodexData.Serial.m_ThreadInfo);
This works if ComWatchProc is a global variable. But I want it inside my CSerial class so that it can run some common routines. If I replace ComWatchProc above with CSerial::ComWatchProc or Serial.ComWatchProc, I get a slew of errors... I'm lost
0
 

Author Comment

by:DBH21
ID: 1310879
Not sufficient information
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 23

Expert Comment

by:chensu
ID: 1310880
This is a common mistake. The thread controlling function must be either a global function or a static member function of a class. A non-static member function has an implicit parameter, which is the this pointer. Thus, it does not comply the prototype required by AfxBeginThread.
0
 

Author Comment

by:DBH21
ID: 1310881
Here's what I've done.

CSerial::CSerial...
...
UINT CSerial::ComWatchProc(LPVOID lpParam)
{
  ....//I'd like to make calls to CSerial::ReadData() and
        other functions here
}

I then initilize the thread in the InitDialog()
{
...

m_pThread = AfxBeginThread(CodexData.Serial.ComWatchProc,   (LPVOID) &CodexData.Serial.m_ThreadInfo);
}

If I declare ComWatchProc as static, everything is fine, until I try to access another function of CSerial from within ComWatchProc().

0
 
LVL 23

Expert Comment

by:chensu
ID: 1310882
m_pThread = AfxBeginThread(CodexData.Serial.ComWatchProc,   (LPVOID) &CodexData.Serial);


UINT CSerial::ComWatchProc(LPVOID lpParam)
{
    //I'd like to make calls to CSerial::ReadData() and
    CSerial *ps = (CSerial *)lpParam;
    ps->ReadData();
}
0
 
LVL 1

Accepted Solution

by:
jclanz earned 400 total points
ID: 1310883
hello,
the first answer from thui is right. peraps you need a sample like this.

class CMyClass
{
public:
  static UINT StaticComWatchProc(LPVOID lpParam);
  UINT ComWatchProc(); // member of this class
};

UINT CMyClass::StaticComWatchProc(LPVOID lpParam)
{
  // get the "this" pointer
  CMyClass *pseudoThis = (CMyClass*)lpParam;
  // call the right class function
  return pseudoThis->CommWatchProc();
}

UINT CMyClass::ComWatchProc()
{
  // the thread run here
  // you access all member data of this object
  for(;;)
  {

  }
  return 0;
}

CMyClass MyClass;

// to start the thread
m_pThread = AfxBeginThread(StaticComWatchProc, (LPVOID)&CMyClass);
 
regards, jean-claude.


0
 

Author Comment

by:DBH21
ID: 1310884
You rule... thanks alot... thats so simple and beautiful...

One question though... could you explain what's happening when "return pseudoThis->CommWatchProc()" is called?
0
 

Author Comment

by:DBH21
ID: 1310885
Hey jclanz,

I just noticed  that you did (LPVOID) &CMyClass

When I changed my program to point to the class, it said that it was an illegal use of this type as an expression...

Any ideas? Other wise I'm in the same pickle and I can't access the other class members
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question