Jump to content
  • Advertisement
Sign in to follow this  
coderx75

Multithreading MADNESS!!!

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm pretty new to multithreading but I've done multithreading before using _beginthread/_endthread. I'm now trying to use an OO approach to multithreading and I think I'm beginning to get a good idea of why _beginthread() is "evil". I started using it because I heard it behaved well with the standard library. However, when involving virtual pointers in ANY way, starting a thread interferes with any other threads that are currently running. (???) I've done some research and have found that my options (so far) are _beginthread, _beginthreadex, CreateThread and AfxBeginThread. What are the +'s and -'s of each?

Share this post


Link to post
Share on other sites
Advertisement
Something else you might want to check out is the boost library. I had a thread a week or two ago about getting it working. Last night I got it working, and it seems pretty neat. Mind you, I haven't really done anything more with it more than shown in the thread, but it might be a bit easier than doing what you are.

Share this post


Link to post
Share on other sites
To wrap it into OO, just have a normal c-function in the .cpp which
receives a void-pointer to your class - cast it to the correct one,
and then call the "runThread"-member function of your class.

Add some nice thing here like an endless loop, with some breaking
condition and your ready to go.

Never had any problems with virtual functions & threading, don't
see the connection either...

/R

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Best to read up on the term atomic and critical section before attempting multi-threading.

Share this post


Link to post
Share on other sites
... and beware of sth like this:

void doSomething()
{
enterCriticalSection (&cs);

if (yadieyadie)
{
return; // <= NOT good, cs still locked!
}
leaveCriticalSection (&cs);
}

To avoid the above you might use sth like this:

class MutexWrapper
{ // wraps your OS-mutex
void on();
void off();
};

class MutexHelper
(
private:
MutexWrapper* mutex;

MutexHelper (MutexWrapper* _m) { if (m) { mutex = _m; m->on(); } }
~MutexHelper () { if (mutex) mutex->off(); }
}

Now have your mutex in the class-definition and in relevant
functions put the mutexHelper at the top, the mutex will
be locked/unlocked automagically - even if someone puts a
returns somewhere.

Share this post


Link to post
Share on other sites
If you're going to use the C/C++ standard library use _beginthread or _beginthreadex as you prefer. If you are using MFC use AfxBeginThread instead. If you're just using the raw Win32 api use CreateThread.

But threads don't just magically interfere with virtual pointers. How you create the thread is pretty much irrelevant to what you've described. You probably have some race condition caused by insufficient locking or possibly some lifetime issues (one thread deletes an object but another keeps using it).

Share this post


Link to post
Share on other sites
Hi, I didn't know that the C++ standard library allowed threads.

A few Quick questions:

What headers would I use?

What kind of threading operations are allowed; I have done threading for University in Java. Does C++ provide monitors like in Java?

Does the C++ threading approach create actual multi process programs so the seperate processes can be run on different processors & make use of dual core processors?

Thanks for any replies!

Share this post


Link to post
Share on other sites
Quote:
Original post by Cacks
Hi, I didn't know that the C++ standard library allowed threads.


It doesn't. I think you got the wrong impression from some of the previous posters.

Share this post


Link to post
Share on other sites
_beginthread() is an artifact of the implementation of Microsoft's C runtime library. _beginthread() will initialize certain variables in the C runtime library that will not get intialized if you create the thread another way such as CreateThread(). As such, if you use functions from Microsoft's C runtime library in the new thread, you should start the thread with _beginthread() or _beginthreadex(). Or you can use a library that wraps thread creation like boost that should use _beginthread() if building on a MSVC compiler.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!