public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/13684] New: local static object variable constructed once but ctors and dtors called multiple times on same memory when called in multiple threads
@ 2004-01-14 17:25 evijaykumar at yahoo dot com
  2004-01-14 17:27 ` [Bug c++/13684] " evijaykumar at yahoo dot com
                   ` (22 more replies)
  0 siblings, 23 replies; 25+ messages in thread
From: evijaykumar at yahoo dot com @ 2004-01-14 17:25 UTC (permalink / raw)
  To: gcc-bugs

I have a simple sample code which illustrates the issue. Basically, if multiple 
threads invoke a function which has local static object, the ctor and dtor are 
called multiple times on the same object in non reentrant manner until object 
is fully constructed. Here is a sample program which illustrates the issue




#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

volatile int i;
class A {
    public:
    int i;
    A(int i)
    {
        this->i = i;
        sleep(1);
        printf("I am in A ctor %d this:%p\n", i, this);
    }
    ~A() {
        printf("A dtor %d this:%p\n", i, this);
    }
};
extern "C" void *foo(void *)
{
    printf("before static ctor in foo\n");
    static A a(i++);
    printf("in foo %p a(i) %d\n", &a, a.i);
    return NULL;
}
main()
{
    printf("I am in main\n");
    pthread_t tid;
    pthread_create(&tid,NULL, foo, NULL);
    pthread_create(&tid,NULL, foo, NULL);
    pthread_create(&tid,NULL, foo, NULL);
    pthread_create(&tid,NULL, foo, NULL);
    pthread_create(&tid,NULL, foo, NULL);
    pthread_create(&tid,NULL, foo, NULL);
    pthread_create(&tid,NULL, foo, NULL);
    pthread_create(&tid,NULL, foo, NULL);
    pthread_create(&tid,NULL, foo, NULL);
    pthread_create(&tid,NULL, foo, NULL);
    pthread_create(&tid,NULL, foo, NULL);
    sleep(3);
    foo(NULL);
}


The output of this program is 

I am in main
before static ctor in foo
before static ctor in foo
before static ctor in foo
before static ctor in foo
before static ctor in foo
before static ctor in foo
before static ctor in foo
before static ctor in foo
before static ctor in foo
before static ctor in foo
before static ctor in foo
I am in A ctor 1 this:210b0
I am in A ctor 3 this:210b0
in foo 210b0 a(i) 10
in foo 210b0 a(i) 10
I am in A ctor 4 this:210b0
in foo 210b0 a(i) 10
I am in A ctor 6 this:210b0
in foo 210b0 a(i) 10
I am in A ctor 8 this:210b0
in foo 210b0 a(i) 10
I am in A ctor 9 this:210b0
in foo 210b0 a(i) 10
I am in A ctor 2 this:210b0
in foo 210b0 a(i) 10
I am in A ctor 5 this:210b0
in foo 210b0 a(i) 10
I am in A ctor 0 this:210b0
in foo 210b0 a(i) 10
I am in A ctor 10 this:210b0
in foo 210b0 a(i) 10
I am in A ctor 7 this:210b0
in foo 210b0 a(i) 10
before static ctor in foo
in foo 210b0 a(i) 10
A dtor 10 this:210b0
A dtor 10 this:210b0
A dtor 10 this:210b0
A dtor 10 this:210b0
A dtor 10 this:210b0
A dtor 10 this:210b0
A dtor 10 this:210b0
A dtor 10 this:210b0
A dtor 10 this:210b0
A dtor 10 this:210b0
A dtor 10 this:210b0

-- 
           Summary: local static object variable constructed once but ctors
                    and dtors called multiple times on same memory when
                    called in multiple threads
           Product: gcc
           Version: 3.3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: evijaykumar at yahoo dot com
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13684


^ permalink raw reply	[flat|nested] 25+ messages in thread
[parent not found: <bug-13684-4@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2014-02-16 13:18 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-14 17:25 [Bug c++/13684] New: local static object variable constructed once but ctors and dtors called multiple times on same memory when called in multiple threads evijaykumar at yahoo dot com
2004-01-14 17:27 ` [Bug c++/13684] " evijaykumar at yahoo dot com
2004-01-14 17:30 ` pinskia at gcc dot gnu dot org
2004-01-14 17:32 ` pinskia at gcc dot gnu dot org
2004-01-14 20:55 ` evijaykumar at yahoo dot com
2004-07-15  1:07 ` pinskia at gcc dot gnu dot org
2004-07-15  2:16 ` gianni at mariani dot ws
2004-07-15  2:42 ` bangerth at dealii dot org
2004-07-15  5:11 ` gianni at mariani dot ws
2004-07-15 11:07 ` rmerkert at alphatech dot com
2004-07-15 13:54 ` gianni at mariani dot ws
2004-08-21  0:01 ` jason at gcc dot gnu dot org
2004-08-28  2:34 ` cvs-commit at gcc dot gnu dot org
2004-08-28 10:18 ` pinskia at gcc dot gnu dot org
2004-08-28 19:04 ` gianni at mariani dot ws
2004-08-31  6:41 ` adah at netstd dot com
2004-08-31 20:58 ` pinskia at gcc dot gnu dot org
2004-09-01 16:58 ` cvs-commit at gcc dot gnu dot org
2004-09-04 18:06 ` jason at redhat dot com
2004-09-05  3:19 ` adah at netstd dot com
2005-04-13 16:56 ` dhruvbird at yahoo dot com
2005-04-14  7:39 ` jason at redhat dot com
2005-04-18  9:07 ` adah at netstd dot com
2005-04-18 18:28 ` jason at redhat dot com
     [not found] <bug-13684-4@http.gcc.gnu.org/bugzilla/>
2014-02-16 13:18 ` jackie.rosen at hushmail dot com

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 read-only IMAP folder(s) and NNTP newsgroup(s).