Im trying to create a multithreaded C++ server. The code compiles
cleanly and runs, but then I SEGV because apparently my private member
variables arent what they were when I 'new'd the class.
Here's my code:
main server loop:
---------------------
SERV_ARGS *largs; // struct to pass
while(loop)
{
int this_fd = accept(m_fd,(SOCKADDR *)&m_sa,&m_sa_len);
if (this_fd > 0)
{
ServFD *sfd = new ServFD(this_fd);
sfd->Start(largs);
usleep(200);
}
}
in ServFD: // some code excluded for brevity
-------------------------------------------------------
extern "C" {
void *start_thread(void *args)
{
ServFD *serv = (ServFD *)args;
serv->ServFDThreadFunc();
return 0;
}
};
ServFD::ServFD(int fd) :
m_fd(fd),
m_run(false)
{
}
int
ServFD::Start(SERV_ARGS *args)
{
int ret = pthread_create(&thread_id, NULL, start_thread, (void
*)args);
if (ret != 0)
{
logit(TERR, "ERROR pthread_create failed : %s <%d>",
strerror(errno), errno);
return -1;
}
//m_run = true; <---- Gets a SEGV
return 0;
}
void
ServFD::ServFDThreadFunc()
{
while (m_run)
{
handle client here etc
}
}
My problem seems to be that my private member variable m_fd is
no longer what I sent in on my 'new'. It was 4, but by the time it
gets
to 'ServFDThreadFunc() its a huge number (bashed - 10732512).
Also when I set m_run = true above, it will SEGV. Its like the values
were never instantiated. So to get beyond that I stubbed out the
m_run
stuff but the same was true for m_fd and Im assuming anything else
I would otherwise use.
Can someone tell me why this is happening and how to fix it?
My assumption is that something is happening after the call to
pthread_create and somehow my ServFD class is no longer the
same ServFD class I 'new'd the first time, but Im unsure.
Any help is appreciated.
Thanks


|