- 论坛徽章:
- 0
|
void* thr_fn(void* arg)
{
unsigned int seq = *(unsigned int *)arg;
fprintf(stderr, "th_fun thread %lx seq %u\n", pthread_self(), seq);
pthread_exit(NULL);
}
int main()
{
pthread_t tid[5];
unsigned int i;
for(i = 0; i < 5; i++)
{
pthread_create(tid + i, NULL, thr_fn, (void *)&i))
fprintf(stderr, "thread %lx seq %u\n", tid[i], i);
}
return 0;
}
结果是
thread 7f3803ba6700 seq 0
thread 7f38033a5700 seq 1
thread 7f3802ba4700 seq 2
thread 7f38023a3700 seq 3
thread 7f3801ba2700 seq 4
th_fun thread 7f38023a3700 seq 0 (序号应为3)
th_fun thread 7f3801ba2700 seq 0 (序号应为4)
th_fun thread 7f3803ba6700 seq 0
th_fun thread 7f38033a5700 seq 1
th_fun thread 7f3802ba4700 seq 2
centos 7.2 gcc 4.8.5
应该怎样修改程序子线程才能传入正确的序号? |
|