Threads ! I'am off topic, but I hope someone could help me!

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();	

}

Perhaps it’s because you’re passing a non-static class method to a C
function that expects another C function. Try making the method static
and passing it in to pthread_create as myClass::doSomething

–Ryan

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 functionfunction’ 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

I will deatch is as Jave do.

Create a run method that make the Thread to start.

BTW What is the problme with the code ?

Mensaje citado por: Anes Lihovac :> 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();
}

There are two problems with this code:

The first problem is that you are passing a C++ method to
pthread_create(). Even if you made the method static this is still not
proper technique. (Linking is fundamentally different in C++ and C.)

The second problem is that you are “doing something” with the class before
it is fully constructed. The constructor never finishes before you enter
the doSomething() method.

Something like the following will yield better results:

class myClass
{
public:
void start();
void doSomething();
};

extern “C” void* startup_myClass(void* class)
{
myClass* object = static_cast<myClass*>(class);
object->doSomething();
}

void myClass::start()
{
pthread_t thread;
pthread_create(&thread, NULL, startup_myClass, this);
}

You then instantiate a myClass instance and call “start()” on it.

Paul Braman
@Paul_Braman

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 functionfunction’ 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

Paul Braman wrote:

proper technique. (Linking is fundamentally different in C++ and C.)

extern “C” void* startup_myClass(void* class)

Symbol names are different, but linking is the same. I really don’t
think you need the ‘extern “C”’. A C++ static method is a C function
with a very strange name and some different compile-time scoping.

Non-static methods are often implemented as a regular C function that
takes a pointer to the object as the first parameter, but some C++
compilers generate different code (passing the ‘this’ pointer in
registers for example).–
Pierre Phaneuf
Systems Exorcist

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 functionfunction’ 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