Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 6th, 2006, 03:35 AM
jayesah@gmail.com
Guest
 
Posts: n/a
Default 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.

  #2  
Old December 6th, 2006, 07:35 AM
Gianni Mariani
Guest
 
Posts: n/a
Default Re: C++ Thread Class

jayesah@gmail.com wrote:
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 ?
>

There are quite a few examples of thread classes.

- Austria C++ (shameless plug)
- boost
- ACE

Then there are some docs on the standardization efforts:

http://www.open-std.org/jtc1/sc22/wg...006/n2094.html
http://www.artima.com/cppsource/threads_meeting.html

That should keep you fed with ideas for quite a while.
  #3  
Old December 6th, 2006, 12:25 PM
nutrina9@gmail.com
Guest
 
Posts: n/a
Default Re: C++ Thread Class


See following example, its's similar

class ScopeGuardBase
{
public:
void dismiss() const throw()
{
m_dismissed = true;
}

protected:
ScopeGuardBase() : m_dismissed(false)
{
}

ScopeGuardBase(const ScopeGuardBase& other)
: m_dismissed(other.m_dismissed)
{
other.dismiss();
}

~ScopeGuardBase()
{
}

mutable bool m_dismissed;

private:
// Disable assignment
ScopeGuardBase& operator=(const ScopeGuardBase&);
};

typedef const ScopeGuardBase& ScopeGuard;

template <class Obj, typename MemFunc>
class ScopeGuardImpl: public ScopeGuardBase
{
public:
ScopeGuardImpl(Obj& obj, MemFunc memFunc)
: m_obj(obj), m_memFunc(memFunc)
{
}

~ScopeGuardImpl()
{
if (!m_dismissed)
{
(m_obj.*m_memFunc)();
}
}
private:
Obj& m_obj;
MemFunc m_memFunc;
};

template <class Obj, typename MemFunc>
ScopeGuardImpl<Obj,MemFuncmakeGuard(Obj& obj,MemFunc fun)
{
return ScopeGuardImpl<Obj,MemFunc>(obj,fun);
}


Gerald

jayesah@gmail.com wrote:
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.
  #4  
Old December 6th, 2006, 11:55 PM
Kai-Uwe Bux
Guest
 
Posts: n/a
Default Re: C++ Thread Class

jayesah@gmail.com wrote:
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();
>
}
What about:

#include <tr1/memory// shared_ptr

class Thread {

struct Data {

virtual ~Data ( void ) {}

virtual
void start ( void ) = 0;
};

template < typename FunType, typename ArgType >
struct ThreadData : public Data {

FunType the_fun;
ArgType the_arg;

ThreadData ( FunType fun, ArgType arg )
: the_fun ( fun )
, the_arg ( arg )
{}

void start ( void ) {
// do whatever it takes to start the thread
}

};

std::tr1::shared_ptr< Data the_data;

public:

template < typename FunType, typename ArgType >
Thread ( FunType fun, ArgType arg )
: the_data ( new ThreadData<FunType,ArgType>( fun, arg ) )
{}

void start ( void ) {
the_data->start();
}

}; // Thread


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

struct B {
static void myfunB(B *ptr)
{
}
};

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

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

}

Best

Kai-Uwe Bux
  #5  
Old December 7th, 2006, 07:35 PM
TvN
Guest
 
Posts: n/a
Default Re: C++ Thread Class

Hi All,
Quote:
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 ?
Firstly take a look at boost::threads - it is pretty simple and small
;)

If you want to create another class for pthread do not forgot the next:
thread function (function passed to pthread_create) must have C
linkage, so it cannot be class/structure member.
Do not forget that it returns void* and takes void* as parameter, so
you should be careful with type conversion;)

As example it could be code below. Do not forget add correct error
handling. I hope it will be helpful ;)


//! Abstract class for thread execution.
class worker
{
public:
//! This method called in thread
virtual void execute() throw() = 0;
//! This method called when thread is canceled
virtual void cancel() throw() = 0;
//! this method called if error occur in thread
virtual void err_handle() throw()= 0;
//! Destructor.
virtual ~worker()
{}
};


extern "C" void* executor(void* b);

//! Main class for thread operations.
class thread
{
friend void* executor(void* b);
//! Main thread function.
//! \arg thread data.
//! \return thread result.
private:
//! disabled constructor
thread();
//! disabled constructor
thread(const thread& t);
//! disabled operator
thread& operator=(const thread& t);

public:
//! Constructor.
/*! Takes a pointer to worker object, and use it as
* thread function.
* \note In this case default thread attribute will be used.
*/
thread(worker* w)
: funct(w)
{
int r;
if ((r = pthread_attr_init(&attr)) != 0)
{
throw r;
}
}
//! Constructor.
//! \arg worker* w - worker object.
//! \arg const pthread_attr_t &attr_ - thread attribute.
thread(worker* w, const pthread_attr_t &attr_)
: attr(attr_), funct(w)
{ }
//! Destructor.
/*! Firstly thread is detached, and then it canceled.
* This approach is used because POSIX does not have other
approach
* to destroy other thread.
*/
virtual ~thread()
{
pthread_detach(tid);
pthread_cancel(tid);
}
//! Returns thread identifier (TID)
pthread_t getTID() const
{
return tid;
}
//! Returns TID of the thread in which this method called.
static pthread_t self()
{
return pthread_self();
}
//! Create new thread and returns it's TID.
//! \note In case of error exception with error code will be thrown

pthread_t create()
{
int r = pthread_create(&tid, &attr, executor, this);
if (r != 0)
{
throw r;
}
return tid;
}
//! Joins the thread. Thsi thread should be joinable.
//! \note In case of error exception with error code will be thrown

void join()
{
int r;
if ((r = pthread_join(tid, NULL)) != 0)
{
throw r;
}
}
//! Detaches the thread.
//! \note In case of error exception with error code will be thrown

void detach()
{
int r;
if ((r = pthread_detach(tid)) != 0)
{
throw r;
}
}
//! Detaches the thread which TID is tid_
//! \note In case of error exception with error code will be thrown

static void detach(pthread_t tid_)
{
int r;
if ((r = pthread_detach(tid_)) != 0)
{
throw r;
}
}
//! Cancels the thread.
//! \note In case of error exception with error code will be thrown

void cancel()
{
int r;
if ((r = pthread_cancel(tid)) != 0)
{
throw r;
}
}
//! Cancels the thread which TID is tid_.
//! \note In case of error exception with error code will be thrown

static void cancel(pthread_t tid_)
{
int r;
if ((r = pthread_cancel(tid_)) != 0)
{
throw r;
}
}
private:
pthread_t tid; //!< Thread's ID
pthread_attr_t attr;//!< Thread's attribute

worker* funct;//!< Thread's class function.
};


void* executor(void* b)
{
try{
thread *thr = reinterpret_cast<thread*>(b);
if (thr != NULL)
{
try
{
thr->funct->execute();
}catch(...)
{
thr->funct->err_handle();
}
}
}catch(...)
{}
return NULL;
}

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles