pthreads with C++

J

John

I have a member function in a class that is
metaprogrammed like the following

dinsert<D>::eval(M);

This in turn unrolls itself to (at compile time, D is a fixed constant)

dinsert<D>::eval(M);
dinsert<D-1>::eval(M);
dinsert<D-2>::eval(M);
....
....
dinsert<0>::eval(M);

These functions are all independent and I would
like them to run on different threads. I tried
using openMP but unfortunately, icc (intel's compiler
slows it down with threads). I would like to try
pthreads. Any ideas on how to parallelize it without
creating too much mess? (Like embedding M into a void * etc,
and the whole thing into a function..??!@#!@#)

Thanks,
--j
 
N

Nadina

Hi,

In order to pass variables to pthreads you can use either
-global variables, either the
-void* arg
parameter of function
int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void
*(*funct)(void*), void *arg);

Now with global variables, they are visible to all the threads and you
would need to lock them as each individual thread accesses the glob
variable (if M is modified).

With the (void* arg) you do not need to worry about locking.

Embeding M into a void * is as simple as:
(void *) &M

Nadina
 
J

John

The problem seems to be that I need

dinsert<D>::eval(M); call. The type of M depends on D.
And I cant do this at runtime. If I write a function that takes
both D and M as input, my code gets messed up since
then the compiler does not know the type of M at compile time.

Thanks for ur help,
--j
 
T

Torsten Robitzki

John said:
The problem seems to be that I need

dinsert<D>::eval(M); call. The type of M depends on D.
And I cant do this at runtime. If I write a function that takes
both D and M as input, my code gets messed up since
then the compiler does not know the type of M at compile time.

Thanks for ur help,
--j

Then pass a structur with a M and pointer to a function taking a M to
the thread function (note that the thread function have to have C binding).

template <unsigned D, typename M>
struct dinsert
{
static void eval(M);
static void eval(void* m)
{
eval(*static_cast<M*>(m));
}
};

struct dipatch
{
void(func*)(void*);
void* param;
dispatch(void(f*)(void*), void* p)
: func(f), param(p) {}
};

extern "C" void* thread_func(void* p)
{
std::auto_ptr<dispatch> disp(static_cast<dispatch*>(p));
disp->func(disp->param);
}

int main()
{
// call dinsert<5,int>::eval(17);
pthread_create(thread_func,
new dispatch(&dinsert<5,int>::eval(int), 17), 0);
}

I did not compiled it, but I hope you can get the idea. And as usual
boost have this already.
for deferred call function calls:
http://www.boost.org/doc/html/function.html
for threads:
http://www.boost.org/doc/html/threads.html

regards
Torsten
 
Joined
Jun 2, 2007
Messages
1
Reaction score
0
Can I invoke the member function of a class from a thread? if so, is it the same way as any ordinary function? Can anyone please cite an eg?
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top