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


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

Автор Тема: C - pthread_mutex_lock  (Прочитано 559 раз)

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

Оффлайн victor00000

  • Автор темы
  • Старожил
  • *
  • Сообщений: 15568
  • Глухонемой (Deaf)
    • Просмотр профиля
C - pthread_mutex_lock
« : 02 Августа 2018, 02:34:56 »
// 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 -k1n

pthread_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;
}

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


Пользователь добавил сообщение 02 Августа 2018, 02:57:06:
Цитировать
pthread_t *d = malloc (ia * sizeof (pthread_t));
ia - 1
pthread_t *d = malloc ((ia - 1) * sizeof (pthread_t));
или чепуха.
))
« Последнее редактирование: 02 Августа 2018, 02:57:06 от victor00000 »
Wars ~.o

 

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