Reputation: 8325
I have built a class called App. This is part of my traditional loop within my App::Loop function:
this->SwapBuffers();
this->Iterate(); // generate a new buffer 1 from buffer 0
this->Render(); // render the original buffer 0
And I can successfully get that to multithread with this code:
this->SwapBuffers();
std::thread t1 ( &App::Iterate, this );
std::thread t2 ( &App::Render, this );
t1.join();
t2.join();
But it actually runs a bit slower than the traditional code above (about 45fps vs 55fps). I suspect it is because the thread calls copy the entire App class object and work on that copy each. So, I've tried passing by reference:
std::thread t1 ( &App::Iterate, ref(this) );
std::thread t2 ( &App::Render, ref(this) );
But I get the compile message:
error: use of deleted function ‘void std::ref(const _Tp&&) [with _Tp = App*]’
I've been trying to understand the threading logic by following this guide here.
How do I properly pass the class object in by reference?
I am working in a Linux environment with GNU development tools.
Upvotes: 1
Views: 117
Reputation: 1573
You don't need to pass the pointer to App as reference because the this
pointer type App*
has a small size (32/ 64 bit) which means it's preferrable to pass it by value.
When you pass this
it is equal to passing App as reference because references get converted to pointers anyway.
Probably your delay happens because you create and destroy a thread at every rendering step. Threads are heavy objects which require the OS to allocate a huge stack which probably causes the performance loss in your code.
Consider to reuse your threads through a threadpool or similar.
Upvotes: 4