0

I have a question about c++ pointer to function.

I have 3 modules in my architecture: Module A, Module B and Module C.

Modules B and C will be created by the module A.

Module B has the logic to decide when to put to stop all modules.

To do this, I thought that the module A can pass a pointer to a function to module B. In this function the module A will stop the module B,C and then himself. These modules have a pthread.

Do you think that this might be a good choice?

I followed these steps:

  • I created a stop method on the form A;
  • In module B, when I put it to stop all modules, I call the function passed as a pointer.

How do I to pass my function pointer in the constructor of the form B?

1
  • I had a hard time understanding your question, I would strongly encourage you to post sample code so that it is easier to respond correctly. If your scenario allows you to use a C++11 compatible compiler, I would strongly recommend you look to std::thread, std::function and lambdas to solve these problems far more cleanly. Commented Jun 26, 2013 at 1:40

1 Answer 1

1

The idea sounds right, however you'll need some gearbox.

Let's play out the scenario:

B calls A::StopAll() A::StopAll() kills module B we return in this function with module killed.

This may work if "stop" means some logical thing, keeping the function intact, and the caller refrains to touch anything. But if it means like unloading b.DLL, you'll have a problem.

It's unclear how threads play either, might have similar problem.

If you have some message pump, the function triggered from B should just post a message to itself and exit to allow finishing the call in B. Then the message execution shall only start the killing. If you have multiple threads instead of message pump, you might need to alert a different thread.

Sign up to request clarification or add additional context in comments.

2 Comments

How do I to pass my function pointer in the constructor of the form B?
typedef void (tCallback*)(); and B::B(tCallback cb); B b(KillAll);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.