Starting a method in another thread C++

2.3k views Asked by At

I'm having trouble finding out how to run a method in a seperate thread in C++ (using Visual C++ 2008), I've tried a number of ways to do this but none of them so far have been successful.

I'm very new to C++ but a fairly experienced programmer in Java, but have been given a task to fix some bugs in an old C++ application. The program uses an object called 'Mpeg' to control packetising and depackitising an Mpeg file. After setting up an Mpeg object properly, mpeg.Depacketise needs to be called, which then runs the method DepacketiseInputFile().

I've tried to make DepacketiseInputFile() run in a seperate thread by both using _beginthread and the System::Threading::Thread object using

Thread^ th = gcnew Thread(gcnew ThreadStart(DepacketiseInputFile));

however this returns the errors

using &Mpeg::Depacketise gives the error

when using _beginthread the code I tried was

However with this I constantly had trouble getting the arguments correct, with errors like

cropping up.

Is there any simple way to do this that anyone can reccomend? I've spent a few days playing around with this but seem to be getting nowhere :(

Any help would be greatly appreciated.

Cheers.

2

There are 2 answers

2
Lou Franco On BEST ANSWER

What kind of type is Mpeg? What kind of method is DepacketiseInputFile?

If it's a regular unmanaged, C++ class, then use _beginthread, but you have to make DepacketiseInputFile a static. It cannot take a member function.

Also, don't call DepacketiseInputFile with DepacketiseInputFile(), pass it in with

  &Mpeg::DepacketiseInputFile

You should use the void* you get to pass it to pass in a pointer to the Mpeg object (and then cast it back).

If you want to use ThreadStart, then Mpeg needs to be a managed class.

EDIT: If you want to make DepacketiseInputFile, but it needs to access the object, then you use the void* argument to pass in a pointer.

So in the .h:

 void DepacketiseInputFileMember();
 static void DepacketiseInputFile(void *thisObj);

Your code goes in DepacketiseInputFileMember(), and write DepacketiseInputFile like this:

 void Mpeg::DepacketiseInputFile(void *thisObj)
 {
     Mpeg* mpeg = reinterpret_cast<Mpeg*>(thisObj);
     mpeg->DepacketiseInputFileMember();
 }

When you call _beginthread, use this

 _beginnthread(&Mpeg::DepacketiseInputFile, (unsigned)0, anMpegObjectPointer);

where anMpegObjectPointer is a pointer to an object of type Mpeg. You have to make sure the lifetime of the object is longer than it would be needed in the thread.

Forgive my syntax, I am writing this in a textarea, not Visual Studio

4
Eran On

Change

_beginthread(DepacketiseInputFile(), (unsigned)0, (void *)NULL);

to

_beginthread(DepacketiseInputFile, (unsigned)0, (void *)NULL);

You wanna pass the address of the function to run (DepacketiseInputFile) and not its return value of that function (which is what you get from DepacketiseInputFile()).

I'm assuming DepacketiseInputFile is declared as void DepacketiseInputFile(void*), and is not a non-static member function of some class. Otherwise, the types won't match even when you do remove the brackets.