Basic thread issue

Basic thread issue

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:

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?Thanks,BQ.

6 posts / novo 0
Último post
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.

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.

Ok thanks for your feedback, I'm going to try this.

Ok I came up with a solution, seems to work, however, I'm sure it's pretty inefficient in term of source code.I ended up having the following ThreadExecutor class:

#ifndef __ThreadExecutor_H_

#define __ThreadExecutor_H_
#include

#include "FuriousBlocksCore.h"
class ThreadExecutor {

    public:

    void operator()(FuriousBlocksCore *core) {

        for (int tick = 0; tick < 1000; tick++) {

            cout << "thread running in parallel @ " << core << endl;

            core->onTick(tick);

            sleep(1);

        }

    }
};
#endif //__ThreadExecutor_H_

And then I launch the thread from Obj-C like this:
ThreadExecutor executor;

tbb::tbb_thread t(executor, core);

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.Thanks

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.

It's ok. I'm not familiar enough with C++11 (which I don't use for this project anyway) to be able to compare the pros/cons of tbb vs std::thread.I'm sticking with pure TBB as it provides all the concepts I need anyway.Thanks for your help,BQ.

Deixar um comentário

Faça login para adicionar um comentário. Não é membro? Inscreva-se hoje mesmo!