On Tuesday, 23 April 2013 at 21:21:28 UTC, Jack Applegame wrote:
I'm writing Ctrl-C handler for console application for Windows:

extern(Windows) {
  int CtrlHandler(uint flag) nothrow {
    auto tmp = new SomeClass; // is it safe?
    ...
    return true;
  }
}

...

SetConsoleCtrlHandler(&CtrlHandler, true);

...

According WinAPI documentation, CtrlHandler will be called in new additional thread. Is it safe to allocate GC memory in NOT Phobos threads? If not, how to make it safe? I'm trying call thread_attachThis() at the beginning of CtrlHandler fucntion, but it doesn't compile because thread_attachThis() is not nothrow.

i guess it call ur callback(enters D code, so GC is in), alloc new instance, and after callback return it should collect it, but there is no such thing like guessing, just try it and see if it works correctly.

also there is an option without using GC at all, use struct instead of class if you don't need polymorphism(for most tasks structs is really enough, they are as powerful as c++ class just without polymorphism and other nasties)

also if you really need class or whole program lifetime memory, you may declare instance in module level with __gshared("__gshared SomeClass tmp;", don't forget to alloc if is class) so gc won't touch it.

also it may be better to read related docs in language reference on this site for more info, since i'm somehow bad with remembering all the details.

Reply via email to