2

I am just trying to understand some source code written in C++. I am a bit familiar with C++, however, the following code sequence is absolutley new to me. A guess would be that here I register some thread routine with the kernel, ptest->Run(). Could that be right?

static unsigned int __stdcall ThreadProc(void *lParameter)  
{  
CTest *ptest;  

ptest= (Ctest *)lParameter;  
ptest->Run();  
return 0;  
 }  


CTest::CTest(int n)  
{  
...  
}  

5 Answers5

2

A bit simplified but a thread is a function, in this case ThreadProc. When the thread starts, the function is called and when the function exits the thread dies.

In this case, someone has started a thread with CreateThread, begin_thread or something else and passed in a pointer to a class called CTest as an argument. The thread then casts it back to a CTest* (as you can see the argument is delivered by the CreateThread API as a more generic void*) and calls the CTest::Run method before exiting.

Edit: Also, except for the "__stdcall" declaration, this is not very Windows specific. Threads in C and C++ works more or less like this on all OSes.

Fredrik
  • 5,759
  • 2
  • 26
  • 32
2

This is a function signature that would be used to define a function that is exported from a DLL or used as a callback function. In this case it is probably going to be used a the main loop of a worker thread.

the __stdcall keyword indicates that the function call is passed on the stack using the stdcall calling convention in Windows (same as used by methods exported from the Win32 API)

OOPS: this link doesn't play nice with markdown http://msdn.microsoft.com/en-us/library/zxk0tw93(VS.80).aspx

MSalters
  • 173,980
  • 10
  • 155
  • 350
Jeff Leonard
  • 3,284
  • 7
  • 29
  • 27
1

Not quite. This is your thread function:

static unsigned int __stdcall ThreadProc(void *lParameter)

It will be executed an different thread than whatever caused it. Calling code creates an object of type CTest, creates a thread that runs ThreadProc, which in turn runs ptest->Run();

ThreadProc is just a convenience wrapper to launch ptest->Run(). (Because otherwise it is kinda hard to use pointers to member functions)

Eugene
  • 7,180
  • 1
  • 29
  • 36
0

What OS? Looks like a Windows sample, if so begin_thread(), or CreateThread or...several

kenny
  • 21,522
  • 8
  • 49
  • 87
0

The code you show declares a pointer to a CTest class object, converts the input parameter into one of those, then calls its run method.

The why this is done is the tricky part. Normally you wouldn't write code like this, however, the profile of ThreadProc is that of a thread's main entry point. For one of those, Windows doesn't give you any choice for the parameter profile of it, and it can't be a class member.

What you have there is fairly standard code to convert a thread entry-point callback from the Windows' required form into a class method call.

For a full discussion of this, see my (accepted) answer for the question: Passing Function pointers in C++

Community
  • 1
  • 1
T.E.D.
  • 44,016
  • 10
  • 73
  • 134