简体   繁体   English

让类完全在自己的线程上运行

[英]Having a class run entirely on its own thread

If I do something like class myclass : public somelibrary::thread 如果我做类似class myclass : public somelibrary::thread事情class myclass : public somelibrary::thread

  1. Are all methods called on myclass executed in their own thread? 是否在myclass调用的所有方法都在各自的线程中执行?
  2. Is only run() executed in its own thread? 仅在自己的线程中执行run()吗?
  3. For every method to be executed in its own thread, do I have to create a new thread in each method? 对于要在其自己的线程中执行的每个方法,是否必须在每个方法中创建一个新线程?

If I do something like class myclass : public somelibrary::thread does this mean that all methods called on myclass will be called in their own thread? 如果我做class myclass : public somelibrary::thread事情,这是否意味着在myclass上调用的所有方法都将在自己的线程中调用?

Not necessarily. 不必要。 It depends on the design of somelibrary::thread . 这取决于somelibrary::thread的设计。 In fact, Boost.Thread is not used this way (via inheritance). 实际上, Boost.Thread不是通过这种方式使用的(通过继承)。 In that case, you simply create a new boost::thread object passing in a callable object (functors, function pointers, etc.) into its constructor. 在那种情况下,您只需创建一个新的boost::thread对象,即可将可调用对象(函数,函数指针等)传递到其构造函数中。

However, I haven't seen any thread library where calling any method on a class derived from somelibrary::thread spawns a new thread. 但是,我还没有看到任何线程库,在该库中,对从somelibrary::thread派生的类调用任何方法会产生一个新线程。 That is asking for data races, unless everything is somehow synchronized. 这就要求进行数据竞赛,除非所有内容都以某种方式同步。 But then your code would be wasting a lot of time waiting on synchronization primitives. 但是那样您的代码将浪费大量时间等待同步原语。 Not to mention that threads are relatively expensive in terms of operating system resources. 更不用说线程在操作系统资源方面相对昂贵。 An application may have at most several threads running at any one time assuming it wasn't designed by an idiot. 假设应用程序不是由白痴设计的,那么一个应用程序最多可以同时运行多个线程。 Methods on the other hand may be called many, many times during the application running time. 另一方面,在应用程序运行期间,方法可能被多次调用。

Is the only thing run in it's own thread in the "run" method of such thread? 在这种线程的“运行”方法中,唯一的事情是在自己的线程中运行吗?

Not all thread libraries call a run() method when running a new thread. 在运行新线程时,并非所有线程库都调用run()方法。 For example in Boost.Thread , the entry point is operator()() . 例如在Boost.Thread ,入口点是operator()() But yes, the entry point runs in its own thread, separate from the calling thread. 但是,是的,入口点在其自己的线程中运行,与调用线程分开。 All methods called by run() , directly or indirectly, runs in the newly-spawned thread. run()直接或间接调用的所有方法都在新产生的线程中运行。

If only the run method is executed in it's own thread, what if I want every method to be called in it's own thread instead of just that one? 如果仅在其自己的线程中执行run方法,那么如果我希望每个方法都在其自己的线程中而不是仅在该线程中被调用怎么办?

This is almost certainly not what you actually want in C++, because of the fact that threads are very expensive in terms of operating system resources. 这几乎肯定不是C ++真正想要的,因为线程就操作系统资源而言非常昂贵。 You'll be making your operating system waste time allocating and destroying threads all the time. 您将使操作系统始终浪费时间分配和销毁线程。 Even in languages like Erlang , where threads are extremely cheap compared to OS threads, you don't typically spawn a new thread for every single method. 即使在诸如Erlang之类的语言中,与OS线程相比,它们的线程也非常便宜,您通常也不会为每个方法都产生一个新线程。 A program running many threads or constantly creating/destroying threads is a sign of a badly-designed program. 运行许多线程或不断创建/销毁线程的程序是程序设计不良的标志。

Do I have to create a new thread in each method? 我必须在每个方法中创建一个新线程吗?

Yes, if you want each method to run in its own thread, but again, you almost certainly don't want this. 是的,如果您希望每个方法都在其自己的线程中运行,但是同样,您几乎肯定不希望这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM