4

私は次の機能に取り組んでいます。この関数は n 個のスレッドを作成する必要があります。また、子スレッドのtidを出力する必要があります。しかし、現時点では少し混乱しています。それを実行すると、たとえば 5 つのスレッドを作成すると、常に同じ tid が返されます。私が理解している限り、tid は呼び出し元のスレッド ID です。同じ呼び出し元がこれらすべてのスレッドを呼び出していますか、それとも何か問題がありましたか? コードは次のとおりです。

void spawnThreads( unsigned int n) {
   int threads = n, ret = -1;
   pthread_t * thread = malloc(sizeof(pthread_t)*threads);
   pid_t tid;
   int i;
   for(i = 0; i < threads; i++) {
       ret = pthread_creation(&thread[i], NULL, (void *(*)(void *)) foo, NULL); // foo does not do anything

       if( ret != 0) {
           printf("pthread error!\n");
       }

       tid = syscall(SYS_gettid);
       printf("%d %d\n", i, tid);
       printf("I just created thread %d\n", i);

       pthread_join(thread[i],NULL);
}

void * foo(void) {
    return NULL;
}

たとえば、次の入力spawnThreads(4) に対して次の出力を取得します。

 0 2411
 I just created thread 0

 1 2411
 I just created thread 1

 2 2411
 I just created thread 2

 3 2411
 I just created thread 3

要約すると、関数は>i< >tid<を出力する必要があります。>tid<は子の TID を示し、>i<は 1 から n まで続きます。

しかし、なぜ同じ tid の 4 倍になるのでしょうか? 私は何を間違えましたか?何がうまくいかなかったのか誰かが説明してくれれば幸いです。

4

2 に答える 2

0

プロセス ID、スレッド ID、pthreads スレッド ID という 3 つの情報が一般的に関心を集めています。すべてのpthread呼び出しは、独自のスレッド ID で使用するという点で自己完結型です。プロセス ID と OS スレッド ID は、pthreads API 以外の理由で重要になる場合があります。

pthread id は と によって報告されpthread_createますpthread_self。前者は作成したスレッドを報告し、後者はそれ自体を報告します。そうでなければ、鶏が先か卵が先かという状況です。あるスレッドは、自分の ID が何であるかを既に知っていない限り、別のスレッドにその ID を尋ねることはできません。これが重要な場合は、それを達成するために何らかのメカニズム、グローバル リスト、IPC などを構築する必要があります。

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <sys/syscall.h>
#include <string.h>

typedef struct
{
    int       i;
    pid_t     pid;   // linux pid
    pid_t     tid;   // linux thread id
    pthread_t ptid;  // pthreads tid    
} data;

void *foo(void *args)
{
    data *p = (data *) args;

    p->pid  = getpid();
    p->tid  = syscall(SYS_gettid);
    p->ptid = pthread_self();

    return(p);
}

void spawnThreads(unsigned int numThreads)
{
    int ret;
    pthread_t *tids = malloc(sizeof(pthread_t) * numThreads);

    int i;

    for (i = 0; i < numThreads; i++)
    {
        data *dp = malloc(sizeof(data) * numThreads);
        memset(dp, '\0', sizeof(*dp));

        dp->i = i;

        ret = pthread_create(&tids[i], NULL, foo, (void *) dp);

        if ( ret != 0)
            perror("pthread create error");
    }

    for (int i = 0; i < numThreads; ++i)
    {
        data *status;

        ret = pthread_join(tids[i], (void *) &status);

        if ( ret != 0)
            perror("pthread join error");
        else
        {
            printf("thread num %d joined and reports pthreadId of %lu "
                   "process pid of %d and linux tid of %d\n",
                   status->i, status->ptid, status->pid, status->tid);

            free(status);
        }
    }

    free(tids);
}

int main(int argc, char *argv[])
{
    printf("main thread reports pthreadId of............ %lu "
           "process pid of %d and linux tid of %ld\n",
           pthread_self(), getpid(), syscall(SYS_gettid));

    spawnThreads(5);

    return (0);
}
于 2013-11-29T02:43:35.577 に答える