1

My ThreadData struct:

typedef struct threadData {
    pthread_t *ths;
} threadData;

Where *ths is an array of pthread_t.

Now, I create a thread that uses as action the following function, which creates a new thread in ths[1]

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

But that doesn't seem to work.

I'm not sure if I'm dereferencing the pthread_t element well. Please help!

Thanks, :).

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 中有一个单独的索引变量,或者需要一个可以遍历列表的第二个指针。要么,要么不创建临时变量 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 回答