aboutsummaryrefslogtreecommitdiff
path: root/docs/notes/threads.txt
blob: fb9e959c4273e414b8c4ce823bfee3bd9c7bf6e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66


thread model
------------

simple message passing:

    static const string[]

    typedef Message = struct(
        int a;
        float b;
        const string static* description;
        readonly static* status;
        string static* log;
        
    );

    thread () work(ThreadQueue[Message own*]* q, Message own* msg) {
        
        // work with msg
        
        // we can access "own" pointers as normal
        msg.a = 1;
        
        // we can also access pointers to static constant data
        // (const data is assumed to not change)
        print(description);
        
        // if we want to access readonly pointers, we need exclusive access
        // TODO
        //print(status);
        
        // Or we can use the atomic keyword
        print(atomic(status));
        
        // with normal pointers we also need exclusive access
        // TODO
        //print(log);
        
        q.push(msg);
    }

    int main(count argc, string[argc] argv) {
        
        ThreadQueue[Message own *] q(); // equivalent to ThreadQueue q; q.init();
        
        for (int i : [0..9]) {
            
            Message *own msg = Message.new(1, 2.34);
            
            // all pointers in (q, msg) must either be "own" or "static"
            thread(work, (q, msg));
            
            // msg may no longer be used!   
        }
        
        while (condition) {
            
            for (Message own *msg 
            
        }
        
    }