C++多线程和事件

发布于 2024-12-11 17:54:10 字数 384 浏览 1 评论 0原文

我有一个 C++ 应用程序,其结构如下:

AAA 类:具有一些函数,其中一个函数用于打开线程。 BBB类:有一些函数,其中之一是打开线程的函数。 CCC 类:称为 AAA 和 BBB,两者在其功能内部都打开了踏板。

在 AAA 线程中,在某些情况下,我知道一些事情,我想退出线程并通知 BBB 和 CCC。 因为我在一个线程(Win32Thread)中,它是我在线程中运行的一个 void 函数,所以我无法将值返回给 CCC。

我是 C++ 新手(来自 c# 领域),不知道该怎么做。 (通知) 注意:我无法更改此结构。我只能添加或对类的功能进行微小的更改。这是一个在线程和大型代码中运行的大进程。

有什么想法吗?如果可能的话,请不要弄脏:)

添加一个样本会对我很有帮助。

I have a C++ application which has the following structure:

Class AAA: has some functions and one of them function that opens a thread.
Class BBB: has some functions and one of them function that opens a thread.
Class CCC: call AAA and BBB that both internaly in their functions open the treads.

In AAA thread in some case I know something that I want to exit the tread and notify both BBB and CCC.
because I am in a thread (Win32Thread) it is a void function I am running in the thread, so I can't return a value to CCC.

I'm new to C++ (coming from c# area) and don't know what is the way to do it. (the notification)
note: I can't change this structure. I can only add or do minor changes in the function of the classes. it is a big process that running in the treads and large code.

Any idea? not a dirty one please, if it possible :)

adding a sample will help me very much.

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

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

发布评论

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

评论(3

樱桃奶球 2024-12-18 17:54:10

我不太明白你的问题,有点太笼统了。
而且您没有指定您使用哪种多线程库。

为了在线程之间发送消息,通常使用消息队列,并使用等待句柄、锁和信号量来同步它们。

当然,您需要一个安全的多线程队列来在线程之间发送消息。

一种可能的解决方案:

如果线程 A 需要通过将消息排入线程 B 队列来向线程 B 发送消息,例如通过等待事件(如果它处于空闲状态)将其唤醒。
线程 B 接收消息并响应,在 A 队列中发布另一条消息。

另一种可能的解决方案:

线程A需要向线程B发送消息并需要回复,阻塞线程A直到没有收到回复。
线程A将消息放入线程B队列中,消息对象可以在函数堆栈中。然后,如果线程 B 处于空闲状态,他会唤醒该线程 B,然后通过等待句柄或信号量等进入等待状态。
线程B,当消息出队时,将答案写入线程A发送的对象中,并将线程A从等待状态唤醒。
对象字段应标记为易失性,因为由两个线程进行读\写访问。
然后线程A使用消息对象中存储的值并从堆栈中删除该对象。

用文字听起来很复杂,但实现起来却很简单。

如果您使用的是 Windows 操作系统,则可以仅使用 Windows 消息队列创建不可见的消息窗口,每个线程一个。第一种情况使用 PostMessage,第二种情况使用 SendMessage。

I don't understand too much your problem, is a bit too generic.
And you didn't specify what kind of multithreading libraries are you using.

To send messages between threads usually Message Queues are used, with wait handles, lock and semaphores to synchornize them.

Of course you need a safe multithreading queue to send your messages between threads.

One possible solution:

If thread A need to send a message to thread B by enqueueing it into thread B queue, waking it up if it is in idle state through a wait event for example.
Thread B receives the message and respond posting another message in A queue.

Another possible solution:

Thread A need to send a message to thread B and need a reply, blocking thread A until the reply is not received.
Thread A enqueue a message in thread B queue, the message object can be in the function stack. Then he wakes up thread B if it is in idle state and then enters in a wait state through a wait handle or a semaphore for example.
Thread B, when dequeues the message, write the answer in the object sent by thread A and wakes up thread A from its awaiting state.
The object field should be marked as volatile because is accessed for read\write by two threads.
Then thread A uses the value stored in message object and delete the object from the stack.

Sounds complicated written in words but the implementation is quite straightforward.

If you are in Windows OS you can just use windows message queue creating invisible message windows, one for each thread. Use PostMessage for the first case, SendMessage for the second case.

病女 2024-12-18 17:54:10

如果您不害怕特定于 Windows 的解决方案:从 BBB 线程调用 PeekMessage()。这将为它创建一个 Windows 消息队列。 (主线程已经有一个)。现在,您可以使用 SendMessage() 将消息从 AAA 发送到其他线程。请注意,您正在发送到线程,而不是类。对 GetMessage() 的任何调用都会看到您的消息。

If you're not afraid of a Windows-specific solution: call PeekMessage() from BBB thread. This will create a Windows message queue for it. (The main thread already has one). Now, you can use SendMessage() to send a message from AAA to the other threads. Note that you are sending to threads, not classes. Any call to GetMessage() will see your messages.

独留℉清风醉 2024-12-18 17:54:10

我用于线程的典型设计是一个线程函数,该函数传递包含该函数的类的实例(该函数还保存该线程的实例变量)。您是否属于这种情况?线程是否有某个类的实例传递给它?如果他们这样做,那么只需跟踪来自外部的那些并放置标记何时停止的 BOOL 属性。在线程的主循环中,您只需检查标志以查看是否有任何业务再次循环。这是退出线程的唯一干净的方法。

the typical design I use for threads is a thread function that is passed an instance of the class that contains it (which also holds the instance variables for that thread). Is this the case for you? Does the thread have some instance of a class passed into it? if they do, then just keep track of those from the outside and put BOOL properties that flag when to stop. In the main loops of the threads you just check the flag to see if you have any business looping around again. This is the only clean way to exit a thread.

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