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
// operations we don't want polluting the parent's memory
46
-
fork_map(|| {
47
-
// Do your ugly operations here
48
-
Ok(item*1234)
49
-
}).expect("fork_map succeeded")
48
+
unsafe {
49
+
fork_map(|| {
50
+
// Do your ugly operations here
51
+
Ok(item*1234)
52
+
}).expect("fork_map succeeded")
53
+
}
50
54
}).collect::<Vec<_>>();
51
55
52
56
// Use results here
@@ -69,17 +73,23 @@ pub fn main() {
69
73
.chunks(512)
70
74
.map(|items| {
71
75
// Now each child process does 512 items at once
72
-
fork_map(|| {
73
-
letmutresults=vec![];
74
-
// Maybe this operation is only mildly heinous
75
-
// and we can do 512 of them before the child
76
-
// process needs to be restarted.
77
-
foriteminitems {
78
-
results.push(item*1234);
79
-
}
80
-
Ok(results)
81
-
}).expect("fork_map succeeded")
76
+
unsafe {
77
+
fork_map(|| {
78
+
letmutresults=vec![];
79
+
// Maybe this operation is only mildly heinous
80
+
// and we can do 512 of them before the child
81
+
// process needs to be restarted.
82
+
foriteminitems {
83
+
results.push(item*1234);
84
+
}
85
+
Ok(results)
86
+
}).expect("fork_map succeeded")
87
+
}
82
88
})
83
89
.collect::<Vec<_>>();
84
90
}
85
91
```
92
+
93
+
## Safety
94
+
95
+
Due to the nature of `fork()`, this function is very unsound and likely violates most of Rust's guarantees about lifetimes, considering all of your memory gets duplicated into a second process, even though it calls `exit(0)` after your closure is executed. Any threads other than the one calling `fork_map` will not be present in the new process, so threaded lifetime guarantees are also violated. Don't even think about using async executors with this.
0 commit comments