Skip to content

Commit e485d8c

Browse files
authored
Update FinalExam.md
1 parent 1d208e8 commit e485d8c

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

FinalExam.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,131 @@ Yukarıdaki kod derleme zamanında **error[E0391]: cycle detected when computing
332332

333333
## Soru 8
334334

335+
Rust standart kütüphanesi birçok **smart pointer** türü içerir. Bunlar **Box<T>**, **Rc<T>**, **RefCell<T>**, **Ref<T>** ve **RefMut<T>** şeklinde kategorize edilir. Aşağıda birkaç senaryo belirtilmiştir. Hangi senaryo için hangi smart pointer kullanılmalıdır. Şıklarda doğru sıralamayı seçerek cevaplayınız.
336+
337+
- **I.** Birden fazla sahipliği _(ownership)_ tek bir veri üzerinde sağlamak.
338+
- **II.** Aynı veri üzerinden birden fazla sahiplik sağlamak
339+
- **III.** Aynı veriye farklı iş parçacıkları _(threads)_ erişim söz konusu olduğunda.
340+
341+
**a)** RefMut, RefCell, Ref
342+
343+
**b)** Box/RefCell, Rc, Arc
344+
345+
**c)** Box, Box, Box
346+
347+
**d)** Arc, Mutex, RefCell
348+
349+
## Soru 9
350+
351+
Birden fazla işi farklı iş parçacıklarına _(thread)_ bölerek çalıştırmak mümkündür. Böylece işlemci/çekirdek gücünden efektif şekilde yararlanılabilir. Rust tarafında hem paralel hem de concurrent _(eş zamanlı)_ programlama için birçok hazır enstrüman bulunur. Bir iş parçacığını başlatmak için **thread** modülünün **spawn** metodu kullanılır. Buna göre aşağıdaki kodun çalışma zamanı çıktısı şıklardan hangisidir?
352+
353+
```rust
354+
use std::thread;
355+
356+
fn main() {
357+
do_something();
358+
println!("After the thread calling");
359+
}
360+
361+
pub fn do_something() {
362+
let student_points = vec![30.50, 49.90, 65.55, 90.00, 81.00];
363+
let handle = thread::spawn(|| {
364+
println!("Thread is starting...");
365+
println!("{:?}", student_points);
366+
println!("Thread completed");
367+
});
368+
handle.join().unwrap();
369+
}
370+
```
371+
372+
**a)** Bu kod derlenmez ve error[E0373]: closure may outlive the current function, but it borrows `student_points`, which is owned by the current function mesajını içeren bir hata fırlatır.
373+
374+
**b)**
375+
```text
376+
Thread is starting...
377+
[30.5, 49.9, 65.55, 90.0, 81.0]
378+
Thread completed
379+
After the thread calling
380+
```
381+
382+
**c)**
383+
```text
384+
After the thread calling
385+
Thread is starting...
386+
[30.5, 49.9, 65.55, 90.0, 81.0]
387+
Thread completed
388+
```
389+
390+
**d)**
391+
```text
392+
Thread is starting...
393+
Thread completed
394+
After the thread calling
395+
```
396+
397+
## Soru 10
398+
399+
Aşağıda örnek bir kod parçası verilmiştir.
400+
401+
```rust
402+
use std::thread;
403+
404+
fn main() {
405+
run_with_error();
406+
println!("After the thread calling");
407+
}
408+
409+
pub fn run_with_error() {
410+
let data = vec![
411+
"Service Red: Task A",
412+
"Service Blue: Task B",
413+
"Service Green: Task C",
414+
"Service Alpha: Task D",
415+
];
416+
417+
let mut handles = vec![];
418+
for i in 0..2 {
419+
let handle = thread::spawn(move || {
420+
for task in &data {
421+
println!("Thread '{}' is processing '{}'", i, task);
422+
}
423+
});
424+
425+
handles.push(handle);
426+
}
427+
428+
for handle in handles {
429+
handle.join().unwrap();
430+
}
431+
}
432+
```
433+
434+
Kod derleme zamanında aşağıdaki hatayı vermektedir.
435+
436+
```text
437+
error[E0382]: use of moved value: `data`
438+
--> practices\src\main.rs:18:36
439+
|
440+
9 | let data = vec![
441+
| ---- move occurs because `data` has type `Vec<&str>`, which does not implement the `Copy` trait
442+
...
443+
17 | for i in 0..2 {
444+
| ------------- inside of this loop
445+
18 | let handle = thread::spawn(move || {
446+
| ^^^^^^^ value moved into closure here, in previous iteration of loop
447+
19 | for task in &data {
448+
| ---- use occurs due to use in closure
449+
```
450+
451+
Aynı anda birden fazla iş parçacığı _(thread)_ ortak bir referansa erişmeye çalışmakta ve referans sahipliği **closure** içerisine alındıktan sonra düşeceği _(drop)_ için bu sorun oluşmaktadır. Problemi çözmek için şıklardan hangisini uygularsınız?
452+
453+
**a)** Data vector yerine referans türlü bir diziye alınırsa sorun çözülür.
454+
455+
**b)** Data Arc (Atomic Reference Counting) smart pointer nesnesi içinde ele alınmalı ve döngüde açılan her bir thread örneği klonlanarak paylaşılmalıdır
456+
457+
**c)** data vektörü her thread::spawn çağrısında yeniden tanımlanmalıdır. Böylece her thread kendi data kopyasına sahip olur ve ownership sorunu yaşanmaz.
458+
459+
**d)** Closure bloğu içine data değişkeni move olmadan geçirilirse, tüm thread’ler ortak referansı kullanabilir.
460+
461+
## Soru 11
335462

0 commit comments

Comments
 (0)