You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: FinalExam.md
+127Lines changed: 127 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -332,4 +332,131 @@ Yukarıdaki kod derleme zamanında **error[E0391]: cycle detected when computing
332
332
333
333
## Soru 8
334
334
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?
**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
+
usestd::thread;
403
+
404
+
fnmain() {
405
+
run_with_error();
406
+
println!("After the thread calling");
407
+
}
408
+
409
+
pubfnrun_with_error() {
410
+
letdata=vec![
411
+
"Service Red: Task A",
412
+
"Service Blue: Task B",
413
+
"Service Green: Task C",
414
+
"Service Alpha: Task D",
415
+
];
416
+
417
+
letmuthandles=vec![];
418
+
foriin0..2 {
419
+
lethandle=thread::spawn(move|| {
420
+
fortaskin&data {
421
+
println!("Thread '{}' is processing '{}'", i, task);
422
+
}
423
+
});
424
+
425
+
handles.push(handle);
426
+
}
427
+
428
+
forhandleinhandles {
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.
0 commit comments