用pthreads优雅地退出主线程
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

I'm currently taking a class where we are learning about synchronization of threads. The assignment asks us to first implement a simple threading library based on pthreads. They provided us with the following header file and told us that we should not be necessary to modify it in any way:

#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.
    */
};

My question is specifically about the GracefullyExitMainThread() function. I've been told I need to use pthread_join() in its implementation, but I don't know how I can pass a thread id to it when its a class method. Also, I would have thought they would include some kind of array or other structure in the header to keep track of all the threads created.

Sorry if my post is difficult to understand or read. I'm still learning all the nuances of C++ and this is my first post on stackoverflow. Any help is greatly appreciated.

#0

One solution would be to have a static std::vector(AKA a resizable array) that stores pthread_ids in the class. Then, whenever a thread is launched, it adds its own pthread_id to the std::vector.

You could also remove the pthread_id once the thread dies, but I am fairly sure pthread_join handles dead threads correctly, so there is no need.

Thus you now have a list of all the threads that have been started in a static member available for your static function. Simply loop over the list, and join all of them.

#1

Maybe you should read this, it has an example on how to join threads

https://computing.llnl.gov/tutorials/pthreads/

if you also read this you will see a description what "join" actually does with the threads and why don't need to create a list to keep track of all the threads :-)

https://computing.llnl.gov/tutorials/pthreads/man/pthread_join.txt

推荐文章

iOS:防止库崩溃整个应用程序

iOS:防止库崩溃整个应用程序

推荐文章

mysql查询和regexp问题

mysql查询和regexp问题

推荐文章

来自通知的Android刷新活动

来自通知的Android刷新活动

推荐文章

使用C#/.net 3.5从xml元素检索属性的最佳方法

使用C#/.net 3.5从xml元素检索属性的最佳方法

推荐文章

这个查询有什么问题

这个查询有什么问题

推荐文章

未定义的JavaScript替换为空

未定义的JavaScript替换为空

推荐文章

ASP.NET MVC在url中隐藏内容目录

ASP.NET MVC在url中隐藏内容目录

推荐文章

SQL Server 2000查询性能

SQL Server 2000查询性能

推荐文章

异常值:呈现时捕获了noreversmatch:Reverse for'begin'with arguments'(u'google-oauth2',)'

异常值:呈现时捕获了noreversmatch:Reverse for'begin'with arguments'(u'google-oauth2',)'

推荐文章

如何使用自定义UITableViewCell在单个单元格中调整三个标签?

如何使用自定义UITableViewCell在单个单元格中调整三个标签?

推荐文章

在我测试过的3种设备上,多点触摸似乎都不起作用

在我测试过的3种设备上,多点触摸似乎都不起作用

推荐文章

关于使用类成员函数作为模板参数的问题

关于使用类成员函数作为模板参数的问题

推荐文章

ODBC或ODP.Net用于C#?

ODBC或ODP.Net用于C#?

推荐文章

在REST/WCF服务中将数据集从LINQ返回到SQL

在REST/WCF服务中将数据集从LINQ返回到SQL

推荐文章

将枚举作为文档的一种形式在数据库中保持镜像是一种最佳实践吗?

将枚举作为文档的一种形式在数据库中保持镜像是一种最佳实践吗?

推荐文章

JDK 1.4与jdk1.5中的java URI更改

JDK 1.4与jdk1.5中的java URI更改