POSIX Threads: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
FrescoBot (discussione | contributi)
m Bot: tag vuoti
Funzionalità collegamenti suggeriti: 3 collegamenti inseriti.
 
(7 versioni intermedie di 7 utenti non mostrate)
Riga 1:
{{NN|informatica|ottobre 2018}}
I '''POSIX Threads''', solitamente denominati '''pthreads''', sono un [[Execution model|modello di esecuzione]] che esiste indipendentemente da un [[linguaggio di programmazione]], nonché un modello di esecuzione parallelo. Consente a un programma di controllare più flussi di lavoro diversi che si sovrappongono nel tempo. Ogni flusso di lavoro viene definito [[Thread (informatica)|thread]] e la creazione e il controllo di questi flussi si ottiene effettuando chiamate all'APIalla libreria dei thread POSIX (pThreads). POSIX Threads è un'APIuna libreria definita dalle estensioni standard di POSIX.1c, Thread (IEEE Std 1003.1c-1995).
 
Le implementazioni delledella APIlibreria pthreads (POSIX threads) sono disponibili su molti sistemi operativi simili a Unix e conformi allo standard POSIX, come [[FreeBSD]], NetBSD, OpenBSD, Linux, Mac OS X, Android, Solaris e AUTOSAR AdaptiveSolaris. In genere sono raggruppati in una libreria denominata '''libpthread'''. Esistono anche implementazioni DR-DOS e [[Microsoft Windows]]: all'interno del sottosistema SFU/SUA che fornisce un'implementazione nativa di un certo numero di API POSIX e anche all'interno di pacchetti di terze parti come pthreads-w32, che implementa pthreads sopra le API Windows esistente.
 
== Contenuti ==
Riga 17:
 
== Esempio ==
Un esempio che illustra l'uso di pthread in linguaggio C:<sourcesyntaxhighlight lang="c">
#include<stdio.h>
#include<stdlib.h>
Riga 33:
sleep(sleep_time);
printf("THREAD %d: Ended.\n",index+1);
return NULL;
}
 
Riga 44 ⟶ 45:
for(i=0;i<NUM_THREADS;i++){
printf("IN MAIN: Creating thread %d.\n",i+1);
threads[i]=i;
thread_args[i]=i;
result_code=pthread_create(&threads[i],NULL,perform_work,&thread_args[i]);
Riga 63:
}
 
</sourcesyntaxhighlight>Questo programma crea cinque thread, ognuno dei quali esegue la funzione ''perform_work'' che stampa il numero univoco del thread sullo standard output. Se un programmatore desidera che i thread comunichino tra loro, ciò richiederebbe la definizione di una variabile al di fuori dell'ambito di ciascuna funzione, rendendola una variabile globale. Questo programma può essere compilato usando il compilatore [[GNU Compiler Collection|gcc]] con il seguente comando:
gcc pthreads_demo.c -lpthread -o pthreads_demo
 
Riga 74:
 
== Note ==
<references />
{{reflist|2}}
 
== Bibliografia ==
Riga 98:
* Beginthread (una subroutine in Windows per creare un nuovo thread e uno Unix thread)
* State Threads, un approccio ''event driven'' al threading
 
<div class="cx-template-editor-source-container" lang="en" dir="ltr" style="display: none;"><div class="cx-template-editor-source"><div class="cx-template-editor-title" title="This template formats a citation to a book using the provided bibliographic information (such as author and title) as well as various formatting options.">Cite book</div><div class="cx-template-editor-param"><div class="cx-template-editor-param-title"><span id="title" class="cx-template-editor-param-key">Title</span></div><div class="cx-template-editor-param-value" data-key="title" style="position: relative;">UNIX Systems Programming</div></div><div class="cx-template-editor-param"><div class="cx-template-editor-param-title"><span id="last" class="cx-template-editor-param-key">Last name</span></div><div class="cx-template-editor-param-value" data-key="last" style="position: relative;">Kay A. Robbins</div></div><div class="cx-template-editor-param"><div class="cx-template-editor-param-title"><span id="last2" class="cx-template-editor-param-key">Last name 2</span></div><div class="cx-template-editor-param-value" data-key="last2" style="position: relative;">Steven Robbins</div></div><div class="cx-template-editor-param"><div class="cx-template-editor-param-title"><span id="publisher" class="cx-template-editor-param-key">Publisher</span></div><div class="cx-template-editor-param-value" data-key="publisher" style="position: relative;">Prentice-Hall</div></div><div class="cx-template-editor-param"><div class="cx-template-editor-param-title"><span id="year" class="cx-template-editor-param-key">Year of publication</span></div><div class="cx-template-editor-param-value" data-key="year" style="position: relative;">2003</div></div><div class="cx-template-editor-param"><div class="cx-template-editor-param-title"><span id="isbn" class="cx-template-editor-param-key">ISBN</span></div><div class="cx-template-editor-param-value" data-key="isbn" style="position: relative;">978-0-13-042411-2</div></div><div class="cx-template-editor-param"><div class="cx-template-editor-param-title"><span id="last-author-amp" class="cx-template-editor-param-key">Last author ampersand</span></div><div class="cx-template-editor-param-value" data-key="last-author-amp" style="position: relative;">yes</div></div></div></div><div class="cx-template-editor-source-container" lang="en" dir="ltr" style="display: none;"><div class="cx-template-editor-source"><div class="cx-template-editor-title" title="This template formats a citation to a book using the provided bibliographic information (such as author and title) as well as various formatting options.">Cite book</div><div class="cx-template-editor-param"><div class="cx-template-editor-param-title"><span id="title" class="cx-template-editor-param-key">Title</span></div><div class="cx-template-editor-param-value" data-key="title" style="position: relative;">UNIX Systems Programming</div></div><div class="cx-template-editor-param"><div class="cx-template-editor-param-title"><span id="last" class="cx-template-editor-param-key">Last name</span></div><div class="cx-template-editor-param-value" data-key="last" style="position: relative;">Kay A. Robbins</div></div><div class="cx-template-editor-param"><div class="cx-template-editor-param-title"><span id="last2" class="cx-template-editor-param-key">Last name 2</span></div><div class="cx-template-editor-param-value" data-key="last2" style="position: relative;">Steven Robbins</div></div><div class="cx-template-editor-param"><div class="cx-template-editor-param-title"><span id="publisher" class="cx-template-editor-param-key">Publisher</span></div><div class="cx-template-editor-param-value" data-key="publisher" style="position: relative;">Prentice-Hall</div></div><div class="cx-template-editor-param"><div class="cx-template-editor-param-title"><span id="year" class="cx-template-editor-param-key">Year of publication</span></div><div class="cx-template-editor-param-value" data-key="year" style="position: relative;">2003</div></div><div class="cx-template-editor-param"><div class="cx-template-editor-param-title"><span id="isbn" class="cx-template-editor-param-key">ISBN</span></div><div class="cx-template-editor-param-value" data-key="isbn" style="position: relative;">978-0-13-042411-2</div></div><div class="cx-template-editor-param"><div class="cx-template-editor-param-title"><span id="last-author-amp" class="cx-template-editor-param-key">Last author ampersand</span></div><div class="cx-template-editor-param-value" data-key="last-author-amp" style="position: relative;">yes</div></div></div></div>
 
== Collegamenti esterni ==
 
* [http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/pthread.h.html The Open Group Base Specifications Issue 7, IEEE Std 1003.1]