Multithreading 如何创建线程管理器?

Multithreading 如何创建线程管理器?,multithreading,rust,Multithreading,Rust,我有一个数据流,我想在后台处理,但我想创建一个结构或一些函数来管理这个流 < C++ >中,我将创建一个抽象所有这些类的类。它将有一个start方法,该方法将初始化数据流并启动线程进行处理。它将有一个stop方法来停止处理并连接线程 然而,这并不是真的生锈,甚至在生锈的情况下也不起作用 例() < >即使我欺骗和删除调用 WorksFn < /C>,我仍然不能真正地使用 JoinHandle < /C> >,就像我可以从C++ Lead中期待的那样。 error[E0507]: cannot m

我有一个数据流,我想在后台处理,但我想创建一个结构或一些函数来管理这个流

< C++ >中,我将创建一个抽象所有这些类的类。它将有一个
start
方法,该方法将初始化数据流并启动线程进行处理。它将有一个
stop
方法来停止处理并连接线程

然而,这并不是真的生锈,甚至在生锈的情况下也不起作用

例()

< >即使我欺骗和删除调用<代码> WorksFn < /C>,我仍然不能真正地使用<代码> JoinHandle < /C> >,就像我可以从C++ Lead中期待的那样。
error[E0507]: cannot move out of `self.worker_handle.0` which is behind a mutable reference
  --> src/main.rs:27:28
   |
27 |         let handle = match self.worker_handle {
   |                            ^^^^^^^^^^^^^^^^^^ help: consider borrowing here: `&self.worker_handle`
28 |             None => return,
29 |             Some(x) => x,
   |                  -
   |                  |
   |                  data moved here
   |                  move occurs because `x` has type `std::thread::JoinHandle<()>`, which does not implement the `Copy` trait

error: aborting due to previous error
error[E0507]:无法移出可变引用后面的'self.worker_handle.0'
-->src/main.rs:27:28
|
27 |让句柄=匹配self.worker|u句柄{
在这里,借用一下:‘自我&工夫句柄’。
28 |无=>返回,
29 |一些(x)=>x,
|                  -
|                  |
|数据移到这里
|之所以发生移动,是因为'x'的类型为'std::thread::JoinHandle',而该类型未实现'Copy'特性
错误:由于上一个错误而中止
因此,很明显,我正在脱离通常的生锈模型,可能不应该使用这种策略

但我仍然希望构建某种接口,使我能够简单地加速数据流,而不必担心线程的管理,而且我不确定这样做的最佳方式

看来我有两个核心问题

1) 我如何创建一个函数在一个线程中运行,该线程接受来自外部源的数据,并且可以发出安全退出的信号?如果我有一个杀死它的原子布尔,我将如何在线程之间共享它


2) 完成后我如何处理加入线程?停止方法需要清理线程,但我不知道如何跟踪对它的引用。

很难回答一篇文章中提出的多个问题。请将它们分成多个问题,这样我们可以更好地帮助您,使您的问题能够帮助其他人e未来有一个与您相同的问题!看起来您的问题可能由的答案回答。如果没有,请您的问题解释差异。否则,我们可以将此问题标记为已回答。另请参阅@Shepmaster感谢这些链接,它们解决了我的问题!我在发布之前搜索了很多,但是我不知道应该做些什么来解决我的问题,特别是第二个问题。非常感谢你的回答。我不喜欢删除问题或标记为重复问题,因为它是两个问题的重复,被包装在一个拱形问题中。很难回答m在一篇文章中提出了多个问题。请将它们分成多个问题,这样我们可以更好地帮助您,并且您的问题将在将来帮助其他与您有相同问题的人!看起来您的问题可能由的答案回答。如果没有,请您的问题解释差异。否则,we可以将此问题标记为已回答。另请参阅@Shepmaster感谢您提供的链接,他们已经解决了我的问题!我在发布之前搜索了很多,但我不知道应该做些什么来解决我的问题,特别是第二个问题。非常感谢您的回复。我没有真正的选择删除问题或将问题标记为重复问题之间的区别,因为它是两个问题的重复,被包装成一个拱形问题。
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
  --> src/main.rs:20:54
   |
20 |           self.worker_handle = Some(std::thread::spawn(move || {
   |  ______________________________________________________^
21 | |             println!("Spawned");
22 | |             self.worker_fn();
23 | |         }));
   | |_________^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 18:5...
  --> src/main.rs:18:5
   |
18 | /     pub fn start(&mut self) {
19 | |         self.stop_flag = false;
20 | |         self.worker_handle = Some(std::thread::spawn(move || {
21 | |             println!("Spawned");
22 | |             self.worker_fn();
23 | |         }));
24 | |     }
   | |_____^
   = note: ...so that the types are compatible:
           expected &mut Handler
              found &mut Handler
   = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `[closure@src/main.rs:20:54: 23:10 self:&mut Handler]` will meet its required lifetime bounds
  --> src/main.rs:20:35
   |
20 |         self.worker_handle = Some(std::thread::spawn(move || {
   |                                   ^^^^^^^^^^^^^^^^^^

error: aborting due to previous error
error[E0507]: cannot move out of `self.worker_handle.0` which is behind a mutable reference
  --> src/main.rs:27:28
   |
27 |         let handle = match self.worker_handle {
   |                            ^^^^^^^^^^^^^^^^^^ help: consider borrowing here: `&self.worker_handle`
28 |             None => return,
29 |             Some(x) => x,
   |                  -
   |                  |
   |                  data moved here
   |                  move occurs because `x` has type `std::thread::JoinHandle<()>`, which does not implement the `Copy` trait

error: aborting due to previous error