C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C++ Thread Class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
jayesah@gmail.com
Guest





PostPosted: Wed Dec 06, 2006 10:10 am    Post subject: C++ Thread Class Reply with quote



Hi All,

I am writting a Thread class with using pthread library. I have some
problem in saving thread function type and argument type. How to make
Thread class generic ?

/* This is my global Function */

template < class FunType, class ArgType>
Thread makeThread(Funtype fun, ArgType arg)
{
Thread thr;

/* How to save Funtype and ArgType in the Thread object ?? */

return thr;
}


/* This is my thread class */
class Thread
{

void start()
{
/* pthread_create here to create a new thread */
}

};


class A
{
static void myfunA(A *ptr)
{
/* This function will run as a new thread); */
}
};

class B
{
static void myfunB(B *ptr)
{
}
}

main()
{
A *objptrA = new A();
Thread thrA = makeThread(myfunA, objptrA);
thrA.start();

B *objptrB = new B();
Thread thrB = makeThread(myfunB, objptrB);
thrB.start();

}

Thanks
Jayesh Shah.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
red floyd
Guest





PostPosted: Thu Dec 07, 2006 1:36 am    Post subject: Re: C++ Thread Class Reply with quote



alex wrote:
Quote:
jayesah (AT) gmail (DOT) com wrote:
Hi All,

I am writting a Thread class with using pthread library. I have some
problem in saving thread function type and argument type. How to make
Thread class generic ?

Have you considered inheritance?

class Thread
{
public:
void start(void);
virtual void run(void) = 0;
};

class MyThread
: public Thread
{
public:
virtual void run(void);
};

Alex Shulgin


Better would be:

class Thread
{
public:
static void start(void *param)
{
static_cast<Thread*>(param)->run();
}
private:
virtual void run() = 0;
protected:
virtual ~Thread() {}
};

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Guest






PostPosted: Thu Dec 07, 2006 10:10 am    Post subject: Re: C++ Thread Class Reply with quote



Have you considered using something like C++ BOOST threads?

http://www.boost.org/doc/html/threads.html


Arash Partow
________________________________________________________
Be one who knows what they don't know,
Instead of being one who knows not what they don't know,
Thinking they know everything about all things.
http://www.partow.net


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.