0

here is the problem I want to sumbit to you fellow gifted coders : I have a custom web container written in C++ that can communicate with javascript. From C++ to JS it can do synchronous JS function calls From JS to C++ it can do asynchronous C++ function calls

With these tools, I have to do a synchronous call from JS to C++... As I can't use message communication system (because it implies asynchronicity on the main thread unless I miss something), my first guess was to use worker thread

"main.js" file

    var synctools = false;
    var syncdata ="";
    var myworker=new worker("demo_workers.js");
    while(synctool == false){} //It s ugly but i haven t had any other ideas

"demo_workers.js" file

    CallCPPFile(param,callback); //call the c++ and c++ call callback when finished
    function callback(data)
    {
      //do stuff
      syncdata = data;
      synctools = true; //this is supposed to stop the looping of the main js
    }

Unfortunatly, synctools and syncdata are not accessible from the worker thread. So here are a few questions :

  • is there another method of communication between worker and main thread ? i have tried to use navigator object but it does'nt seem to work either (I don t know if its the same navigator object between the two threads)
  • Apart from web worker, do you see another way I can achieve my goal ? (I tried promise too with no luck)

Thanks for your help

Patrice Pistone
  • 73
  • 1
  • 1
  • 4
  • From what I've seen with web worker, it is purposefully very restricted in how you can use it. Only allows you to pass copies into the worker thread and no sharing of resources between threads. So you could pass synctools and syncdata as parameters, but if you manipulate it on one thread, you won't see the changes on the other thread. But if you only need to have the worker thread manipulate it and pass it back to the main thread, you might be able to accomplish that with postMessage(). – lintmouse Mar 23 '15 at 20:54
  • what is `CallCPPFile` exactly?.. and entry point function to determining what resource to send an http request to? – Brett Caswell Mar 23 '15 at 21:04
  • @ dustmouse : postMessage is not an option as I can't recieve the posted message into the main js file because it s looping. And if I sopt the loop I loose the synchronicity aka the javascript return and the C++, on another thread, continues its work – Patrice Pistone Mar 23 '15 at 21:06
  • @Brett No it is simply the way the container does the communication between JS and C++. CallCPPFile is the name of a function that communicate data to the C++, call a specific function into the C++ code. And the C++ when finished call bakc the js callback passed as a parameter. Unfortunatly I have no rights to modifiy the C++ – Patrice Pistone Mar 23 '15 at 21:08
  • let me rephrase, what is the context of this communication?.. is it a request and response using http?.. would a synchronous XMLHttpRequest object be viable? `xhr.open(method, resource, asyncflag)` – Brett Caswell Mar 23 '15 at 21:28
  • actually.. there is another context that needs to be scoped here, is this all client side, i.e. in a user-agent of some sort? – Brett Caswell Mar 23 '15 at 21:39
  • @Brett : sorry. The CallCPPFile's code is hidden from me but I guess it s a kind of XMLHttprequest. This is correctly catched on the C++ side but it is totally wrapped for me. I have tried the synchronous request like you suggest but I have find no ways to catch my event on the c++ side despite it seems to work with the dedicated version.. – Patrice Pistone Mar 23 '15 at 21:42
  • I have seen in the C++ class provided to me a function called SetUserAgent. i don t really know about the user agent but the person who provided the class says to me that it s based on chrome. The problem is that I have no way to work on c++ client side, I have to find a pure javascript way/hack – Patrice Pistone Mar 23 '15 at 21:46

1 Answers1

0

Here's the nuts and bolts of your problem. Synchronization in an asynchronous system requires both sides to cooperate. Your problem is that a piece of code running in complete isolation cannot have any of it's data updated unless it does this itself. Since all JavaScript threads are completely isolated from each other, unless you write some kind of external callable that can do the waiting for you, then you're stuck. There is no purely JavaScript solution to this problem.

If, however you don't mind writing some more C++, then you can do something clumsy and get something working:

Create a multi-threaded mini html server. Design the server to take an id (provided by the caller) as a parameter, create a storage point for data and wait. Design the server to take an id and data as parameters. When receiving a data parameter, the storage point associated with the received id is set and the wait associated with the id is removed.

This lets 1 javascript thread wait on another thread with a synchronous AJAX request. The only requirement is that the waiting thread use postMessage to send the wait id to the destination thread before waiting.

On second thought, something like this could also be done in pure javascript as long as you can create an http server in a separate thread. The trick would be keeping the http connection open until the response is ready. It all depends on what kind of JavaScript environment you're working in.

Arkain
  • 918
  • 6
  • 10