By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,903 Members | 1,973 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,903 IT Pros & Developers. It's quick & easy.

Problem creating new posix thread

P: n/a
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!

Nov 24 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
darklupine wrote:
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

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Nov 24 '05 #2

P: n/a
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...

Nov 24 '05 #3

P: n/a
Ian
darklupine wrote:
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
Nov 24 '05 #4

P: n/a
darklupine wrote:
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.
Nov 25 '05 #5

P: n/a
ben
darklupine wrote:
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.
Nov 26 '05 #6

P: n/a
Ian
ben wrote:
darklupine wrote:
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.


No, make it an extern "C" friend.

Ian
Nov 26 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.