First time using threads. Am I doing it correctly?

Started by
24 comments, last by Kylotan 13 years, 9 months ago
Yes that were my thoughts. I guess now I'm able to put some nice multi-threaded features into my game. [smile]

Thank you for all your help!
Advertisement
One final piece of advice: if you're not going to use boost::threads right away, at least make sure you use one of its best features, which can be easily implemented even with the standard WIN32 threading API (I'll show you how in a second). I'm talking about those scoped locks I already mentioned before.


/// Rather than sprinkling explicit calls to EnterCriticalSection() and LeaveCriticalSection() all over your code,/// use this class to handle it for you. Simply create an instance of this class on the stack every time you need/// to work with a critical section. The constructor will acquire the critical section for you. Once the/// ScopedLock instance goes out of scope, the destructor automatically takes care of releasing/// the critical section again./// See the example below.class ScopedLock{public:	/// The constructor acquires the critical section and stores a pointer to it so that we can later LeaveCriticalSection()	/// in the destructor.	explicit ScopedLock( CRITICAL_SECTION& criticalSectionToAcquire ) : m_criticalSection( criticalSectionToAcquire ), m_acquired( true )	{		::EnterCriticalSection( &criticalSectionToAcquire );	}		/// The destructor automatically releases the critical section.	~ScopedLock()	{		release();	}		/// Function to explicitly release the critical section before this instance is destroyed, should you ever need to.	void release()	{		if ( ! m_acquired )		{			return;		}				::LeaveCriticalSection( &m_criticalSection );		m_acquired = false; 	}private:	CRITICAL_SECTION& m_criticalSection;	bool m_acquired;		// prevent copying and assigning	explicit ScopedLock( const ScopedLock& );	ScopedLock& operator = ( const ScopedLock& );};// usage exampleCRITICAL_SECTION myCriticalSection;bool globalBool = true;void threadFunction_A(){	const ScopedLock sl( myCriticalSection );		if ( ::globalBool )	{		// ...	}}void threadFunction_B(){	const ScopedLock sl( myCriticalSection );		::globalBool = false;}
This sounds cool!

However, I am using boost right now. How do I do that with boost? I only found a mutex. Is this the same? I'm not sure...
Read this.


example:

boost::mutex theMutex;bool globalBool = false;void threadFunction_A(){    const boost::mutex::scoped_lock sl( theMutex );    globalBool = true;}
So easy! [grin] Thank you, again! I wish I could rate you up even higher [smile]
Hey no problem, us sauerkraut and bratwurst munchers have to stick together, eh? ;)
How do I build that damn "libboost_thread-vc100-mt-gd-1_42.lib"?
Sorry, I'm a bit annoyed right now. I've been messing around with the boost docs for almost an hour:

http://www.boost.org/doc/libs/1_36_0/more/getting_started/windows.html#prepare-to-use-a-boost-library-binary (Yes...)
http://www.boost.org/doc/libs/1_36_0/tools/build/index.html (hmpf)
http://www.boostpro.com/products/free (404?)
http://sourceforge.net/project/showfiles.php?group_id=7586&package_id=8041 (What??)

I downloaded that bjam tool and it wants boost.build which takes a couple of minutes to download here. ... Oh, and right now the download stopped [smile]

A great start [sad]

EDIT: Weee! Download crashed! Again...

EDIT2: And it looks like that this from the documentation isn't working:
C:\WINDOWS> cd C:\Program Files\boost\boost_1_36_0C:\Program Files\boost\boost_1_36_0> bjam ^More? --build-dir="C:\Documents and Settings\dave\build-boost" ^More? --toolset=msvc stage


Produces:
C:\WTech DX10\boost_1_42>bjam ^Mehr? --build-dir="c:\WTech DX10\boost_1_42\build" ^Mehr? --toolset=msvc stagenotice: could not find main target  stagenotice: assuming it's a name of file to createC:/WTech DX10/boost_1_42/build\project.jam:699: in attributewarning: rulename $($(project).attributes).get expands to empty stringC:/WTech DX10/boost_1_42/build\project.jam:709: in project.targetC:/WTech DX10/boost_1_42\build-system.jam:395: in loadC:\WTech DX10\boost_1_42\kernel\modules.jam:261: in importC:\WTech DX10\boost_1_42\kernel\bootstrap.jam:132: in boost-buildC:\WTech DX10\boost_1_42\boost-build.jam:1: in module scopeC:/WTech DX10/boost_1_42/build\project.jam:699: in project.attributewarning: rulename $($(project).attributes).get expands to empty stringC:/WTech DX10/boost_1_42/build\targets.jam:203: in object(project-target)@34.__init__C:/WTech DX10/boost_1_42/kernel\class.jam:93: in newC:/WTech DX10/boost_1_42/build\project.jam:709: in project.targetC:/WTech DX10/boost_1_42\build-system.jam:395: in loadC:\WTech DX10\boost_1_42\kernel\modules.jam:261: in importC:\WTech DX10\boost_1_42\kernel\bootstrap.jam:132: in boost-buildC:\WTech DX10\boost_1_42\boost-build.jam:1: in module scopeC:/WTech DX10/boost_1_42/build\project.jam:699: in project.attributewarning: rulename $($(project).attributes).get expands to empty stringC:/WTech DX10/boost_1_42/build\targets.jam:222: in getC:/WTech DX10/boost_1_42/build\targets.jam:284: in targets-to-buildC:/WTech DX10/boost_1_42/build\targets.jam:253: in object(project-target)@34.generateC:/WTech DX10/boost_1_42\build-system.jam:414: in loadC:\WTech DX10\boost_1_42\kernel\modules.jam:261: in importC:\WTech DX10\boost_1_42\kernel\bootstrap.jam:132: in boost-buildC:\WTech DX10\boost_1_42\boost-build.jam:1: in module scopeC:/WTech DX10/boost_1_42/build\project.jam:699: in project.attributewarning: rulename $($(project).attributes).get expands to empty stringC:/WTech DX10/boost_1_42/build\targets.jam:222: in getC:/WTech DX10/boost_1_42/build\targets.jam:285: in targets-to-buildC:/WTech DX10/boost_1_42/build\targets.jam:253: in object(project-target)@34.generateC:/WTech DX10/boost_1_42\build-system.jam:414: in loadC:\WTech DX10\boost_1_42\kernel\modules.jam:261: in importC:\WTech DX10\boost_1_42\kernel\bootstrap.jam:132: in boost-buildC:\WTech DX10\boost_1_42\boost-build.jam:1: in module scopedon't know how to make <e>stage...found 1 target......can't find 1 target...


[sad]
Assuming you have boost installed to C:\boost\boost_1_43_0, do the following things on the command line ...

- cd into "C:\boost\boost_1_43_0"
- bjam --build-dir=. --toolset=msvc --build-type=complete stage


As an aside, if you have multiple Visual Studio versions on your machine, bjam will normally build boost for the latest version. I.e. if you have both Visual C++ 2008 and Visual C++ 2010, it will only build for Visual C++ 2010. To force it to build for Visual C++ 2008:

- bjam --build-dir=. --toolset=msvc-9.0 --build-type=complete stage


And yeah, I really wish they'd update their 'Getting started' documentation ... I've had trouble with their instructions, too, until I found a working command line somewhere on the internet.
This complains about the same thing as above.
I still use 1_42, is that a problem? I don't want to download that whole thing again [sad] (56k Mobile Webstick <- Takes almost days)
Hmmm, no 1_42 should be fine. I have both 1_42 and 1_43 on my computer and both builds worked fine. The problem must be something else.

When you step into "C:\WTech DX10\boost_1_42", does that directory contain the following?

- directory "boost"
- directory "doc"
- directory "libs"
- directory "more"
- directory "people"
- directory "status"
- directory "tools"
- directory "wiki"
- file "boost-build.jam"
- file "bootstrap.bat"
- file "Jamroot"

And a couple of other files?

This topic is closed to new replies.

Advertisement