method type void* (*) (void*)

G

Gary Wessle

I am getting

task.cpp:109: error: argument of type 'void (Spac_task::)()' does not
match 'void* (*)(void*)'

when trying to execute the line
int thr_id = pthread_create(&p_thread, NULL, spc_optm, NULL);

where

void Spac_task::spc_optm(){/*...*/}

if I change it to
void* Spac_task::spc_optm(void*){/*...*/}

then I get

task.cpp:109: error: argument of type 'void* (Spac_task::)(void*)'
does not match 'void* (*)(void*)'

I even tried

static void* (*) spc_optm(void*);

static void* (*) Spac_task::spc_optm(void*){/*...*/}

that did not cut it.

any guide?

thanks alot
 
P

Puppet_Sock

Gary said:
I am getting

task.cpp:109: error: argument of type 'void (Spac_task::)()' does not
match 'void* (*)(void*)'

when trying to execute the line
int thr_id = pthread_create(&p_thread, NULL, spc_optm, NULL);

where

void Spac_task::spc_optm(){/*...*/}

Sure. You are trying to make a member function the thread function.
You can't. A thread function has to be static or global.

The function spc_optm is a member function. It has an implied this
pointer. That is, it needs a class instance to run. That;s likely why
you were trying to do this, to put an object in charge of a thread.
But how does the call you made know about the instance of the
class you want it to be handled by?

There are a few ways to deal with this. One is read the FAQ, as
it gets asked fairly regularly. Look up "callback" and such. This
is a variant on a callback.
Socks
 
G

Gianni Mariani

Gary said:
I am getting

task.cpp:109: error: argument of type 'void (Spac_task::)()' does not
match 'void* (*)(void*)'

when trying to execute the line
int thr_id = pthread_create(&p_thread, NULL, spc_optm, NULL);

where

void Spac_task::spc_optm(){/*...*/}

if I change it to
void* Spac_task::spc_optm(void*){/*...*/}

then I get

task.cpp:109: error: argument of type 'void* (Spac_task::)(void*)'
does not match 'void* (*)(void*)'

I even tried

static void* (*) spc_optm(void*);

static void* (*) Spac_task::spc_optm(void*){/*...*/}

that did not cut it.

any guide?

thanks alot

You need a non member or static "start routine" method - I pulled this
from at_posix_thread_cpp.i from Austria C++ ...

void * start_routine( void * i_task )
{
Spac_task * l_this_task = static_cast<Spac_task *>( i_task );

l_this_task->spc_optm();
....


// You also need to get the thread_id so you can join to it.
int l_result = pthread_create(
& m_task_context->m_thread_id,
static_cast<const pthread_attr_t *>( 0 ),
& start_routine,
static_cast<void *>( pointer_to_Spac_task )
);

if ( 0 != l_result )
{
abort();
}


Then again - you just use Austria's thread (Task) class or boost's
thread classes.
 
G

Gary Wessle


thanks

following the example in the FAQ,


void spac_task::spc_optm(){/*...*/}

void Spac_task::preform(){
/*...*/
pthread_t p_thread;
int thr_id = pthread_create(&p_thread, NULL, spc_optm, NULL);
}

is being replaced by:

Spac_task* signal_handle;
void spac_memb_f_wrap(){
signal_handle->spc_optm;
}
signal(SIGINT, spac_mem_f_wrap);

but I don't see how the threading takes place in above replacement.
is it done behind the seen?

thanks
 
G

Gary Wessle


I read the FAQ relating to this problem which is at
http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2

I am having hard time applying the principle to my problem.

the principle is:

// Wrapper function uses a global to remember the object:
Waiter* object_which_will_handle_signal;
void Waiter_memberFn_wrapper()
{
object_which_will_handle_signal->waiting();
}

int main(){
signal(SIGINT, Waiter_memberFn_wrapper);
}


how do I apply this to my code below?

****************************************************************
#include <boost/thread/thread.hpp>
#include <iostream>
using namespace std;


class Waiter {
short customers_serv;
public:
Waiter(short);
void waiting();
void preform();
};
Waiter::waiter(short cs) : customers_serv( cs ) {
preform();
}

void Waiter::waiting(){
customers_serv--; // modify the object
cout << "serving " << customers_serv << " customers" << endl;
}
void Waiter::preform(){
boost::thread thrd( waiting );
thrd.join();
}

int main()
{
Waiter jack(5);
Waiter sofi(2);
}
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top