Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用pthreads优雅地退出主线程_C++_Pthreads - Fatal编程技术网

C++ 使用pthreads优雅地退出主线程

C++ 使用pthreads优雅地退出主线程,c++,pthreads,C++,Pthreads,我目前正在学习一门关于线程同步的课程。任务要求我们首先实现一个基于pthreads的简单线程库。他们向我们提供了以下头文件,并告诉我们无需以任何方式对其进行修改: #include <pthread.h> #include <cstring> class Task { protected: /* -- NAME */ static const int MAX_NAME_LEN = 15; char name[MAX_NAME_LEN];

我目前正在学习一门关于线程同步的课程。任务要求我们首先实现一个基于pthreads的简单线程库。他们向我们提供了以下头文件,并告诉我们无需以任何方式对其进行修改:

#include <pthread.h>
#include <cstring>


class Task {
protected:
    /* -- NAME */
    static const int MAX_NAME_LEN = 15;
    char name[MAX_NAME_LEN];

    /* -- IMPLEMENTATION */
    pthread_t thread_id;

    /* If you implement tasks using pthread, you may need to store
    the thread_id of the thread associated with this task.
    */
public:
    /* -- CONSTRUCTOR/DESTRUCTOR */
    Task(const char _name[]) {

    /* Create, initialize the new task. The task is started
    with a separate invocation of the Start() method. */

    std::strncpy(name, _name, MAX_NAME_LEN);
    }
    ~Task();
    /* -- ACCESSORS */
    char * Name();
    /* Return the name of the task. */

    /* -- TASK LAUNCH */
    virtual void Start();

    /* This method is used to start the thread. For basic tasks
    implemented using pthreads, this is where the thread is
    created and started. For schedulable tasks (derived from
    class Task) this is where the thread is created and handed
    over to the scheduler for execution. The functionality of
    the task is defined in "Run()"; see below. This method is
    called in the constructor of Task.
    */

    /* -- TASK FUNCTIONALITY */

    //make a thread here

    virtual void Run() = 0;
    /* The method that is executed when the task object is
    started. When the method returns, the thread can be
    terminated. The method returns 0 if no error. */

    /* -- MAIN THREAD TERMINATION */
    static void GracefullyExitMainThread();
    /* This function is called at the end of the main() function.
    Depending on the particular thread implementation, we have
    to make sure that the main thread (i.e., the thread that
    runs executes the main function) either waits until all
    other threads are done or exits in a way that does not
    terminate them.
    */
};
#包括
#包括
课堂任务{
受保护的:
/*--名字*/
静态常数int MAX_NAME_LEN=15;
字符名[MAX_name_LEN];
/*--实施*/
pthread_t thread_id;
/*如果使用pthread实现任务,则可能需要存储
与此任务关联的线程的线程id。
*/
公众:
/*--构造函数/析构函数*/
任务(常量字符_名称[]){
/*创建、初始化新任务。任务已启动
通过单独调用Start()方法*/
std::strncpy(名称、名称、最大名称);
}
~Task();
/*--访问者*/
char*Name();
/*返回任务的名称*/
/*--任务启动*/
虚拟void Start();
/*此方法用于启动线程。对于基本任务
使用pthreads实现,这就是线程所在的位置
已创建并已启动。对于可调度任务(从
类任务)这是创建和传递线程的地方
移交给调度程序执行。的功能
任务在“Run()”中定义。请参阅下面的。此方法是
在任务的构造函数中调用。
*/
/*--任务功能*/
//在这里做一根线
虚空运行()=0;
/*创建任务对象时执行的方法
已启动。当方法返回时,线程可以
已终止。如果没有错误,则该方法返回0*/
/*--主线程终止*/
静态void GracefullyExitMainThread();
/*在main()函数的末尾调用此函数。
根据特定的线程实现,我们有
确保主线程(即
运行(执行主函数)或等到所有
其他线程以不允许的方式完成或退出
终止他们。
*/
};

我的问题是关于
GracefullyExitMainThread()
函数的。有人告诉我需要在它的实现中使用
pthread\u join()
,但我不知道当它是类方法时如何向它传递线程id。此外,我还以为它们会在头中包含某种数组或其他结构,以跟踪创建的所有线程


对不起,如果我的帖子很难理解或阅读。我仍然在学习C++的所有细微差别,这是我在StAcExobe上的第一篇文章。非常感谢您的帮助。

一种解决方案是使用静态std::vector(也称为可调整大小的数组)在类中存储pthread_id。然后,每当启动线程时,它都会将自己的pthread_id添加到std::vector中

一旦线程死亡,您也可以删除pthread_id,但我相当肯定pthread_join正确地处理死线程,因此没有必要


因此,您现在有了一个静态成员中已启动的所有线程的列表,这些线程可用于静态函数。只需在列表上循环,并加入所有线程。

也许您应该阅读此文章,其中有一个关于如何加入线程的示例

如果您也阅读了本文,您将看到一个描述“join”实际上对线程做了什么,以及为什么不需要创建一个列表来跟踪所有线程:-)


GracefullyExitMainThread的定义中有一个“或”,这使得它无法实现。选择一个。通常你不想在头文件中声明变量;您可以直接在.c或.cpp文件中执行此操作。标头包含其他源文件需要知道的函数和变量声明。@AdamLiss他声明了哪些额外变量?“我原以为标头中会包含某种数组或其他结构,以跟踪创建的所有线程。”有一次,我继承了一个刚刚学习C语言的开发团队。他们认为头文件应该导入其相应源文件所需的所有内容,而不是导出公共部分。理解标题是一个重要的基础;如果你弄错了,你的生活会很悲惨。:-)@阿达姆利斯啊!这为我澄清了一些事情。所以我认为最好的方法是使用静态向量或数组?谢谢,我想我想知道为什么他们没有在头文件中包含声明,如果这是最好的方法,或者至少是我想要的方法。Ben,他们没有,因为你不应该这样做,这可以在不保留列表的情况下实现。