Comments (1)
This code show that in multithread enviroment RefCounted can call destructor and dealloc multiple times for same object.
import core.atomic : atomicFetchAdd, atomicFetchSub, atomicLoad, atomicStore;
import std.experimental.allocator.mallocator : Mallocator;
import std.concurrency : spawn;
import std.meta : AliasSeq;
import std.stdio : writeln;
import std.conv : to;
import automem.ref_counted;
__gshared int counter = 0;
__gshared ulong iteration = 0;
alias Rc(T) = RefCounted!(shared(T));
struct Foo{
int i;
this(int i)shared {
this.i = i;
atomicFetchAdd(counter, 1);
}
~this(){
atomicFetchSub(counter, 1);
}
}
void foo(shared(Rc!Foo)* ptr, const ulong iteration, shared(uint)* ready){
{
Rc!Foo x = *cast(Rc!Foo*)ptr; /// copy ref counted object
atomicFetchAdd(*ready, 1); ///inform main thread that this thread is ready to new iteration
while(atomicLoad(.iteration) != iteration){} ///wait until main thread start iteration
///destruction of ref counted object
}
atomicFetchSub(*ready, 1); ///inform main thread that this thread ended iteration
}
void main(){
foreach(ulong i; 1 .. 30){
shared x = Rc!Foo(-1);
shared uint ready = 0;
spawn(&foo, &x, i, &ready);
spawn(&foo, &x, i, &ready);
while(atomicLoad(ready) != 2){} ///wait until both threads make copy of x.
(*cast(Rc!Foo*)&x) = Rc!Foo.init; ///release x
atomicStore(iteration, i); ///start new iteration
while(atomicLoad(ready) != 0){} ///wait until both threads end iteration.
}
assert(counter >= 0, "double destruction: " ~ (-counter).to!string);
assert(counter == 0, "missing destruction" ~ counter.to!string);
}
from automem.
Related Issues (20)
- could you please tag a new relase 0.0.10 or 0.1.0 HOT 2
- After dmd 2.076.0, RefCounted can't be used with shared types
- Please tag a new release for DMD's CI HOT 1
- Use after free, caused by iterator(slice) invalidation not prevented by dip1000/dip25 HOT 1
- Wrongly trusted return statement in automem.vector.range() allows obtaining a reference to expired stack frame HOT 7
- Vector should implement opEquals v.s. arrays and compatible Vectors
- Vector.free violates scope HOT 2
- opSlice can be @trusted on an immutable Vector? HOT 2
- Can't compile with gdc 9.2.0 HOT 3
- Cannot foreach over Unique or RefCounted ranges HOT 1
- Crash with RefCounted HOT 3
- Linking error HOT 1
- Invariant violation in debug mode HOT 2
- Cannot slice whole vector explicitly HOT 1
- Please tag a new release
- dispose() will not free the proper size of an object derived from a template HOT 1
- RefCounted fails to destroy payload in combination with zip HOT 4
- Vector.opIndex violates return HOT 5
- New tag release HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from automem.