Приветствую.Изучаю треды, вот простенькая программа, проблема в том, что ВСЕГДА тред создается с одним и тем же ID:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>/* read dice on standard input, write results on standard output */
void *roll_dice(void *tid)
{
printf("thread %lu run ", *((pthread_t *)tid));
pthread_exit(NULL);
}int main(void)
{
pthread_t tid;while (1) {
if (pthread_create(&tid, NULL, roll_dice, (void *)&tid) != 0) {
perror("pthread_create()");
return -1;
}if (pthread_join(tid, NULL) != 0) {
perror("pthread_join()");
return -1;
}
printf("and terminated, sleep 3 sec.\n");
sleep(3);
}return 0;
}Вывод программы такой:
thread 1082354880 run and terminated, sleep 3 sec.
thread 1082354880 run and terminated, sleep 3 sec.
thread 1082354880 run and terminated, sleep 3 sec.
...ОС - линукс 2.4.x. В чем может быть проблема?
Спасибо!
> printf("thread %lu run ", *((pthread_t *)tid));Попробуй выводить результат pthread_self. Она должна сказать текущий thread id.
попробуй распечатать этот tid из main после создания потока.
имхо у тебя переключение потока происходит после того, как создался thread, но до того, как в main происходит присваивание переменной tid значения, возвращенного pthread_create.
>Приветствую.
>
>Изучаю треды, вот простенькая программа, проблема в том, что ВСЕГДА тред создается
>с одним и тем же ID:
>
>#include <stdio.h>
>
>#include <unistd.h>
>#include <pthread.h>
>
>/* read dice on standard input, write results on standard output */
>
>void *roll_dice(void *tid)
>{
> printf("thread %lu run ", *((pthread_t *)tid));
> pthread_exit(NULL);
>}
>
>int main(void)
>{
> pthread_t tid;
>
> while (1) {
> if (pthread_create(&tid, NULL, roll_dice, (void *)&tid) != 0) {
> perror("pthread_create()");
> return -1;
> }
>
> if (pthread_join(tid, NULL) != 0) {
> perror("pthread_join()");
> return -1;
> }
> printf("and terminated, sleep 3 sec.\n");
> sleep(3);
> }
>
> return 0;
>}
>
>Вывод программы такой:
>
>thread 1082354880 run and terminated, sleep 3 sec.
>thread 1082354880 run and terminated, sleep 3 sec.
>thread 1082354880 run and terminated, sleep 3 sec.
>...
>> gcc -lpthread test.c
> ./a.out
thread 134558720 run and terminated, sleep 3 sec.
thread 134559232 run and terminated, sleep 3 sec.
thread 134559744 run and terminated, sleep 3 sec.
thread 134560256 run and terminated, sleep 3 sec.
thread 134560768 run and terminated, sleep 3 sec.
^C
> uname -rs
FreeBSD 6.0-STABLE
>>ОС - линукс 2.4.x.
Может быть в этом? Стоит порыть рассылки на этот счёт.