Joe wrote:
> > > I am looking for documentation on how to implement threads
> > with C++. My
> > > problem is that I call a function from a class via
> > pthread_create and
> > > pass a (void *)-pointer with parameters. But this pointer
> > points
> > > anywhere, but not to the structure I want...
> >
> >  would this pointer happen to be relative to a class/struct ?
> >
> >  Threads only understand C-style pointers (i.e. no 'this'
> > pointer).
> > you need to provide C-style interface functions, or pass a
> > struct
> > containing the 'this' and any other pointers you need to the
> > thread
> > startup func.

Steven Roberts wrote:
> You want want to take a look at www.ysl.org.  (I'm the principle
> maintainer so I'm a bit biased, but others who have used the YSL's think
> they are not too bad either).
> 
> Basically the YSL's are a growing set of C++ class libraries.  In there
> are C++ wrappers for threads and semphores.  All classes work fine under
> both win32 and linux.  you will need a very recent egcs to handle the
> some of the template classes, but if nothing else you can get some ideas
> on doing the thread stuff in C++ by looking at the code.

The YSLs rule!!! (Completely non-biased opinion from the guy sitting
across the room from Steve and who wrote the thread class:)

Anyways, just put a static member function in your thread class and have
it call a non-static member function off a this pointer... say:

class YThread
{
        public:
                YThread();
                virtual ~YThread() { pthread_join(hThread, NULL); }
                // note that the derived thread better be dead before we hit this
destructor or the
                // thread will be dealing with member of a destructed object.
                // This is frequently solved with Terminate_v() and WaitForExit_v()
style functions

                void Start_v() { pthread_create(&hThread, NULL, &YThread::Execute_v,
this); }

        protected:
                virtual void Run_v() = 0;
                static void* Execute_pv(void *pvThread) {
((YThread*)pvThread)->Run_v(); }
};

It's about that simple... *especially* if you don't support the rather
problematic features of suspend and resume. There are issues you have to
deal with about how to deal with the thread handle and possible race
conditions but this is the core. Better yet just use the YSLs... they'll
make your life easy.

Which as far as compiler support goes:
- Linux egcs or ming egcs (best compiler... must have build from CVS in
the last 2-3 months... gcc 2.95 will likely work when it comes out)
- Borland C++ 5.01 (works well)
- VC++ 5.0 SP3 and 6.0 (kind of work as long as you stay away from the
binary tree based stuff)
- BC++ Builder 3.0 (sucks as bad as VC++)

Brian Macy
-
Linux SMP list: FIRST see FAQ at http://www.irisa.fr/prive/mentre/smp-faq/
To Unsubscribe: send "unsubscribe linux-smp" to [EMAIL PROTECTED]

Reply via email to