URL: https://www.opennet.me/cgi-bin/openforum/vsluhboard.cgi
Форум: vsluhforumID9
Нить номер: 5188
[ Назад ]

Исходное сообщение
"pthread_create() всегда возвращает одинаковый ID"

Отправлено roman , 28-Фев-06 14:53 
Приветствую.

Изучаю треды, вот простенькая программа, проблема в том, что ВСЕГДА тред создается с одним и тем же 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. В чем может быть проблема?
Спасибо!


Содержание

Сообщения в этом обсуждении
"pthread_create() всегда возвращает одинаковый ID"
Отправлено Sergey , 28-Фев-06 16:04 
> printf("thread %lu run ", *((pthread_t *)tid));

Попробуй выводить результат pthread_self. Она должна сказать текущий thread id.


"pthread_create() всегда возвращает одинаковый ID"
Отправлено йцукенг , 28-Фев-06 16:08 
попробуй распечатать этот tid из main после создания потока.
имхо у тебя переключение потока происходит после того, как создался thread, но до того, как в main происходит присваивание переменной tid значения, возвращенного pthread_create.

"pthread_create() всегда возвращает одинаковый ID"
Отправлено chip , 28-Фев-06 18:50 
>Приветствую.
>
>Изучаю треды, вот простенькая программа, проблема в том, что ВСЕГДА тред создается
>с одним и тем же 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.

Может быть в этом? Стоит порыть рассылки на этот счёт.