Форум русскоязычного сообщества Ubuntu


Увидели сообщение с непонятной ссылкой, спам, непристойность или оскорбление?
Воспользуйтесь ссылкой «Сообщить модератору» рядом с сообщением!

Автор Тема: C - pthread_join void**  (Прочитано 564 раз)

0 Пользователей и 1 Гость просматривают эту тему.

Оффлайн victor00000

  • Автор темы
  • Старожил
  • *
  • Сообщений: 15568
  • Глухонемой (Deaf)
    • Просмотр профиля
C - pthread_join void**
« : 02 Августа 2018, 01:10:18 »
// test_pjoin.c
// victor00000 (c) 2018
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
// ~$ gcc test_pjoin.c -o test_pjoin -lpthread
// ~$ ./test_pjoin /* 1 2 3

void *a (void *b){
char *c = strdup ((char*) b);
c = realloc (c,strlen (c) + strlen (" -> ok") + 1);
sprintf (c+strlen (c)," -> ok");

return c;
}

int main (int ia, char *ca[]){
pthread_t d;
int e;
for (e=1;e<ia;e++){
void *f;
char *g;
pthread_create (&d,0,a,ca[e]);
pthread_join (d,&f);
g = (char*) f;
printf ("%s\n",g);
free ( g );
}
return 0;
}

http://paste.ubuntu.com/p/WgnXpVJb2h/

Пользователь добавил сообщение 02 Августа 2018, 01:52:53:
// test_pjoinA.c
// victor00000 (c) 2018
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
// #include <unistd.h>

// ~$ gcc test_pjoinA.c -o test_pjoinA -lpthread
// ~$ ./test_pjoinA /* 1 2 3

void *a (void *b){
char *c = strdup ((char*) b);
c = realloc (c,strlen (c) + strlen (" -> ok") + 1);
sprintf (c+strlen (c)," -> ok");
// sleep (1);
return c;
}

int main (int ia, char *ca[]){
if (ia == 1) return 0;
pthread_t *d = malloc (ia * sizeof (pthread_t));
int e;
for (e=1;e<ia;e++){
pthread_create (&d[e],0,a,ca[e]);
}

for (e=1;e<ia;e++){
void *f;
char *g;
int h;
h = pthread_join (d[e],&f);
g = (char*) f;
printf ("%s\n",g);
if (h == 0) free ( g );
}
free (d);
return 0;
}

http://paste.ubuntu.com/p/fGQBw3p353/
« Последнее редактирование: 02 Августа 2018, 01:52:53 от victor00000 »
Wars ~.o

 

Страница сгенерирована за 0.037 секунд. Запросов: 25.