C2C++ itch

V

Vio

While converting a C file to C++
the C line:
THREAD_CREATE(tid, (void *)server_thread, sss);

complains with:
ANSI C++ forbids implicit conversion from `void *' in argument passing.

some context info:

the macro THREAD_CREATE is:
#define THREAD_CREATE(tid, entry, arg) do { _beginthread((entry), 0,
(arg));\
(tid) = GetCurrentThreadId(); \
} while (0)
#endif

server_thread is:
void THREAD_CC server_thread(void *arg){...}

Someone suggested to make it explicit, but I don't know how to do that
right now (or perhaps I don't understand what he ment). Declaring
server_thread explicit doesn't work, since that only works for
constructors (which server_thread is not).

Other suggestions?

Cheers,
Vio
 
V

Vio

Ron said:
It's not even valid in C. The arg to beginthread isn't a void*, it's a pointer
to a function. Server_thread is also a pointer to function. First try, get
rid of the the (void*) cast.




Hello Ron, thx for your reply.

strange this is illegal C, as it comes from an official O'Reilly book:
http://www.opensslbook.com/NSwO-1.3.tar.gz
in file ssl/server.c

Yeah, essentially I am trying to move that file to c++, in order to
add some c++ facilities, like fstream, std::string and such.

Getting rid of the (void *) cast.
You mean something like:
THREAD_CREATE(tid, server_thread, sss);

Complains with:
myserver.cpp:334: passing `void (*)(void *)' as argument 3 of
`pthread_create(pthread_t *, const pthread_attr_t *, void * (*)(void *),
void *)'

Ok, I'm kind of lost here. To make it more understandable,
I expanded the macro to:

//THREAD_CREATE(tid, (void *)server_thread, ssl);
do { _beginthread(((void *)server_thread), 0, (ssl));
(tid) = GetCurrentThreadId();
} while (0)

"_beginthread()", well, I don't know where it's coming from
(grep-ing some include dirs returned empty), but anyways

"server_thread" is a function in server.c
The (void *) cast seems needed by gcc, as it compiles ok.
g++ chokes with the forementionned whining.

Ok, I'm pretty lost here (well, I am just a newbie after all).
More hints appreciated :)

Cheers,
Vio
 
R

Ron Natalie

Vio said:
While converting a C file to C++
the C line:
THREAD_CREATE(tid, (void *)server_thread, sss);
It's not even valid in C. The arg to beginthread isn't a void*, it's a pointer
to a function. Server_thread is also a pointer to function. First try, get
rid of the the (void*) cast.
 
D

Denis Remezov

Vio said:
While converting a C file to C++
the C line:
THREAD_CREATE(tid, (void *)server_thread, sss);

complains with:
ANSI C++ forbids implicit conversion from `void *' in argument passing.

some context info:

the macro THREAD_CREATE is:
#define THREAD_CREATE(tid, entry, arg) do { _beginthread((entry), 0,
(arg));\
(tid) = GetCurrentThreadId(); \
} while (0)
#endif

server_thread is:
void THREAD_CC server_thread(void *arg){...}

Well, my guess is that you are compiling this under an OS
other than MS Windows. If this is so, then your assumption about
the macro expansion is incorrect. From your other post it
appears that you are actually using pthreads. In particular,
THREAD_CREATE uses pthread_create() rather than _beginthread().
pthread_create() expects

void* server_thread(void* arg); // (note the return value type)

for the "entry" parameter. Make sure that you've got the
platform-specific parts right, double-check the function
signatures, and you should be able to simply write

THREAD_CREATE(tid, server_thread, sss);

Denis
 
V

Vio

Denis said:
Well, my guess is that you are compiling this under an OS
other than MS Windows. If this is so, then your assumption about
the macro expansion is incorrect. From your other post it
appears that you are actually using pthreads. In particular,
THREAD_CREATE uses pthread_create() rather than _beginthread().
pthread_create() expects

void* server_thread(void* arg); // (note the return value type)

for the "entry" parameter. Make sure that you've got the
platform-specific parts right, double-check the function
signatures, and you should be able to simply write

THREAD_CREATE(tid, server_thread, sss);

Denis

Hi Denis,
Thanks for your reply.
Yes, I am using pthreads, and my OS is Debian (linux).

Oops, you got me here!! For some reason, I completely missed that
#ifdef. You are completely right, I expanded the win32 macro instead of
the pthread one. Let's call it channel vision :(
Ok, I'll correct that, and see what happens.

Many thanks for pointing that out,
Vio
 
V

Vio

Denis said:
Well, my guess is that you are compiling this under an OS
other than MS Windows. If this is so, then your assumption about
the macro expansion is incorrect. From your other post it
appears that you are actually using pthreads. In particular,
THREAD_CREATE uses pthread_create() rather than _beginthread().
pthread_create() expects

void* server_thread(void* arg); // (note the return value type)

for the "entry" parameter. Make sure that you've got the
platform-specific parts right, double-check the function
signatures, and you should be able to simply write

THREAD_CREATE(tid, server_thread, ssl);


Ok, let's see now:

The linux-version of the expanded macro is:

pthread_create(&(tid), NULL, ((void *)server_thread), (ssl));

which complains as initially noticed:
myserver.cpp:360: ANSI C++ forbids implicit conversion from `void *' in
argument passing


What compiler expects is actually (from "pthread.h"):
extern int pthread_create (pthread_t *__restrict __thread,
__const pthread_attr_t *__restrict __attr,
void *(*__start_routine) (void *),
void *__restrict __arg) __THROW;

Hm, I guess my problem is that I don't quite understand the 3rd
argument. I mean, really, what does

void *(*__start_routine) (void *)

actually mean? Well, I could make some guesses, but I don't want to
scare people who might read this :)


Well, just to elliminate some obvious possibilities,

pthread_create(&(tid), NULL, server_thread, (ssl));

myserver.cpp:369: passing `void (*)(void *)' as argument 3 of
`pthread_create(pthread_t *, const pthread_attr_t *, void * (*)(void *),
void *)'

Hm, so I'm essentially missing an asterix. So developping story:
if arg `server_thread' is `void (*)(void *)',
how do I make it into a `void * (*)(void *)' ?

Vio
 
D

Denis Remezov

Vio said:
[...]
Hm, so I'm essentially missing an asterix. So developping story:
if arg `server_thread' is `void (*)(void *)',
how do I make it into a `void * (*)(void *)' ?

You are passing the address of the wrong function. Type
void* (*)(void*) means "pointer to a function taking void* and
returning void*". That means you have to use

void* server_thread(void* arg) {...}

instead of

void server_thread(void* arg) {...}


As a side note, in a perfect world I would also indicate the proper
language linkage:
extern "C" void* server_thread(void* arg) {...}
though a bare "C++" function will probably have the same calling
convenvtions anyway. OTOH, in the MS world you would have to use
the calling conventions specification THREAD_CC instead:
void* THREAD_CC server_thread(void* arg) {...}

Denis
 
V

Vio

Denis Remezof and Ron Natalie, many thanks for your help.
Essentially, doing

Denis said:
void* THREAD_CC server_thread(void* arg) {...}

gives warning:

myserver.cpp:302: warning: control reaches end of non-void function
`server_thread(void *)'

but it compiles, and I can live with that :)


Hm, some unwanted "undefined reference" during linking:

g++ -g -o myserver myserver.o reentrant.o common.o -L/d/ssl/run/lib
-lssl -lcrypto -Wall -pthread

but I've been there before, so I'll eventually figure this one out.
Just wondering, is there some special requirements when linking
against C object code from cpp? reentrant.o and common.o are C code,
which I didn't touch (my cpp code includes common.h which includes
reentrant.h). I recall something similar for the client code (did that
some weeks ago), and my fix, if I recall, was to put all functions
from all these 3 files in the same cpp file
(then did some editing there, put some as class methods,
while other remained C-like -without the `extern "C"' statement).

Anyway, many thanks,
Vio
 
V

Vio

Vio said:
Hm, some unwanted "undefined reference" during linking:

g++ -g -o myserver myserver.o reentrant.o common.o -L/d/ssl/run/lib
-lssl -lcrypto -Wall -pthread

but I've been there before, so I'll eventually figure this one out.
Just wondering, is there some special requirements when linking
against C object code from cpp? reentrant.o and common.o are C code,
which I didn't touch (my cpp code includes common.h which includes
reentrant.h). I recall something similar for the client code (did that
some weeks ago), and my fix, if I recall, was to put all functions
from all these 3 files in the same cpp file
(then did some editing there, put some as class methods,
while other remained C-like -without the `extern "C"' statement).


took me a little to figure it out, but embedding the C functions
in `extern "C"{...}' fixed that.
Vio
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top