Publi

Concurrencia, POSIX threads y variables compartidas en C


Hace poco veíamos cómo empezar a hacer nuestros primeros programas multi-thread utilizando POSIX threads. Aunque pronto surge una nueva necesidad: compartir datos entre el proceso principal y el thread que se ha creado, al menos para decirle qué queremos que haga. Para eso podemos utilizar el último argumento de la función pthread_create(), el cuál es un puntero a void, por lo que podemos pasar cualquier tipo de variable. Por ejemplo un número:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *newtask(void *_number)
{
  int number = *(int*)_number;
  printf("The number I was asked for: %d\n", number);
  pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t thread;
   int rc;
   int i;
   int number = 99;

   printf ("Main process just started.\n");
   rc = pthread_create(&thread, NULL, newtask, &number);
   if (rc)
     {
       printf("ERROR in pthread_create(): %d\n", rc);
       exit(-1);
     }

   printf ("Main process about to finish.\n");
   /* Last thing that main() should do */
   pthread_exit(NULL);
}

Recordad que hay que compilar utilizando pthread:

$ gcc -o simplepass simplepass.c -lpthread

De esta forma, el thread puede leer el valor que le hemos pasado desde number como veremos si ejecutamos el programa. Justo al empezar la función newtask(), extraemos el número desde el puntero a void _number. Aunque si cambiamos un poco el código, nos damos cuenta de que también podemos escribir en la variable number para que el proceso principal pueda leer lo que ha escrito el thread secundario que hemos lanzado. (En lugar de esto, se puede utilizar una variable global, ya que el thread principal y el secundario comparten la zona de datos, esto es totalmente posible, pero no me gusta crear variables globales):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *newtask(void *_number)
{
  int *number = _number;
  printf("The number I was asked for: %d\n", *number);
  *number = 10;
  pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t thread;
   int rc;
   int i;
   int number = 99;

   printf ("Main process just started.\n");
   rc = pthread_create(&thread, NULL, newtask, &number);
   if (rc)
     {
       printf("ERROR in pthread_create(): %d\n", rc);
       exit(-1);
     }

   usleep(100000);

   printf (" Before exiting. Number = %d\n", number);

   printf ("Main process about to finish.\n");
   /* Last thing that main() should do */
   pthread_exit(NULL);
}

Ahora bien, si queremos pasar múltiples variables al thread que vamos a crear, podemos perfectamente crear un struct que contenga toda la información:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

struct thread_vars_t
{
  int number;
  char string[50];
};

void *newtask(void *_number)
{
  struct thread_vars_t *vars = _number;

  printf("The number I was asked for: %d\n", vars->number);
  vars->number = 10;
  strcpy(vars->string, "Hello world from thread");
  pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t thread;
   int rc;
   int i;
   struct thread_vars_t *vars = malloc (sizeof(struct thread_vars_t));


   vars->number = 99;

   printf ("Main process just started.\n");
   rc = pthread_create(&thread, NULL, newtask, vars);
   if (rc)
     {
       printf("ERROR in pthread_create(): %d\n", rc);
       exit(-1);
     }

   usleep(100000);

   printf (" Before exiting. Number = %d\n", vars->number);
   printf (" Before exiting. String = %s\n", vars->string);

   printf ("Main process about to finish.\n");
   /* Last thing that main() should do */
   pthread_exit(NULL);
}

En ordenadores más lentos, tal vez tengamos que aumentar el valor de usleep(), estoy simulando una espera, aunque esto bien podía ser el establecimiento de un valor. Aunque esto ya supone un problema,tenemos que variar el valor de la espera dependiendo de las capacidades del ordenador, aunque podemos solucionarlo de diferentes maneras. La primera de ellas, como muestra de algo que no debemos hacer es utilizar una espera activa:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

struct thread_vars_t
{
  int ready;
  int number;
  char string[50];
};

void *newtask(void *_number)
{
  struct thread_vars_t *vars = _number;

  printf("The number I was asked for: %d\n", vars->number);
  vars->number = 10;
  strcpy(vars->string, "Hello world from thread");
  vars->ready = 1;
  pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t thread;
   int rc;
   int i;
   struct thread_vars_t *vars = malloc (sizeof(struct thread_vars_t));

   vars->ready = 0;
   vars->number = 99;

   printf ("Main process just started.\n");
   rc = pthread_create(&thread, NULL, newtask, vars);
   if (rc)
     {
       printf("ERROR in pthread_create(): %d\n", rc);
       exit(-1);
     }

   //   usleep(100000);
   while (!vars->ready);
   printf (" Before exiting. Number = %d\n", vars->number);
   printf (" Before exiting. String = %s\n", vars->string);

   printf ("Main process about to finish.\n");
   /* Last thing that main() should do */
   pthread_exit(NULL);
}

En este caso hemos puesto una variable (vars->ready) que cuando sea 1 significará que los datos están definidos, y eso lo hará el thread secundario, pero el principal por su parte tiene un bucle para quedarse quieto mientras vars->ready vale 0:

1
while (!vars->ready);

Aunque esto tiene un gran problema: el proceso estará comiendo recursos de sistema mientras la condición no se cumpla, podemos verlo desde cualquier gestor de tareas, si por ejemplo ponemos un sleep(20) en el thread secundario justo antes de vars->ready=1, es más si lo pensamos bien, si el thread secundario está haciendo algún tipo de procesamiento para el cálculo de esos valores, y el thread principal tiene este tipo de espera activa, el principal estará entorpeciendo al secundario y haciéndole más lento, por lo que tendremos que buscar otro tipo de solución como semáforos, señales o mutex entre otras. Que quedarán para otro post.

Foto: OakleyOriginals (Flickr) CC-by

También podría interesarte....

There are 3 comments left Ir a comentario

  1. Pingback: BlogESfera.com /

  2. Madeline /
    Usando Google Chrome Google Chrome 120.0.0.0 en Windows Windows NT

    With the help of our instructions, you’ll be able to solve any wordle answer today with ease and efficiency.

  3. Tracker Justin Hartley Bomber Jacket /
    Usando Google Chrome Google Chrome 121.0.0.0 en Windows Windows NT

    Excellent information providing by your Article thank you for taking the time to share with us such a nice article.

Leave a Reply to Anónimo Cancle Reply