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.
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.