Hi, I'm using TBB to port a game I have written in Java (for Android) to C++ (for iPhone). As the game is multithreaded in Java, I was looking for something looking like Java for concurrency and came accross TBB that provides all the paradigm I need to port my game, which is great.
However, I'm struggling with a detail that I don't know how to work around.
The design of the game is pretty straight forward:
- An engine in C++, that I run in a dedicated thread,
- The main thread in Objective-C rendering the world based on the core outputs.
These 2 threads are supposed to "talk" via an atomic provided by the engine to the other thread, containing all the necessary info to render a frame.
The fact is that I instantiate the engine somewhere in the renderer, assign it to a member variable and start it like this:
[bash]core = new GameCore(configuration, 0);
tbb::tbb_thread *thread = new tbb::tbb_thread(*core);
It starts and everything is ok.
In the main thread (the renderer), I keep a reference on this object (core) to be able to get the atomic reference on screen updates, to be able to render the adequate frame.
And this is where my problem lies, when I dump memory addresses of the member instance (core) and the instance getting executed in the thread, I have different values. It must be because the thread creation creates a new instance of the "core" object. However, I couldn't figure out how to get this new object address, or maybe i'm completely wrong and I missed something obvious.
A workaround would be to have the "atomic" as a member variable of the renderer itself, passing a pointer to the renderer to the engine, and have the engine updating it, but I'd like to do it the other way around to stick to the Java design.
Do you guys have any idea of how I can achieve this?
Call the tbb_thread constructor with a function object f as the first argument and the pointer core as the second argument, where f(core) calls (*core)(), because that's how tbb_thread will execute it.
I now i'm missing something, however, this is the only way I found working ... It seems I can only rely on the operator() overloading for some reason, I have read the std::thread doc, but it seems that tbb_thread and std::thread are not constructed the same way ...
If someone can provide me with a cleaner solution, I would appreciate.
You can also do '#include "tbb/compat/thread"' and "std::thread t(ThreadExecutor(), core);", perhaps also reducing ThreadExecutor to a plain function; I don't see what the sleep()'ing loop is about.
BTW, I would have to check what C++11 std::thread does, exactly: it only has move semantics for the first argument, so does that mean that it will copy a persistent argument, mess it up, or refuse the code above (I assume the latter)? Anyhow, it doesn't seem that you would be better off using the real std::thread instead (which you are invited to do if the development environment knows C++11?). Maybe somebody else can comment on that.