Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pthread_create 1

Status
Not open for further replies.

Skute

Programmer
Jul 21, 2003
272
GB
Hi,

im having trouble launching some threads under linux.
I get the following errors and i have no idea why!

g++ xSockSrv.cpp -o xSockSrv.o -lpthread
xSockSrv.cpp: In member function `bool xSockSrv::AcceptConnectionLoop(void
(*)(tagClient*), void (*)(tagClient*), void (*)(tagClient*, char*, long
unsigned int))':
xSockSrv.cpp:169: invalid conversion from `void*' to `void*(*)(void*)'



Code:
iResult = pthread_create(&m_ptAcceptID, NULL, (void *)&AcceptConnectionLoopThread, this);


Makefile lines:

xSockSrv.o: xSockSrv.cpp xSockSrv.h
g++ -c xSockSrv.cpp -o xSockSrv.o



Any suggestions are most welcome!
(I have included <pthread.h> and linked to pthread library).
 
As far as I can understand, AcceptConnectionLoopThread is the entry function for your threads.

Third argument to pthread_create() ie entry function is of the type:

void *start_func(void *);

And in your API call, you are typecasting it to (void *) which is wrong.

You shall have a function like

void *start_func(void *arg)
{
// Do Something
while(1)
{
sleep(1);
}
}

And call your API as
pthread_create((&m_ptAcceptID, NULL, start_func, this);

So problem is with your API calling.
 
ahh right thanks alot! works perfect now :)

i only have 1 more trouble...

getch()...

im using it to see if the user wants to quit the application...but i get segmentation fault whenever the application is run (compiles fine)

unsigned char cQuit = '\0';

printf(&quot;Accepting connections on port %d\n\n&quot;, g_pServer->GetPort());
printf(&quot;Press q to stop the server\n&quot;);
cQuit = getch();
if ((cQuit == 'q') || (cQuit == 'Q'))
{
// User wants to quit
....


thats the code, i have the <curses.h> and -lcurses but i still get the error.

:confused:
 
I hope you have done initscr() of curses. Because yu have to initialize screen before calling any of its functions.
 
oh right, once again thanks for your quick responses!

ive done that now, but it made the screen go blank until the application quit? normal or should i be calling another function before that?
 
Hmmm... Its normal.. Curses is fofr graphics like programming... So as soon as you do a initscr(), you have switched on to a virtual screen mode.. Incase you were printing something, you will not be able to do it with printf()/Cout.. Curses provides its own APIs for that..

I have used it but a long time back. I dont remember the APIs currently. Google for Curses.h and you will get a lot of sites explaining the usage.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top