Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Programming Threads > pthread_create ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 4054 of 4146
Post > Topic >>

pthread_create C++ and variable problems

by Jeff <jeep@[EMAIL PROTECTED] > Sep 25, 2008 at 05:10 PM

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
 




 1 Posts in Topic:
pthread_create C++ and variable problems
Jeff <jeep@[EMAIL PROT  2008-09-25 17:10:50 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Nov 22 8:19:38 CST 2008.