Click to See Complete Forum and Search --> : C++ .NET and Threads


pixal
January 4th, 2004, 07:37 PM
I've been looking at the sample code for creating threads from a managed class. The C++ examples always use static functions for the function called by start. Is there a requirement that static functions need to be used?

The problem is there are no examples showing passing data to the threads for c++. The examples for C# pass data using the constructor. The C++ samples don't use constructors for the class containing the thread.

I created a sample program that uses non-static functions and it seems to run ok, but it is a very simple program.

al

Andreas Masur
January 5th, 2004, 02:41 AM
Well...I assume that these static functions were member functions of a class? That would be normal...as this FAQ (http://www.codeguru.com/forum/showthread.php?s=&threadid=231246) explains...

pixal
January 6th, 2004, 12:54 PM
Thanks for the FAQ.

I removed the static decl in the .cpp but forgot to in the .h, so it was still static. Removing it from the .h gave compiler errors which was what I was expecting in the first place.

However, I still don't have a good way to pass data to the thread.
The only idea I've had (haven't tried) is to call a separate static function of the class to pass a data pointer before starting the thread:

Class1 * pclass1 = new Class1;
class1->StaticDataFunc(pData);
Thread *oThread;
oThread = new Thread(new ThreatStart(0,
&Class1::StaticStartFunc));
oThread->Start();

The problem I see with this is I'll have to be careful when I start multiple threads. Is there a better solution?

az