1
+ use std:: collections:: HashMap ;
2
+ use std:: pin:: Pin ;
3
+ use std:: ptr:: null;
4
+ use std:: sync:: Arc ;
5
+ use std:: sync:: mpsc:: { channel, Receiver , sync_channel} ;
6
+ use std:: task:: { Context , Poll } ;
7
+ use std:: thread:: sleep;
8
+ use std:: time:: Duration ;
9
+ use crossbeam_channel:: unbounded;
10
+ use futures:: Stream ;
11
+
12
+ // // Exercise 1: STD channels
13
+ // fn main() {
14
+ //
15
+ // // syscall init
16
+ // // OS -> [..............................................................................] -> computation -> serve
17
+ // // OS -> [.] -> computation -> serve
18
+ // // backpressure konusu
19
+ //
20
+ // // Create a simple streaming channel
21
+ // let (tx, rx) = sync_channel::<Vec<u8>>(1);
22
+ // let tx2 = tx.clone();
23
+ // std::thread::spawn(move|| {
24
+ // tx.send(vec![0_u8; 20]).unwrap();
25
+ // });
26
+ //
27
+ // println!("before 20");
28
+ //
29
+ // tx2.try_send(vec![1_u8; 20]).unwrap();
30
+ // tx2.send(vec![3_u8; 20]).unwrap();
31
+ //
32
+ // println!("after 20");
33
+ //
34
+ // // Main thread
35
+ // assert_eq!(rx.recv().unwrap(), vec![0_u8; 20]);
36
+ // // assert_eq!(rx.recv().unwrap(), 10);
37
+ // // assert_eq!(rx.recv().unwrap(), 20);
38
+ // sleep(Duration::from_secs(10));
39
+ // }
40
+
41
+
42
+ // // Exercise 2: mpmc channels
43
+ // fn main() {
44
+ // let (tx, rx) = unbounded::<usize>();
45
+ //
46
+ // tx.clone();
47
+ // rx.clone();
48
+ // tx.clone();
49
+ // rx.clone();
50
+ // }
51
+
52
+ // // Exercise 3: Future generation mental model
53
+ // #[derive(Clone)]
54
+ // pub struct RTStream {
55
+ // rx: Receiver<usize>,
56
+ // }
57
+
58
+ // enum FutureResult {
59
+ // Ready = 0,
60
+ // Pending = 1
61
+ // }
62
+
63
+
64
+ // // Exercise 4: Immutable data sharing between futures.
65
+ // #[derive(Debug)]
66
+ // pub struct Person {
67
+ // age: usize,
68
+ // name: String,
69
+ // surname: String
70
+ // }
71
+ //
72
+ //
73
+ // impl Person {
74
+ // pub fn new(age: usize, name: String, surname: String) -> Self {
75
+ // Self {
76
+ // age,
77
+ // name,
78
+ // surname
79
+ // }
80
+ // }
81
+ // }
82
+ //
83
+ // fn main() {
84
+ // let theo = Person::new(15, "theo".into(), "bulut".into());
85
+ // let theo = Arc::new(theo);
86
+ // let theo1 = theo.clone();
87
+ //
88
+ //
89
+ // let tm = async {
90
+ // // blocking - 10s
91
+ // let fs = std::fs::File::open("test").unwrap();
92
+ // theo1
93
+ // };
94
+ //
95
+ // let tms = async {
96
+ // theo.age
97
+ // };
98
+ //
99
+ // let runner = async {
100
+ // tm.await.age + tms.await
101
+ // };
102
+ //
103
+ // let x = futures::executor::block_on(runner);
104
+ //
105
+ // dbg!(&x);
106
+ // }
107
+
108
+ // // Exercise 5: Stream implementation
109
+
110
+ // pub struct RTStream {}
111
+ //
112
+ // impl Stream for RTStream {
113
+ // type Item = usize;
114
+ //
115
+ // fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
116
+ // todo!()
117
+ // }
118
+ //
119
+ // fn size_hint(&self) -> (usize, Option<usize>) {
120
+ // todo!()
121
+ // }
122
+ // }
123
+
124
+ // // Exercise 6: Can't share raw pointer across thread boundaries.
125
+ // #[derive(Clone)]
126
+ // pub struct RTRawPointerExample {
127
+ // age: usize,
128
+ // __priv: *const ()
129
+ // }
130
+
131
+ // fn main() {
132
+ // let (tx, rx) = unbounded::<usize>();
133
+ //
134
+ // let rpe = RTRawPointerExample {
135
+ // age: 32,
136
+ // __priv: null()
137
+ // };
138
+ //
139
+ // // Sakin bundan baska birsey kullanma: Arc, Rc, Mutex, RwLock
140
+ // let rpe = Arc::new(rpe);
141
+ //
142
+ // std::thread::spawn(move|| {
143
+ // rpe
144
+ // });
145
+ // }
146
+
147
+
148
+ // mutable data sharing between futures, pin projection, spawn, spawn_blocking, future_combinatorlar, streamler, stream_combinators 2. derse
149
+
150
+ // 3.ders TM, STM, HTM. conc types
0 commit comments