www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2162] New: Access violation when threads run closures

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2162

           Summary: Access violation when threads run closures
           Product: D
           Version: 2.016
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: bartosz relisoft.com


Passing closures to threads might be considered an error, but the current
thread implementation virtualy encourages it. The program below behaves
erratically--the starting value of x and y inside thread functions is random
and, at leas in my environment, the program access violates. (I'm not sure how
reproducible it is).

void test(ref Thread thr1, ref Thread thr2)
{
        int x, y;
        int run1() 
        {
                writeln("Run1 ", x);
                for(int i = 0; i < 100000; ++i)
                        ++x;
                writeln("End Run1 ", x);
                return 0; 
        }
        int run2() 
        {
                writeln("Run2 ", y);
                for(int i = 0; i < 100000; ++i)
                        ++y;
                writeln("End Run2 ", y);
                return 0; 
        }
        thr1 = new Thread(&run1);
        thr2 = new Thread(&run2);
}

void main()
{
        try
        {
                Thread thr1, thr2;
                test(thr1, thr2);
                thr1.start();
                thr2.start();
                thr1.wait();
                thr2.wait();
        }
        catch(Exception e)
        {
                writeln ("Exception in main: ", e.toString);
        }
}


-- 
Jun 22 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2162


brunodomedeiros+bugz gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |brunodomedeiros+bugz gmail.c
                   |                            |om




------- Comment #1 from brunodomedeiros+bugz gmail.com  2008-10-03 08:43 -------
Bump! I've recently run into this as well.

The problem happens when the context of the passed delegate is in the stack.
If the context of the passed delegate is in the heap, all is fine. If you add
this code to th end of test():

...
    thr1 = new Thread(&run1);
    thr2 = new Thread(&run2);

    auto dummy = (&run1);
}

---
The program will run correctly, as the context will be on the heap.
So I think the problem might just be incorrect closure detection?


-- 
Oct 03 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2162


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2011-05-24
22:45:34 PDT ---
In 2.053 it will deny compiling this as it detects that run1 and run2 are
delegates.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 24 2011