fellow D programmers

I would like to ask a question! I would like to create a class, which works in a separated thread, while the class exisit. This is important. I want to kill/terminate/stop the third whenn the class get cleared. Thas what i got so far. This is what i got so far (very easy solution):

class MyClass {
        public:
                this() {
                        Thread thread=new Thread(&threadFunc);
                        running=true;
                        thread.start();
                }
                
                ~this() {
                        writeln(someVariable);
                        running=false;
                }
                
        private:
                bool running=false;
                int someVariable=1;
                
                void threadFunc() {
                        while(running) {
                                someVariable++;
                        }
                }
}

Becouse core.thread.Thread doesnt have any stop,pause,therminate method i have to stop the thread by myself (with the help of the running variable). The problem is: this ISNT WORKING. The reason is: the destructor never gets called, so the thread runs forever (application didnt exit). So the GC finds somewhere a pointer to the class's instance memory (which is ok i think, becouse i use delegate to create the thread). But imagagine this: when i change the running variable to static, everything works as expected, and the app frees and closes normally. Why is the running variable is different from the other someVariable??
Before u ask:
- "shared bool running" doesnt works, only static.
- Tried to keep a reference of the Thread as a local variable, result is the same - i tried 1000 times the code on 2 different PC, result is the same (no random pointer somewhere)
- i use latest LDC

Obviously, i want to avoid a static running variable, my class can have multiple instances.

Ty Collerblade

Reply via email to