2012-03-03 6 views
2

私は現在、スレッドの同期について学んでいるクラスを取っています。この割り当てでは、まずpthreadに基づいて簡単なスレッドライブラリを実装するように求められます。彼らは、次のヘッダーファイルを提供してくれて、私たちはどのような方法でそれを変更する必要はないことを私たちに語った: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. 
    */ 
}; 

私の質問はGracefullyExitMainThread()機能については、具体的です。私はその実装でpthread_join()を使用する必要があると言われましたが、クラスメソッドのときにスレッドIDを渡す方法がわかりません。また、ヘッダーには、作成されたすべてのスレッドを追跡するための配列やその他の構造体が含まれていると思います。

申し訳ありませんが、私の投稿を理解したり読んだりするのが難しいです。私はまだC++のすべてのニュアンスを学んでいますが、これはstackoverflowに関する私の最初の投稿です。どんな助けでも大歓迎です。

+0

:-)すべてのスレッドを追跡するためにリストを作成する必要がある 『または』実装することが不可能にGracefullyExitMainThreadの定義です。 1つを選択します。 – stark

+0

一般に、ヘッダファイルに変数を宣言したくないのです。それは.cまたは.cppファイルで直接行います。ヘッダーには、他のソースファイルが知っておく必要がある関数と変数の宣言が含まれています。 –

+0

@AdamLiss彼は何の追加変数を宣言していますか? – Lalaland

答えて

1

解決策の1つは、クラスにpthread_idsを格納する静的なstd :: vector(サイズ変更可能な配列)を使用することです。次に、スレッドが起動されるたびに、自身のpthread_idをstd :: vectorに追加します。

スレッドが終了するとpthread_idを削除することもできますが、pthread_joinはデッドスレッドを正しく処理するため、必要はありません。

これで、静的関数で使用できる静的メンバーで開始されたすべてのスレッドの一覧が表示されるようになりました。単にリストをループして、それらのすべてに参加してください。

+0

ありがとう、私はこれが最良の方法であるか、少なくとも私がそれを行うための意図された方法であるならば、彼らがヘッダーファイルに宣言を含まなかった理由を疑問に思っていたと思います。 –

0

たぶん、あなたがこれを読んでください、それはあなたもこれを読めば、あなたが実際のスレッドで何をするか、「参加」の説明と、なぜドンが表示されますスレッド

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

に参加する方法についての例があります「Tは

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

+0

ベン、彼らはあなたがそうしなければならない場所ではなかったので、これはリストを残さずに達成することができます。 –

関連する問題