Увидели сообщение с непонятной ссылкой, спам, непристойность или оскорбление?Воспользуйтесь ссылкой «Сообщить модератору» рядом с сообщением!
0 Пользователей и 1 Гость просматривают эту тему.
// test_pmutex.c// victor00000 (c) 2018#define _GNU_SOURCE#include <stdio.h>#include <pthread.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <time.h>// ~$ gcc test_pmutex.c -o test_pmutex -lpthread// ~$ ./test_pmutex /* 1 2 3// ~$ ./test_pmutex /* 1 2 3 | sort -k1npthread_mutex_t m;int i = 0;void *a (void *b){usleep (100 * (rand () % 10));int mi;pthread_mutex_lock (&m);i++;mi = i;pthread_mutex_unlock (&m);char *c;asprintf (&c,"%-5d %s",mi,(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[]){srand (time (NULL));pthread_mutex_init (&m,0);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);fflush (stdout);if (h == 0) free ( g );}free (d);return 0;}
Код: [Выделить]pthread_t *d = malloc (ia * sizeof (pthread_t));
pthread_t *d = malloc (ia * sizeof (pthread_t));
pthread_t *d = malloc ((ia - 1) * sizeof (pthread_t));
Страница сгенерирована за 0.032 секунд. Запросов: 25.