Problem creating new posix thread

D

darklupine

I'm working on a project for a class at school. In order to complete
the project, I have to create a new thread, but I'm not overly certain
on how to do so. I have it coded, but it still throws an error, and I
can't figure out why it is doing so.

The create line reads:

pthread_create(&thread1, NULL, (void *)&CourNet::recieve, NULL);

CourNet is my class name, and recieve is the function I'll be calling.
recieve returns (NULL) and its header is the following:

void * CourNet::recieve(void * pointer)

The error it returns when I try to compile (using g++, I'm not sure
what version it is) is as follows:

CourNet.cpp:103: converting from 'void*(CourNet::*)(void*)' to 'void*'
CourNet.cpp:103: invalid conversion from 'void*' to 'void*(*)(void*)'

I've mucked with the code a bit trying to fix it, adding the void *
return type on recieve instead of a regular void, and adding the void *
pointer parameter, as well as the return (NULL).

Can anyone give me any suggestions on how I might correct this error?
If you need any more information, let me know.

Thanks!
 
A

Artie Gold

darklupine said:
I'm working on a project for a class at school. In order to complete
the project, I have to create a new thread, but I'm not overly certain
on how to do so. I have it coded, but it still throws an error, and I
can't figure out why it is doing so.

The create line reads:

pthread_create(&thread1, NULL, (void *)&CourNet::recieve, NULL);

CourNet is my class name, and recieve is the function I'll be calling.
recieve returns (NULL) and its header is the following:

void * CourNet::recieve(void * pointer)

The error it returns when I try to compile (using g++, I'm not sure
what version it is) is as follows:

CourNet.cpp:103: converting from 'void*(CourNet::*)(void*)' to 'void*'
CourNet.cpp:103: invalid conversion from 'void*' to 'void*(*)(void*)'

I've mucked with the code a bit trying to fix it, adding the void *
return type on recieve instead of a regular void, and adding the void *
pointer parameter, as well as the return (NULL).

Can anyone give me any suggestions on how I might correct this error?
If you need any more information, let me know.

Thanks!
Without getting into any specifics about using the pthread calls (which
would be off topic here) I advise you to read the errors carefully.

Think about what pthread_create expects as its third argument (hint:
it's *not* a member function; functions and member functions are two
different things).

Next thing to do is a Google search; believe it or not, you're not the
first to encounter this! [;-)]

Oh -- and it's `receive'.

HTH,
--ag
 
D

darklupine

Hmm...I think I may have cleared it up. If I am undestanding you
correctly, and its not making some runtime error or somesuch, moving
the recieve function (now that I know its mispelled, I'll go back and
fix the spelling...oops!) outside of the class and changing the
pthread_create call to:

pthread_create(&thread1, NULL, &recieve, NULL);

seems to have fixed the error. Thanks a lot! Neither of my profs are
overly familiar with threading, and I've been basically teaching
myself. Wish I had thought to come here earlier, instead of pounding my
head against the wall...
 
I

Ian

darklupine said:
Hmm...I think I may have cleared it up. If I am undestanding you
correctly, and its not making some runtime error or somesuch, moving
the recieve function (now that I know its mispelled, I'll go back and
fix the spelling...oops!) outside of the class and changing the
pthread_create call to:

pthread_create(&thread1, NULL, &recieve, NULL);
To be strictly correct (which is a good thing if you are learning!),
declare receive an extern "C" function.

extern "C" {
void* receive( void* );
}

Always do this when passing a C++ function pointer to a C library.

Ian
 
G

Gianni Mariani

darklupine said:
I'm working on a project for a class at school. In order to complete
the project, I have to create a new thread, but I'm not overly certain
on how to do so. I have it coded, but it still throws an error, and I
can't figure out why it is doing so.

The create line reads:

pthread_create(&thread1, NULL, (void *)&CourNet::recieve, NULL);

CourNet is my class name, and recieve is the function I'll be calling.
recieve returns (NULL) and its header is the following:

void * CourNet::recieve(void * pointer)

Look at the posix thread layer in Austria C++.

You REALLY want a layer like this. This works for both win32 and *nix
systems.
 
B

ben

darklupine said:
Hmm...I think I may have cleared it up. If I am undestanding you
correctly, and its not making some runtime error or somesuch, moving
the recieve function (now that I know its mispelled, I'll go back and
fix the spelling...oops!) outside of the class and changing the
pthread_create call to:

pthread_create(&thread1, NULL, &recieve, NULL);

seems to have fixed the error. Thanks a lot! Neither of my profs are
overly familiar with threading, and I've been basically teaching
myself. Wish I had thought to come here earlier, instead of pounding my
head against the wall...
You can still put your function in a class, but make it static.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top