()
| 1023 | |
| 1024 | #[test] |
| 1025 | fn access_using_rwlock() { |
| 1026 | // ANCHOR: access_using_rwlock |
| 1027 | let ops: Vec<_> = vec![Op::Add, Op::Sub, Op::Div, Op::Mul, Op::Add, Op::Div]; |
| 1028 | |
| 1029 | // Set active GPU/device on main thread on which |
| 1030 | // subsequent Array objects are created |
| 1031 | set_device(0); |
| 1032 | |
| 1033 | let c = constant(0.0f32, dim4!(3, 3)); |
| 1034 | let a = constant(1.0f32, dim4!(3, 3)); |
| 1035 | let b = constant(2.0f32, dim4!(3, 3)); |
| 1036 | |
| 1037 | // Move ownership to RwLock and wrap in Arc since same object is to be modified |
| 1038 | let c_lock = Arc::new(RwLock::new(c)); |
| 1039 | |
| 1040 | // a and b are internally reference counted by ArrayFire. Unless there |
| 1041 | // is prior known need that they may be modified, you can simply clone |
| 1042 | // the objects pass them to threads |
| 1043 | |
| 1044 | let threads: Vec<_> = ops |
| 1045 | .into_iter() |
| 1046 | .map(|op| { |
| 1047 | let x = a.clone(); |
| 1048 | let y = b.clone(); |
| 1049 | |
| 1050 | let wlock = c_lock.clone(); |
| 1051 | thread::spawn(move || { |
| 1052 | //Both of objects are created on device 0 in main thread |
| 1053 | //Every thread needs to set the device that it is going to |
| 1054 | //work on. Note that all Array objects must have been created |
| 1055 | //on same device as of date this is written on. |
| 1056 | set_device(0); |
| 1057 | if let Ok(mut c_guard) = wlock.write() { |
| 1058 | match op { |
| 1059 | Op::Add => { |
| 1060 | *c_guard += x + y; |
| 1061 | } |
| 1062 | Op::Sub => { |
| 1063 | *c_guard += x - y; |
| 1064 | } |
| 1065 | Op::Div => { |
| 1066 | *c_guard += x / y; |
| 1067 | } |
| 1068 | Op::Mul => { |
| 1069 | *c_guard += x * y; |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | }) |
| 1074 | }) |
| 1075 | .collect(); |
| 1076 | |
| 1077 | for child in threads { |
| 1078 | let _ = child.join(); |
| 1079 | } |
| 1080 | |
| 1081 | //let read_guard = c_lock.read().unwrap(); |
| 1082 | //af_print!("C after threads joined", *read_guard); |
nothing calls this directly
no test coverage detected