I have a C++ application that does some interactive rendering, and I'd like to link it up with a Mathematica notebook with two-way communication (there are some tasks like drawing plots that are easier in Mathematica, and some like the 3D renderer that are easier in C++, but the goal is for both of things to be linked). I've been reading up on WSTP and it seems like that would be ideal to set up communication between the two programs (launching the C++ application from the Wolfram Kernel via Install is not an issue).

My problem is that WSMain puts the C++ application in a loop which checks for packets from Mathematica. Since this is an interactive graphics application, it already has its own main loop. It would be simpler for me if I could just do the checking for new packets manually in my own loop, since I obviously can't run both main loops at once (I guess I could have the two loops run in different threads or C++ processes, and then have those communicate but that seems like more trouble than it's worth).

Does anyone have experience with how to set this up or is there any example code floating around for how to set up the communication between the C(++) application and a Mathematica notebook without using the loop implied by the use of WSMain (or MLMain in the case of MathLink)?

(If it matters, I'm on Windows 10 and compile the C++ application with VS2013 and don't require portability to other platforms.)

  • 2
    $\begingroup$ Actually this sounds like it should use MathLink, not LibraryLink. I don't know enough about threading to tell you if that's the "right way" to do it, although I would lean in that direction. So you can do this: MLMain is not provided by the MathLink library. It is generated by the mprep/wsprep programs. That means that you have full access to the source code of this main loop, and you can modify it as you wish. You can use the mprep output as a starting point. $\endgroup$ – Szabolcs Jul 16 '16 at 18:19
  • $\begingroup$ @Szabolcs Oh, that's a good starting point. I had assumed it's part of one of Wolfram's DLLs. $\endgroup$ – Martin Ender Jul 16 '16 at 18:21
  • $\begingroup$ I haven't done this, so I'm not sure, but doesn't that main loop contain blocking elements? I.e. doesn't it get stuck waiting for a packet on the link? I'd assume it does. Then you have to use threads anyway. $\endgroup$ – Szabolcs Jul 17 '16 at 9:49
  • $\begingroup$ @Szabolcs Yes, I'm looking into this right now, and it ultimately uses WSWaitForLinkActivity which blocks, so at that point I'll have to hand off to a second thread. $\endgroup$ – Martin Ender Jul 17 '16 at 9:51
  • $\begingroup$ Okay, I've got a simple toy example working based on this. I'll try integrating this into my main application over the next few days, and if everything works out, I'll self-answer with the toy example. Thanks for the help, @Szabolcs! $\endgroup$ – Martin Ender Jul 17 '16 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.