简体   繁体   English

Pthreads-主线程和其他线程

[英]Pthreads - main thread and other thread

In Pthreads, when we create multiple threads inside the main function, does all the created threads become a worker thread for the main thread? 在Pthreads中,当我们在主函数中创建多个线程时,是否所有创建的线程都成为主线程的工作线程? or each thread is a manager thread by itself? 还是每个线程本身就是一个管理器线程?

when we create multiple threads inside the main function, does all the created threads become a worker thread for the main thread? 当我们在主函数中创建多个线程时,是否所有创建的线程都成为主线程的工作线程?

Worker/main thread designation is conceptual. 工作者/主线程的指定是概念性的。

There's no Pthreads designated thread that manages all the other worker threads. 没有Pthreads指定的线程来管理所有其他工作线程。 Typically, main thread is used for that job. 通常,主线程用于该作业。 But nothing prevents you from choosing another thread to manage all the threads. 但是,没有什么可以阻止您选择另一个线程来管理所有线程。 Main thread can also participate in the the "work" as a worker. 主线程也可以作为工作者参加“工作”。

each thread is a manager thread by itself? 每个线程本身就是一个管理器线程?

A thread is an independent entity within a process. 线程是流程中的独立实体。 Usually, they all "co-ordinate" with each other to accomplish a common goal. 通常,他们都相互“协调”以达成共同的目标。 But you can also have multiple groups of threads, with each group doing different tasks. 但是您也可以有多组线程,每组线程执行不同的任务。 So there doesn't necessarily have to be a "manager thread". 因此,不必一定要有“管理线程”。 Because a thread can be completely independent and exit (pthread_exit) without ever needing to communicate with others (eg Each thread sorting a separate file). 因为一个线程可以完全独立并退出(pthread_exit),而无需与其他线程进行通信(例如,每个线程对一个单独的文件进行排序)。 Or the threads may co-ordinate with each other (eg matrix multiplication). 或者线程可以彼此协调(例如,矩阵乘法)。 Or they may be "managed" by a one thread (eg main thread "feeding" work to others in queue which will be worked on by other threads in a master/slaves manner). 或者,它们可以由一个线程“管理”(例如,主线程将工作“馈送”给队列中的其他人,其他线程将以主/从方式对其进行处理)。

When you create a thread, you can call it a "worker" if you want. 创建线程时,可以根据需要将其称为“工人”。 The thread doesn't care how people think about it. 该线程不在乎人们如何看待它。 It runs the same whether you call it a "worker", "manager", or "moose" for all it matters. 无论您称其为“工人”,“经理”还是“驼鹿”,它的运行都相同。

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

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