Multithreading MADNESS!!!

Started by
14 comments, last by anonuser 18 years, 8 months ago
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?
Quit screwin' around! - Brock Samson
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.
...virtual pointers? Interferes with other threads? What are you talking about?
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
visit my website at www.kalmiya.com
Best to read up on the term atomic and critical section before attempting multi-threading.
... 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.
visit my website at www.kalmiya.com
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).
-Mike
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!
Reject the basic asumption of civialisation especially the importance of material possessions
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.
--Michael Fawcett
_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.

This topic is closed to new replies.

Advertisement