C++ Thread Class

J

jayesah

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.
 
G

Gianni Mariani

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/wg21/docs/papers/2006/n2094.html
http://www.artima.com/cppsource/threads_meeting.html

That should keep you fed with ideas for quite a while.
 
N

nutrina9

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,MemFunc> makeGuard(Obj& obj,MemFunc fun)
{
return ScopeGuardImpl<Obj,MemFunc>(obj,fun);
}


Gerald
 
K

Kai-Uwe Bux

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
 
T

TvN

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 ?
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top