我可以将 STL 数据结构传递给 Win32 消息循环吗?

发布于 2024-09-14 20:09:34 字数 617 浏览 1 评论 0原文

我有一个多线程 Windows 应用程序,其中一个线程中有一个消息泵。我需要向该线程发送消息,向其传递信息。但是,我想在工作线程中使用的库之一需要 std::string。我可以执行如下操作:

typedef struct tagCOMMAND
{
    std::map<std::string, std::string> locator;
    std::string body;
} COMMAND, *LPCOMMAND;

然后将结构传递给消息,如下所示:

LPCOMMAND cmd;
cmd->body = "Hello";
cmd->locator["Hello"] = "World";
PostThreadMessage(dwThread, MY_CUSTOM_MESSAGE, NULL, (LPARAM)cmd);

如果可以,谁负责释放内存、调用线程或工作线程?

注意,我的印象是使用 Windows 消息循环是最简单、最好的方法,但如果更合适的话,我并不反对使用 Boost 库之类的东西。然而,这是一个特定于 Windows 的应用程序,并且只能在 Windows 上运行,因此跨平台兼容性并不是我过于关心的问题。

I have a multithreaded Windows application where one of the threads has a message pump in it. I need to send a message to that thread, passing information to it. However, one of the libraries I want to use in the worker thread requires std::string. Can I do something like the following:

typedef struct tagCOMMAND
{
    std::map<std::string, std::string> locator;
    std::string body;
} COMMAND, *LPCOMMAND;

Then pass the struct to the message like so:

LPCOMMAND cmd;
cmd->body = "Hello";
cmd->locator["Hello"] = "World";
PostThreadMessage(dwThread, MY_CUSTOM_MESSAGE, NULL, (LPARAM)cmd);

If this is okay, who's responsible for freeing the memory, the calling thread or the worker thread?

N.B. I'm proceeding under the impression that using a Windows message loop is the easiest and best approach here, but I'm not opposed to using something like a Boost library if that's more appropriate. However, this is an application that is Windows-specific, and will only ever run on Windows, so cross-platform compatibility isn't something I'm overly concerned with.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窝囊感情。 2024-09-21 20:09:34

只要有一个接收者(不多也不少),将指针作为异步消息参数传递给对象是正常的。

释放内存应该是接收者的责任,因为调用者没有指示何时可以安全地执行此操作。

It is ok and normal to pass pointer to objects as async message parameters, as long as there is a single receiver (no more and no less).

It should be the responsibility of the receiver to free the memory, since the caller has no indication when it is safe to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文