linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: Multithreading with C++
  2005-04-07 17:02 Multithreading with C++ Huber, George K RDECOM CERDEC STCD SRI
@ 2005-04-07 16:36 ` Eric Bambach
  2005-04-08  2:01 ` Ron Michael Khu
  1 sibling, 0 replies; 7+ messages in thread
From: Eric Bambach @ 2005-04-07 16:36 UTC (permalink / raw)
  To: Huber, George K RDECOM CERDEC STCD SRI; +Cc: linux prg

On Thursday 07 April 2005 12:02 pm, you wrote:
> All,
>
> This may be a little off topic, but I though I would try here first.

Did you compile with -lpthread and -D_REENTRANT? If I remeber correctly, this 
is something I had alot of trouble with when I started thread programming. 
Similiar error and everything. Compiled but the library calls weren't 
working. Make sure to include <pthread.h> too  ;)

> 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:

--snip--

> Thanks,
> George Huber
>

-- 
----------------------------------------
--EB

> All is fine except that I can reliably "oops" it simply by trying to read
> from /proc/apm (e.g. cat /proc/apm).
> oops output and ksymoops-2.3.4 output is attached.
> Is there anything else I can contribute?

The latitude and longtitude of the bios writers current position, and
a ballistic missile.

                --Alan Cox LKML-December 08,2000 

----------------------------------------
-
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] 7+ messages in thread

* Multithreading with C++
@ 2005-04-07 17:02 Huber, George K RDECOM CERDEC STCD SRI
  2005-04-07 16:36 ` Eric Bambach
  2005-04-08  2:01 ` Ron Michael Khu
  0 siblings, 2 replies; 7+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2005-04-07 17:02 UTC (permalink / raw)
  To: linux-c-programming

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


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

* RE: Multithreading with C++
@ 2005-04-07 18:52 Huber, George K RDECOM CERDEC STCD SRI
  2005-04-07 19:01 ` Darío Mariani
  0 siblings, 1 reply; 7+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2005-04-07 18:52 UTC (permalink / raw)
  To: Huber, George K RDECOM CERDEC STCD SRI; +Cc: linux prg

Eric wrote:

Did you compile with -lpthread and -D_REENTRANT? If I remeber correctly, this 
is something I had alot of trouble with when I started thread programming. 
Similiar error and everything. Compiled but the library calls weren't 
working. Make sure to include <pthread.h> too  ;)

yes included <pthread.h>, defined -D_REENTRANT and linked with -lpthread

I have written multi-threaded code in C, this is the first time that I have attempted 
to encapsulate a thread in a C++ object.  Must be missing something. :-(

George

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

* Re: Multithreading with C++
  2005-04-07 18:52 Huber, George K RDECOM CERDEC STCD SRI
@ 2005-04-07 19:01 ` Darío Mariani
       [not found]   ` <fde202080504071322520de47@mail.gmail.com>
  0 siblings, 1 reply; 7+ messages in thread
From: Darío Mariani @ 2005-04-07 19:01 UTC (permalink / raw)
  Cc: linux prg

There are several libraries that implement multithreading in C++:

* http://www.boost.org
* http://www.melikyan.com/ptypes/
* http://www.cs.wustl.edu/~schmidt/ACE.html

You can use one of them and avoid the trouble of creating your own
library, or look there to see how they did it ;-).

           Darío

On Apr 7, 2005 3:52 PM, Huber, George K RDECOM CERDEC STCD SRI
<George.K.Huber@us.army.mil> wrote:
> Eric wrote:
> 
> Did you compile with -lpthread and -D_REENTRANT? If I remeber correctly, this
> is something I had alot of trouble with when I started thread programming.
> Similiar error and everything. Compiled but the library calls weren't
> working. Make sure to include <pthread.h> too  ;)
> 
> yes included <pthread.h>, defined -D_REENTRANT and linked with -lpthread
> 
> I have written multi-threaded code in C, this is the first time that I have attempted
> to encapsulate a thread in a C++ object.  Must be missing something. :-(
> 
> George
> -
> 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
> 

-- 
That is not dead which can eternal lie,
and with strange aeons, even death may die.
-
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] 7+ messages in thread

* Re: Multithreading with C++
       [not found]   ` <fde202080504071322520de47@mail.gmail.com>
@ 2005-04-07 20:26     ` Sandro Dangui
  0 siblings, 0 replies; 7+ messages in thread
From: Sandro Dangui @ 2005-04-07 20:26 UTC (permalink / raw)
  To: linux-c-programming

How many threads are you trying to create? I mean, using your class CThread...
I see only a problem when trying to create more than one thread, due
to the "static CThread*  m_pThis". This attribute should not be shared
between all threads you create. I think you want to have a
relationship 1 to 1, for each instance of CThead class you want to
have one actual running thread handling with.
  
Sandro.
 
 ----------------------------------------------------------------------------------------
  
> On Apr 7, 2005 4:01 PM, Darío Mariani <mariani.dario@gmail.com> wrote: 
> > There are several libraries that implement multithreading in C++:
> > 
> > * http://www.boost.org
> > * http://www.melikyan.com/ptypes/
> > * http://www.cs.wustl.edu/~schmidt/ACE.html
> > 
> > You can use one of them and avoid the trouble of creating your own
> > library, or look there to see how they did it ;-).
> > 
> >           Darío
> > 
> > On Apr 7, 2005 3:52 PM, Huber, George K RDECOM CERDEC STCD SRI
> > <George.K.Huber@us.army.mil> wrote:
> > > Eric wrote:
> > >
> > > Did you compile with -lpthread and -D_REENTRANT? If I remeber correctly, this
> > > is something I had alot of trouble with when I started thread programming.
> > > Similiar error and everything. Compiled but the library calls weren't
> > > working. Make sure to include <pthread.h> too  ;)
> > >
> > > yes included <pthread.h>, defined -D_REENTRANT and linked with -lpthread
> > >
> > > I have written multi-threaded code in C, this is the first time that I have attempted
> > > to encapsulate a thread in a C++ object.  Must be missing something. :-(
> > >
> > > George
> > > -
> > > 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
> > >
> > 
> > --
> > That is not dead which can eternal lie,
> > and with strange aeons, even death may die.
> > -
> > 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
> > 
> 
>
-
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] 7+ messages in thread

* Re: Multithreading with C++
  2005-04-07 17:02 Multithreading with C++ Huber, George K RDECOM CERDEC STCD SRI
  2005-04-07 16:36 ` Eric Bambach
@ 2005-04-08  2:01 ` Ron Michael Khu
  1 sibling, 0 replies; 7+ messages in thread
From: Ron Michael Khu @ 2005-04-08  2:01 UTC (permalink / raw)
  Cc: linux-c-programming

i've encountered a similar problem a few months ago...
have u tried declaring AND defining the threadfun() function OUTSIDE of 
the class??


Huber, George K RDECOM CERDEC STCD SRI wrote:

>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] 7+ messages in thread

* RE: Multithreading with C++
@ 2005-04-08  6:47 Colovic (ext_evosoft) Aleksandar
  0 siblings, 0 replies; 7+ messages in thread
From: Colovic (ext_evosoft) Aleksandar @ 2005-04-08  6:47 UTC (permalink / raw)
  To: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 2661 bytes --]

Hi, 

try declaring threadfun as class static function : void* CThread::threadfun(void* arg)
Attached is the working code. I built it with make cthread.cpp LDFLAGS=-lpthread


Cheers



-----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: Donnerstag, 7. April 2005 19:02
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

[-- Attachment #2: cthread.h --]
[-- Type: application/octet-stream, Size: 245 bytes --]

#include "pthread.h"
#include "stdio.h"

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


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

};


[-- Attachment #3: cthread.cpp --]
[-- Type: application/octet-stream, Size: 435 bytes --]

#include "cthread.h"


CThread* CThread::m_pThis = NULL;

CThread::~CThread() {}
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* CThread::threadfun(void* arg)
{
     printf("Starting thread\n");
}

int main(int argc, char** argv)
{
	CThread ct(0);
	
	return 0;
}

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

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

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-04-07 17:02 Multithreading with C++ Huber, George K RDECOM CERDEC STCD SRI
2005-04-07 16:36 ` Eric Bambach
2005-04-08  2:01 ` Ron Michael Khu
  -- strict thread matches above, loose matches on Subject: below --
2005-04-07 18:52 Huber, George K RDECOM CERDEC STCD SRI
2005-04-07 19:01 ` Darío Mariani
     [not found]   ` <fde202080504071322520de47@mail.gmail.com>
2005-04-07 20:26     ` Sandro Dangui
2005-04-08  6:47 Colovic (ext_evosoft) Aleksandar

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).