2013-01-04 2 views
2

어떻게 p_thread의 ID를 배열에 저장할 수 있습니까?pthread_t id를 배열에 저장하는 방법

int i; 
pthread_t t[N]; 
float arrayId[N]; 


for (i = 0; i < N; i++) { 
    pthread_create(&t[i], NULL, f, (void *) &i); 
    printf("creato il thread id=%lu\n", t[i]); 
    arrayId[i] = t[i]; 
    printf("a[%d]=%f\n", i, arrayId[i]); 
} 

나는 그것을 인쇄 할 수 있습니다,하지만 난 저장할 수 아니에요 ...

나는이 배열을 정렬해야하고 내가 먼저 ID로 주문한 모든 스레드를 실행해야합니다

+0

'저장'이란 무엇을 의미합니까? 't'는 이미 각 쓰레드 ID를 포함하고 있기 때문에 '저장'되어 있으므로 다른 배열이 필요한 이유는 무엇입니까? 그리고 당신이 원한다고해도, 플로트를 사용하는 것은 이치에 맞지 않습니다. – stijn

+1

이것 좀보세요 http://stackoverflow.com/questions/1759794/how-to-print-pthread-t – benjarobin

답변

2

모든 스레드는 값 (동일한 주소)으로 전달하므로 i에 대해 동일한 값을 받게됩니다. 이 그것을 수정해야합니다 : 사람이 상태로

int i; 
pthread_t t[N]; 
float arrayId[N]; 

int indexes[N]; 

for (i = 0; i < N; i++) { 
    indexes[i] = i; 
    pthread_create(&t[i], NULL, f, (void *) &indexes[i]); 
    printf("creato il thread id=%lu\n", t[i]); 
    arrayId[i] = t[i]; 
    printf("a[%d]=%f\n", i, arrayId[i]); 
} 
1
I'll have to sort this array and then i'll have to execute first all the thread 
ordered by id 

pthread_create 이미 스레드를 실행합니다

The pthread_create() function starts a new thread in the calling process. 

그래서 루프가 이미 N 스레드를 시작합니다. 또한 스레드 ID를 지정할 수 없으며 스레드가 작성 될 때 리턴됩니다.

관련 문제