Multi-threading

Get answers to all your basic programming questions. No Ogre questions, please!
TwGxSlugger
Gnoblar
Posts: 5
Joined: Tue Feb 07, 2012 5:22 am

Multi-threading

Post by TwGxSlugger »

So I have to learn how to do threading for a class I am taking and it needs to work with ogre as well. I have no background in threading and was told to come here and see if anyone did know how to thread in ogre. Any tutorials that people have would be greatly appreciated. :D
TwGxSlugger
Gnoblar
Posts: 5
Joined: Tue Feb 07, 2012 5:22 am

Re: Multi-threading

Post by TwGxSlugger »

bump?
User avatar
duststorm
Minaton
Posts: 921
Joined: Sat Jul 31, 2010 6:29 pm
Location: Belgium
x 80

Re: Multi-threading

Post by duststorm »

You wouldn't be threading in Ogre exactly, but you would like to search for threading using C++ in general.
You usually have a few choices, from the ones supported by your compiler or development environment (gcc, MSVC++) to intermediary and platform independent stuff like boost or Qt libraries.
Ogre uses boost libraries for threading, so "boost threading" might be something you want to search for.

If you are more comfortable with C# you could use its threading functionality and link with mogre.

Just know that threading a render engine is hard, so you might not have chosen the right case study to learn threading with. If you still persist in experimenting with threading using ogre, I suggest you try to run a thread in parallel to your ogre rendering code that does a different task. For example update some data model in one thread that is visualized in ogre in another thread, or by including physics or audio in another thread parallel to the ogre rendering.
Developer @ MakeHuman.org
TwGxSlugger
Gnoblar
Posts: 5
Joined: Tue Feb 07, 2012 5:22 am

Re: Multi-threading

Post by TwGxSlugger »

well i didn't really get to choose everyone in the class kind of chose me to do it without allowing me to say no and i have to do the sound, threading, rendering, and shader
Jusas
Halfling
Posts: 91
Joined: Mon May 14, 2007 9:14 am
x 8

Re: Multi-threading

Post by Jusas »

TwGxSlugger wrote:well i didn't really get to choose everyone in the class kind of chose me to do it without allowing me to say no and i have to do the sound, threading, rendering, and shader
If you're doing all that, what's the rest of the class doing, eating popcorn? :lol:

Ok to the issue at hand... If you're looking for C++ threading advice and tutorials I'd suggest googling boost threading tutorials, I personally use boost for threading as it feels easy to use supports multiple platforms. Also remember that you can download the boost binaries directly from http://www.boostpro.com, no need to go compiling them yourself which can be a hassle. You can choose Ogre to be built using boost threads as well, which makes using it in your own code a natural thing.

To get you started I'll throw you a few bones:

Code: Select all

// The basic lock used for synchronizing threads. Ogre uses this as well.
boost::recursive_mutex my_mutex;

// Use it like this:
{
    boost::recursive_mutex::scoped_lock lock(my_mutex);
    // do stuff here that needs the protection of the mutex
}

// Create a new boost thread from an object:
// In order to use a class instance as a thread the class needs to have operator() overloaded, for example something like this:

class WorkerSample
{
public:
void operator()()
{
    // do thread work here
}

...

};

// ...and then you can just call it to run like this:
WorkerSample* w = new WorkerSample();
boost::thread trd = new boost::thread(boost::ref(*w));

// Wait and join the thread:
trd->join();
That should get you started.

If you're interested in C# threading it's a little different but also very simple indeed. This sample here should give you something to start with: http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx. In addition if you're using WPF there are things like Dispatcher and DispatcherTimer that are useful especially when working with the GUI... but I don't think you're interested in WPF if you're working on an Ogre app so I'll just leave it at that :)
TwGxSlugger
Gnoblar
Posts: 5
Joined: Tue Feb 07, 2012 5:22 am

Re: Multi-threading

Post by TwGxSlugger »

I will do that thank you
al2950
OGRE Expert User
OGRE Expert User
Posts: 1227
Joined: Thu Dec 11, 2008 7:56 pm
Location: Bristol, UK
x 157

Re: Multi-threading

Post by al2950 »

Hi

If you want to use threading with Ogre I would suggest you look at the Ogre WorkQueue system. However I would not want to confuse you if are just starting out in threading but I think it would be easier for you than starting from scratch with boost threads. This does of course depend on exactly why you need to multi thread your system.
TwGxSlugger
Gnoblar
Posts: 5
Joined: Tue Feb 07, 2012 5:22 am

Re: Multi-threading

Post by TwGxSlugger »

I believe the threading is needed for some sounds but I am not entirely sure. I will look into that as well because I have to agree boost so far does not look like a fun time but I expect threading wont be a fun time at all