0

私はCpthreadを使用してスレッドプールを作成しています。それがどのように機能するかについては理解していますが、複雑さについていくつか質問があります。

実行する関数ポインターのリストを含む、スレッドプールの表現となる構造体を作成しました。これをwork_listと呼びます。threadpool構造体は、アクセスを同期するためのミューテックス(?)と条件、スレッド数のint、および各作業スレッドのスレッドIDを保持する配列も保持します。work_list自体は、完了する関数を表す構造体を保持します。 structsは、関数に対してvoid *、引数に対してvoid *、結果を配置するためにvoid*を保持します。このアイデアをコーディングすると、次のようになります。

typedef struct threadpool
{
    list work_list;
    pthread_t* tidArray;
    int num_threads;
    pthread_mutex_t lock;
    pthread_cond_t condition;
} threadpool;

と:

typedef struct fuFunction
{
    void* functionCall;
    void* functionArgs;
    void* returnValue;
    list_elem elem;
} fuFunction;

私は現在、プールを初期化するスレッドを持っています。int num_of_threadsを受け取り、すべてのメンバーが初期化されたスレッドプールのインスタンスへのポインターを返します。私が作成したボディは次のようになります。

threadpool * threadpool_init(int num_of_threads)
{
    threadpool* retPool = (threadpool*) malloc(sizeof(threadpool));

    //Initialize retPool members

    int x;
    for(x = 0; x < num_of_threads; x++)
    {
            pthread_t tid;

            if( pthread_create(&tid, NULL, thread_start, retPool) != 0)
            {
                    printf("Error creating worker thread\nExting\n");
                    exit(1);
            }

            retPool->tidArray[x] = tid;
    }

    return retPool;
}

開始時に各スレッドが実行する関数、worker関数thread_starは、これまでのところ次のようになっています。

void *thread_start(void* args)
{
    threadpool* argue = (threadpool*) args;

    pthread_mutex_lock(&(argue->lock));
    while(\* threadpool not shut down*\)
    {
            if(!list_empty(&argue->work_list))
            {
                    fuFunction* tempFu = list_entry(list_pop_front(&argue->workQ), fuFunction, elem);

                    \\WHAT TO PUT HERE
            }

            pthread_cond_wait(&argue->condition, &argue->lock);
    }
    pthread_mutex_unlock(&(argue->lock));
}

私の質問は、私が現在持っているこのコードが正しいと仮定すると、ワーカースレッドがワーカー関数で作成するtempFuで関数を実行するようにするにはどうすればよいですか?これが長いか混乱している場合は申し訳ありませんが、会話で説明する方がはるかに簡単です。これがFUBARの場合は、私にも知らせてください。

4

1 に答える 1

0

構造体要素の署名「void*functionCall;」間違っている。代わりに関数ポインタを使用してください。例えば:

typedef struct fuFunction
{
    void* (*functionCall)( void* arg);
    void* functionArgs;
    void* returnValue;
    list_elem elem;
} fuFunction;

次にそこに置きます:

tempfu->returnValue = (*tempfu->functionCall)(tempfu->functionArgs);
于 2013-03-26T14:33:56.497 に答える