Question

I wrote a tool in C++ using wxWidgets for the GUI and IBM ILOG Cplex to solve an optimization problem.

In one of the functions called by the wx event handler, I invoke the IBM ILOG Cplex Optimizer which is itself multi-threaded code.

I realize that this causes indererministic bugs with non-sensical memory contents.

Since I have no experince in writing multi-threaded code and would like to get away without spending three weeks learning how to do it, I would like to know:

Is there is some safe, possibly inelegant way to avoid problems here? (More elegant, maybe, than writing a file to disc, calling a different task through the OS and reading the output back in).

Is it a bad idea to launch Cplex threads from a wx thread?

Is it generally a bad idea to use two libraries that might use different libraries internally to implement multi-threading? (I have no idea what there is except pthreads and what is used by either cplex or wx).

Any help and background information is appreciated.

Was it helpful?

Solution

Based on my experience, the rule is:

every wxWdiget function call that change the display must be made in the wxWidget thread

I don't know much about Cplex, but if you say it's multithreaded, chances are you are calling an asynchronous function and you handle the results in a call back. The callback is most definitely not called withing the wxWidget thread. If you then try to display the results within the callback, you are breaking the rule stated above. That's when you'll get nice little bugs, which in my case usually materialize as heap corruption.

To fix that you must pass the results of your callback to the wxWidget thread and display them in that thread. There's many way to do it, but the global mechanism is to trigger a custom event on wxWigdet that get passed to the wxWidget thread.

Check this link, http://wiki.wxwidgets.org/Custom_Events you need to use wxEvtHandler::AddPendingEvent(wxEvent& event)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top