1

私の ThreadData 構造体:

typedef struct threadData {
    pthread_t *ths;
} threadData;

*ths は の配列ですpthread_t

ここで、ths[1] に新しいスレッドを作成する次の関数をアクションとして使用するスレッドを作成します。

void *rootThread(threadData *d) {
    pthread_t *b = (*d).ths;
    pthread_create(*(b+1),NULL,someRandomFunction,NULL);
}

しかし、それはうまくいかないようです。

pthread_t 要素を適切に逆参照しているかどうかはわかりません。助けてください!

ありがとう、 :)。

4

2 に答える 2

1

(たとえば)割り当てていないようです。次のようなことをしなければなりません:

void* Thread(void* theCUstom);

pthread_t* threadHandle = malloc(sizeof(pthread_t));
pthread_mutex_t mutex; // mutex lock
pthread_attr_t attr;   // thread attributes
pthread_mutex_init(&mutex, NULL);
pthread_attr_init(&attr);
unsigned long errRes = pthread_create(threadHandle, &attr, Thread, yourCustom);
于 2013-02-24T22:36:49.340 に答える
0

そのように使用する pthread_t のインデックスを維持することはできません。rootThread() を再入力するたびに b+1 は一定になります。おそらく、threadData に個別のインデックス変数、またはリストを反復処理できる 2 番目のポインターが必要です。そうするか、一時変数 pthread_t *b を作成しないでください。

typedef struct threadData {
    pthread_t *ths;
    int thsIdx;
} threadData;

void *rootThread(threadData *d) {
    pthread_create( &d->ths[d->thsIdx++],NULL,someRandomFunction,NULL);
}

またはあなたの方法:

 void *rootThread(threadData *d) {
    pthread_create( d->ths, NULL, someRandomFunction, NULL);
    ++d->ths; // this is nasty because you lose the pointer to the beginning of the array.
}
于 2013-02-24T22:33:01.800 に答える