简体   繁体   English

将对象传递给C ++中的线程

[英]Passing an object to a thread in C++

I have a project that creates an object of a class stored in a header file. 我有一个项目,该项目创建存储在头文件中的类的对象。 I need this object passed into a thread from the main function. 我需要将此对象从主函数传递到线程中。

Currently I'm using hChatThread = CreateThread(NULL,0,ChatFunc,"1",0,&dwChatThreadId); 目前,我正在使用hChatThread = CreateThread(NULL,0,ChatFunc,"1",0,&dwChatThreadId); as a way of creating a thread, and it seems I cannot change the fact that the called function can only have LPVOID as a type. 作为创建线程的一种方式,看来我无法改变被调用函数只能将LPVOID作为类型的事实。 If I could somehow change this, I could then pass the object as an argument like so: hChatThread = CreateThread(NULL,0,ChatFunc,myObject,0,&dwChatThreadId); 如果我可以以某种方式更改它,则可以像这样将对象作为参数传递: hChatThread = CreateThread(NULL,0,ChatFunc,myObject,0,&dwChatThreadId);

But it seems this is next to impossible. 但这似乎几乎是不可能的。

Any ideas? 有任何想法吗?

Thread: 线:

DWORD WINAPI ChatFunc(LPVOID randparam)
{
    ServerMain(myObject); //need to pass object again here to be used in other functions
    return 0;
}

Main: 主要:

int main(int argc, char *argv[])
{
    myObject w;
    w.exec();
    hChatThread = CreateThread(NULL,0,ChatFunc,"1",0,&dwChatThreadId);
    return 0;
}

You can use LPVOID lpParameter to pass a pointer to the object, and then cast it back. 您可以使用LPVOID lpParameter将指针传递给对象,然后将其LPVOID lpParameter回退。 Something like this: 像这样:

int main(int argc, char *argv[])
{
    myObject w;
    w.exec();
    hChatThread = CreateThread(NULL,0,ChatFunc,&w,0,&dwChatThreadId);

    // wait for the thread to terminate,
    // otherwise w might get destroyed while the thread
    // is still using it (see comments from @MartinJames)

    return 0;
}

DWORD WINAPI ChatFunc(LPVOID param)
{
     myObject* p = static_cast<myObject*>(param);
    ServerMain(p); //need to pass object again here to be used in other functions
    return 0;
}

The easiest way would be to make that Object a global object in the header file, and given that you included the code: 最简单的方法是使该对象成为头文件中的全局对象,并假定您包含以下代码:

#include "headerFilesName.h"

in the file where your main function is. 在您主要功能所在的文件中。 Then I would declare another object inside the file with the main function and just pass it its value 然后我将使用main函数在文件中声明另一个对象,然后将其值传递给它

headerFileName myNewObject;

with this object of the class of the header file, you should be able to access the other object you created initially. 使用头文件类的该对象,您应该能够访问最初创建的另一个对象。

There is only one flexible solution - dynamically allocate 'w' with operator new, pass the pointer into CreateThread, cast it back in the thread function and, when/if the thread function exits/returns and the object instance is no longer required, delete it. 只有一种灵活的解决方案-使用new运算符动态分配'w',将指针传递给CreateThread,将其强制转换回线程函数中,并且,当/如果线程函数退出/返回且不再需要对象实例,则删除它。

All the other solutions either don't work reliably, have messy globals or are inflexible. 所有其他解决方案要么运行不可靠,要么全局混乱,要么不灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM