User Tools

Site Tools


cs:posix:threads_example1

logo skenz.it

Return to Home page

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cs:posix:threads_example1 [2017/11/30 06:47] (current)
zioskenz created
Line 1: Line 1:
 +====== Threads Example 1 ======
 +**Concepts:​**\\
 +Example regarding basic functionalities of POSIX threads
  
 +**Text:**\\
 +Realizzare un programma di esempio di utilizzo delle funzionalit√† dei threads
 +
 +**Solution:​**\\
 +<file C threads_example1.c>​
 +/* 
 +   - Creazione di thread con pthread_create
 +   - Passaggio ai thread di una struttura con valori diversi per ogni thread
 +   - Passaggio di un valore di ritorno tra i thread e il main
 +   - Attesa di thread con pthread_join
 +*/
 +
 +#include <​stdio.h>​
 +#include <​unistd.h>​
 +#include <​ctype.h>​
 +#include <​pthread.h>​
 +
 +#define N_THREAD 3
 +
 +
 +/* Variabile globale, tutti i thread vedono questo valore, se un thread ne modifica il valore anche gli altri thread lo vedono modificato */
 +int global_variable = 10;
 +
 +
 +typedef struct {
 +  int thread_num;
 +  char letter;
 +} my_t;
 +
 +
 +static void *process (void *arg)
 +{
 +  int i;
 +  my_t *param = (my_t *) arg; // cast the void argument to the real type
 +
 +  if (param->​thread_num == 0) global_variable = 20;
 +  fprintf (stdout, "​Starting thread %d, letter=%c, global_variable=%d\n",​ param->​thread_num,​ param->​letter,​ global_variable);​
 +  for (i = 0; i < 10; i++) 
 +    write (STDOUT_FILENO,​ &​(param->​letter),​ 1); /* Di default quando un processo viene lanciato vengono aperti i file descriptor 0, 1 e 2, ed associati rispettivamente a stdin, stdout e stderr */
 +  printf("​\n"​);​
 +  param->​letter = toupper(param->​letter);​
 +  pthread_exit(&​(param->​letter));​
 +}
 +
 +
 +int main (void)
 +{
 +  int retcode;
 +  pthread_t th[N_THREAD];​
 +  char *retval;
 +  my_t value[N_THREAD];​
 +  int i = 0;
 +  ​
 +  for(i=0; i<​N_THREAD;​ i++){
 +    value[i].letter = '​a'​+i;​
 +  }
 +
 +  for(i=0; i<​N_THREAD;​ i++){
 +    value[i].thread_num = i;
 +    retcode = pthread_create (&​th[i],​ NULL, process, (void *) &​(value[i]));​
 +    if (retcode != 0)
 +      fprintf (stderr, "​pthread_create failed %d\n", retcode);
 +  }
 +
 +
 +  for(i=0; i<​N_THREAD;​ i++){
 +
 +    retcode = pthread_join (th[i], (void *)&​retval);​
 +    if (retcode != 0)
 +      fprintf (stderr, "​pthread_join failed %d\n", retcode);
 +
 +    fprintf(stdout,​ "​retval : %c\n", *(retval) );
 + }
 +
 +  return 0;
 +}
 +</​file>​
 +
 +**Comments:​**\\

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/posix/threads_example1?rev=1512020853&do=diff
/web/htdocs/www.skenz.it/home/data/pages/cs/posix/threads_example1.txt · Last modified: 2017/11/30 06:47 by zioskenz

Privacy Policy