Re: Member variables in method are null when called as delegate from thread

2021-01-13 Thread Arafel via Digitalmars-d-learn
On 13/1/21 3:15, Tim wrote: Fantastic response, thank you! I did some more digging and properly narrowed down where the issue is and created a test script that demonstrates the problem. Let me know what you think and if it could still be a similar problem to what you have stated above. I'll s

Re: Member variables in method are null when called as delegate from thread

2021-01-12 Thread tsbockman via Digitalmars-d-learn
On Wednesday, 13 January 2021 at 02:15:49 UTC, Tim wrote: Basically, the program calls a function which modifies a document in the database. If it is called form it's own class' constructor, it works fine. If it is called by a thread, it never returns. ... class Caller : Thread{ void deleg

Re: Member variables in method are null when called as delegate from thread

2021-01-12 Thread Tim via Digitalmars-d-learn
On Tuesday, 12 January 2021 at 01:49:11 UTC, tsbockman wrote: The compiler and the physical CPU are both allowed to change the order in which instructions are executed to something different from what your code specifies, as long as the visible, "official" results and effects of the chosen orde

Re: Member variables in method are null when called as delegate from thread

2021-01-12 Thread tsbockman via Digitalmars-d-learn
On Tuesday, 12 January 2021 at 14:00:11 UTC, Steven Schveighoffer wrote: On 1/11/21 8:49 PM, tsbockman wrote: However, this re-ordering IS permitted to freely alter the behavior of your code from the perspective of OTHER threads. A likely cause of your bug is that the write to db by the constr

Re: Member variables in method are null when called as delegate from thread

2021-01-12 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/11/21 8:49 PM, tsbockman wrote: On Monday, 11 January 2021 at 00:43:00 UTC, Tim wrote: When MessageService calls the delegate for start, db is null. If I call start() in the Foo constructor it works just fine. Am I missing something here? Do delegates get called outside of their class con

Re: Member variables in method are null when called as delegate from thread

2021-01-12 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/11/21 12:26 PM, Arafel wrote: Thanks for the detailed explanation! I think this mixing of types and storage classes makes a very unfortunate combination: ``` import std; int i = 0; shared int j = 0; struct S {     int i = 0;     shared int j = 0; } S s; void main() {     i = 1;    

Re: Member variables in method are null when called as delegate from thread

2021-01-12 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/11/21 6:52 PM, Paul Backus wrote: On Monday, 11 January 2021 at 16:10:49 UTC, Steven Schveighoffer wrote: There are some... odd rules. struct S { [...]    immutable int e = 5; // stored in data segment, not per instance! Are you sure? struct S {     immutable int n = 123;     this(i

Re: Member variables in method are null when called as delegate from thread

2021-01-12 Thread Imperatorn via Digitalmars-d-learn
On Monday, 11 January 2021 at 17:26:00 UTC, Arafel wrote: void f() { assert(i == 0); // Expected assert(j == 1); // Expected assert(s.i == 0); // Expected assert(s.j == 0); // Wait, what? } At first sight this looks unexpected. But I think if you have a shared variable inside

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread tsbockman via Digitalmars-d-learn
On Monday, 11 January 2021 at 00:43:00 UTC, Tim wrote: When MessageService calls the delegate for start, db is null. If I call start() in the Foo constructor it works just fine. Am I missing something here? Do delegates get called outside of their class context? I know I could just pass the db

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread Paul Backus via Digitalmars-d-learn
On Monday, 11 January 2021 at 16:10:49 UTC, Steven Schveighoffer wrote: There are some... odd rules. struct S { [...] immutable int e = 5; // stored in data segment, not per instance! Are you sure? struct S { immutable int n = 123; this(int n) { this.n = n; } } void main() {

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread Arafel via Digitalmars-d-learn
On 11/1/21 17:10, Steven Schveighoffer wrote: A shared member is a sharable member of the class. It does not put the item in global storage. There are some... odd rules. struct S {    static int a; // TLS    shared static int b; // shared data storage    shared int c; // local variable, but

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/11/21 10:42 AM, Arafel wrote: On 11/1/21 14:42, Steven Schveighoffer wrote: That isn't exactly true. Member variables are members of the object. If the object is shared, the member variables are shared. If the object is local the variables are local. Thread local really only applies to

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread Arafel via Digitalmars-d-learn
On 11/1/21 14:42, Steven Schveighoffer wrote: That isn't exactly true. Member variables are members of the object. If the object is shared, the member variables are shared. If the object is local the variables are local. Thread local really only applies to *static* variables, such as globals

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/11/21 3:21 AM, Arafel wrote: On 11/1/21 1:43, Tim wrote: Hi there, I have something like this: class Foo{ MongoClient db; this(){ db = connectMongoDB("127.0.0.1"); void delegate()[string] commands = ["start": &this.start];  MessageService messenger = new M

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread Tim via Digitalmars-d-learn
On Monday, 11 January 2021 at 08:21:21 UTC, Arafel wrote: It's also possible that you'll have to make Foo itself `shared`, or at least convert your constructor into a `shared this ()` to get a shared instance that you can pass to a different thread, but I'm not sure how function pointers / del

Re: Member variables in method are null when called as delegate from thread

2021-01-11 Thread Arafel via Digitalmars-d-learn
On 11/1/21 1:43, Tim wrote: Hi there, I have something like this: class Foo{     MongoClient db;     this(){     db = connectMongoDB("127.0.0.1");     void delegate()[string] commands = ["start": &this.start]; MessageService messenger = new MessageService(8081, commands);    

Re: Member variables in method are null when called as delegate from thread

2021-01-10 Thread Tim via Digitalmars-d-learn
Ok, so it seems that it isn't null now. But I stall can't call db.getCollection().findAndModify() from vibe.d successfully here. Works just fine in the constructor. When it's called form the MessengerService thread it never returns from the function call

Member variables in method are null when called as delegate from thread

2021-01-10 Thread Tim via Digitalmars-d-learn
Hi there, I have something like this: class Foo{ MongoClient db; this(){ db = connectMongoDB("127.0.0.1"); void delegate()[string] commands = ["start": &this.start]; MessageService messenger = new MessageService(8081, commands); } void start(){ /