지금 스레드에 대해 배우고 있습니다. 내 스레드 변수를 전달할 수 있는지 궁금하네요. 제 할당은 스레드를 만들고 각 스레드에 번호 (원하는 경우 이름)를 지정하고 100ms마다 번호를 인쇄하는 것입니다. 현재 프로그램은 다음과 같습니다 :새로 생성 된 스레드에 하나 이상의 변수를 전달하는 방법은 무엇입니까?
#define PCHECK(sts,msg) if((sts)!=0){printf("error : %s\n",msg); exit(EXIT_FAILURE)}
#define NB_THREAD 5
void* do_nothing(void* data)
{
int i;
//printf("creation thread %d",(int)data);
while(1)
{
usleep(100000);
printf("thread number : %d \n",data);
}
i = 0;
pthread_exit(NULL);
//exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[])
{
int pid, j, status;
pthread_t thread;
int * res;
int stat;
for (j = 0; j < NB_THREAD; j++)
{
stat = pthread_create(&thread, NULL, do_nothing, (void *) j);
if (stat !=0)
{
perror ("pthread_create()");
}
}
for (j = 0; j < NB_THREAD; j++)
{
pthread_join(thread, (void **) &res);
}
return EXIT_SUCCESS;
}
순간적으로 인쇄되는 숫자는 0 (데이터 값)입니다. 내가 잘못 감사합니다 :)
감사합니다.이 링크는 매우 유용합니다. 스레드에 관한 모든 것을 다룹니다. 이제 내 수업을 더 쉽게 할 수있을거야 :) – kaninabu
물론입니다. 당신은 또한 스레드 스택, 스레드 개인 데이터를 탐색 할 수 있습니다 .. –