linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* RE: Multithreading with C++ - solved
@ 2005-04-08 14:04 Huber, George K RDECOM CERDEC STCD SRI
  2005-04-08 23:04 ` Glynn Clements
  0 siblings, 1 reply; 2+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2005-04-08 14:04 UTC (permalink / raw)
  To: linux-c-programming

All,

Thanks for all of the suggestions.  Actually, Eric hit the nail on the head - I was forgetting to 
link to libpthread.  Knew I had to, remember thinking that I need to change my makefile and 
add libpthread to the list of libraries that I was linking to.  Would have bet money that I did 
this.  Oh well....

Now this bring up an interesting question - how can a program using pthread compile cleanly
when it is not linked to libpthread?  I would expect unresolved externals on all pthread functions?

George


-----Original Message-----
From: linux-c-programming-owner@vger.kernel.org [mailto:linux-c-programming-owner@vger.kernel.org]On Behalf Of Huber, George K RDECOM CERDEC STCD SRI
Sent: Thursday, April 07, 2005 1:02 PM
To: linux-c-programming@vger.kernel.org
Subject: Multithreading with C++


All,

This may be a little off topic, but I though I would try here first.

I am writting a multi-threaded program and would like to have each 
thread `managed' by a class.  I have attempted to do this like this,
using the pthreads library:

<code>
====================begin cthread.h

class CThread
{
public:
    CThread(void*);
    ~CThread();

    ....

private:
    static CThread*     m_pThis;
    pthread_t             m_tid;
    static void*           threadfun(void*);

   ....
}

==================== begin cthread.cpp

CThread*  CThread::m_pThis = NULL;

CThread::CThread(void* arg)
{
    int      ret;

    m_pThis = this;
    if(0 != (ret = pthread_create(&m_tid, NULL, threadfun, arg)))
    {
         fprintf(stderr, "Thread creation failed");
   }
}   

void*  threadfun(void* arg)
{
     printf("Starting thread\n");
     ....
}

</code>

The code compiles without warnings or errors, except when I try and 
run the application I get a segmentation fault at the pthread_create.  
Trying to run under GDB, I am able to examine the value of the 
variables and they seem consisten with what I would expect

// stop on the pthread_create line in the constructor...

(gdb) print m_pThis
$1 = (CThread*) 0xfee756b0
(gdb) print this
$2 = (CThread*0xfee756b0
(gdb) print m_tid
$3 = 134514818               // to be expected because I have not initialized m_tid yet
(gdb) print &m_tid
$4 = (pthread_t*)0xfee756d8
(gdb) print threadfun
$5=&CThread::threadfun(void*)

// attempt the pthread_create line ....

Program received signal SIGSEGV, Segmenatation fault
0x0000000 in ?? ()
(gdb) where 
#0 0x00000000 in ?? ( )
#1 0x0804a6bb in CThread (this=0xfee756b0, args=0x0) at ....
#2 0x08049aad in daemon_main(argc=3, argv=0xfeec42d4) at ....
#3 0x08049193 in main(argc=3, argv=0xfeec42d3) at main.cpp: 83

Any idea on what is going on, or how to fix?

Thanks, 
George Huber

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 2+ messages in thread

* RE: Multithreading with C++ - solved
  2005-04-08 14:04 Multithreading with C++ - solved Huber, George K RDECOM CERDEC STCD SRI
@ 2005-04-08 23:04 ` Glynn Clements
  0 siblings, 0 replies; 2+ messages in thread
From: Glynn Clements @ 2005-04-08 23:04 UTC (permalink / raw)
  To: Huber, George K RDECOM CERDEC STCD SRI; +Cc: linux-c-programming


Huber, George K RDECOM CERDEC STCD SRI wrote:

> Thanks for all of the suggestions. Actually, Eric hit the nail on
> the head - I was forgetting to link to libpthread. Knew I had to,
> remember thinking that I need to change my makefile and add
> libpthread to the list of libraries that I was linking to. Would
> have bet money that I did this. Oh well....
> 
> Now this bring up an interesting question - how can a program using
> pthread compile cleanly when it is not linked to libpthread? I would
> expect unresolved externals on all pthread functions?

Maybe you were linking against another library which had libpthread as
a dependency; that would cause it to be linked in.

However, while that will prevent unresolved symbols, it won't
generally be sufficient for the resulting program to execute
correctly.

The reason is that libpthread overrides a number of libc functions.

If you explicitly link against libpthread, everything which uses those
functions will use the libpthread versions; however, if you rely upon
libpthread being linked implicitly, some code will use the libpthread
versions while other code will use the libc versions. This tends to
cause crashes.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-04-08 23:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-04-08 14:04 Multithreading with C++ - solved Huber, George K RDECOM CERDEC STCD SRI
2005-04-08 23:04 ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).