Anes Lihovac wrote:
Hello !
I have written some classes which are multithreaded.
I am creating a thread out of a method which is called
once the object is created, eg:
class myClass
{
public:
myClass(){StartServer();}
void StartServer();
void doSomething();
}
…
.
void myClass::StartServer()
{
pthread_t thread;
pthread_create(&thread,NULL,&doSomething,(void*)this);
}
Okay this worked fine at every time(during compiling there are a few
warnings!),
but now I have changed my compiler from egcs-2.95.1 to egcs-2.95.2 and
it won’t compile
any more.It breaks with an error message:
test.cpp: In method void myClass::startServer()': test.cpp:30: no matches converting function
function’ to type `void *
(*)(void *)'
test.cpp:13: candidates are: void myClass::function()
I don’t know why, and how to solve it.
I know their is a threads newsgroup, but there wasn’t someone who could
help me.
Thanks in advance and best regards
Ciao
Anes
Try something like this:
#include <pthread.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
class Pthread
{
public:
// Create a new thread
Pthread(void (__start_routine)(void *),void *__arg);
// Die gracefully
~Pthread();
void *arg(void); // return passed argument
void *(*startup)(void *); // What function to execute
private:
void *__argdata;
};
extern “C” {static void *thread_start(Pthread *thread)
{
void *x;
// Start the routine that the user _actually_ wanted
x=thread->startup(thread->arg());
// at the end, we delete the thread
delete thread;
// pthread_exit(x);
return x;
}
}
Pthread::Pthread(void (__start_routine)(void *),void *__arg)
{
#if USEFUL_OCCASIONALLY
struct rlimit rlim;
getrlimit(RLIMIT_NOFILE,&rlim);
if(rlim.rlim_cur>200)
{
rlim.rlim_cur=200;
rlim.rlim_max=200;
}
setrlimit(RLIMIT_NOFILE,&rlim);
#endif
pthread_t thread_id;
// Store the argument data
__argdata=__arg;
// Store the startup position
startup=__start_routine;
// Create the thread, passing a pointer to self
while(pthread_create(&thread_id,0,thread_start,(void *)this))
{
// Thread creation failed. What now?
sleep(1);
}
}
void *Pthread::arg(void)
{
return __argdata;
}
Pthread::~Pthread()
{
// Nothing.
}
Then create a new thread with:
new Pthread(start_func,arg_val);
(Yes, the pointer isn’t stored. The class deconstructs itself)
D