 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
jayesah@gmail.com Guest
|
Posted: Wed Dec 06, 2006 10:10 am Post subject: C++ Thread Class |
|
|
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
|
Posted: Thu Dec 07, 2006 1:36 am Post subject: Re: C++ Thread Class |
|
|
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
|
Posted: Thu Dec 07, 2006 10:10 am Post subject: Re: C++ Thread Class |
|
|
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 |
|
 |
|
|
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
|
|