在另一个线程中启动一个方法 C++
我无法找出如何在 C++ 中的单独线程中运行方法(使用 Visual C++ 2008),我尝试了多种方法来执行此操作,但到目前为止没有一个成功。
我对 C++ 很陌生,但在 Java 方面是一位相当有经验的程序员,但我被赋予了修复旧 C++ 应用程序中的一些错误的任务。该程序使用名为“Mpeg”的对象来控制 Mpeg 文件的打包和解包。正确设置 Mpeg 对象后,需要调用 mpeg.Depacketise,然后运行 DepacketiseInputFile() 方法。
我试图通过使用 _beginthread 和 System::Threading::Thread 对象使 DepacketiseInputFile() 在单独的线程中运行,
Thread^ th = gcnew Thread(gcnew ThreadStart(DepacketiseInputFile));
但是这会返回错误,
给出错误
使用 &Mpeg::Depacketise在使用 _beginthread 代码时 尝试过
但是,这样我总是很难让论点正确,出现诸如
突然出现的错误。
有没有任何人都可以推荐的简单方法来做到这一点?我花了几天时间研究这个问题,但似乎一无所获:(
任何帮助将不胜感激。
干杯。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Mpeg 是什么类型? DepacketiseInputFile是什么样的方法?
如果它是常规的非托管 C++ 类,则使用 _beginthread,但必须将 DepacketiseInputFile 设为静态。它不能采用成员函数。
另外,不要使用 DepacketiseInputFile() 调用 DepacketiseInputFile,而是将其传递给
您应该使用您获得的 void* 来传递它以传递指向 Mpeg 对象的指针(然后将其转换回来)。
如果你想使用ThreadStart,那么Mpeg需要是一个托管类。
编辑:如果您想制作 DepacketiseInputFile,但它需要访问该对象,那么您可以使用 void* 参数传入指针。
因此,在 .h 中:
您的代码放入 DepacketiseInputFileMember() 中,并像这样编写 DepacketiseInputFile:
当您调用 _beginthread 时,请使用此代码,
其中
anMpegObjectPointer
是指向 Mpeg 类型对象的指针。您必须确保对象的生命周期比线程中所需的生命周期长。请原谅我的语法,我正在文本区域中编写此内容,而不是 Visual Studio
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
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:
Your code goes in DepacketiseInputFileMember(), and write DepacketiseInputFile like this:
When you call _beginthread, use this
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
更改
为
您想要传递要运行的函数的地址 (
DepacketiseInputFile
),而不是该函数的返回值(这是您从DepacketiseInputFile()
获得的值)。我假设 DepacketiseInputFile 被声明为 void DepacketiseInputFile(void*) ,并且不是某个类的非静态成员函数。否则,即使删除括号,类型也不会匹配。
Change
to
You wanna pass the address of the function to run (
DepacketiseInputFile
) and not its return value of that function (which is what you get fromDepacketiseInputFile()
).I'm assuming
DepacketiseInputFile
is declared asvoid 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.