POSIX Threads: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
AttoBot (discussione | contributi)
m Bot: Formatto ISBN; modifiche estetiche
Funzionalità collegamenti suggeriti: 3 collegamenti inseriti.
 
(12 versioni intermedie di 12 utenti non mostrate)
Riga 1:
{{NN|informatica|ottobre 2018}}
I '''thread 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 ==
pthreads definisce un insieme di tipi, funzioni e costanti in linguaggio C. È implementato con un'intestazione  <code>pthread.h</code>  e una [[Libreria (software)|libreria]] di thread.
 
Esistono circa 100 procedure di thread, tutte con prefisso  <code>pthread_</code>  e possono essere classificate in quattro gruppi:
 
* Gestione dei thread: creazione, unione, ecc.
* mutex
* Variabili di condizione 
* Sincronizzazione tra thread utilizzando blocchi di lettura/scrittura e barriere
 
L'API POSIX dei [[Semaforo (informatica)|semafori]] funziona con i thread POSIX ma non fa parte dei thread standard, essendo stata definita nello standard ''POSIX.1b - estensioni in tempo reale (IEEE Std 1003.1b-1993)''. Di conseguenza, le procedure del semaforo sono precedute da  <code>sem_</code>  invece di  <code>pthread_</code>. 
 
== 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 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 64 ⟶ 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 70 ⟶ 69:
Windows non supporta lo standard '''pthreads '''in modo nativo, pertanto il progetto Pthreads-w32 cerca di fornire un'implementazione [[wrapper]] portatile e open source. Può anche essere usato per portare il software Unix (che utilizza '''pthreads''') con poca o nessuna modifica alla piattaforma Windows. Con alcune patch aggiuntive l'ultima versione 2.8.0 è compatibile con i sistemi Windows a 64 bit. Si dice che anche la versione 2.9.0 sia compatibile a 64 bit.
 
Il progetto mingw-w64 contiene anche un'implementazione di wrapper di '''pthreads '''chiamata  '''winpthreads''', che tenta di utilizzare più chiamate di sistema native rispetto al progetto Pthreads-w32.
 
Il sottosistema di ambiente [[Interix]] disponibile in [[Windows Services for UNIX/Subsystem for UNIX-based Application]] fornisce una porta nativa dell'API '''pthreads''', ossia non mappata sull'API Win32/Win64 ma costruita direttamente sull'interfaccia [[Chiamata di sistema|syscall]] del sistema operativo.
 
== Note ==
<references />
{{reflist|2}}
 
== Bibliografia ==
 
* David R. Butenhof (1997). Programming with POSIX Threads. Addison-Wesley. ISBN 978-0-201-63392-4. 
* Bradford Nichols; Dick Buttlar; Jacqueline Proulx Farell (September 1996). Pthreads Programming. O'Reilly & Associates. ISBN 978-1-56592-115-3. 
* Charles J. Northrup (1996-01-25). Programming with UNIX Threads. John Wiley & Sons. ISBN 978-0-471-13751-1. 
* Kay A. Robbins & Steven Robbins (2003). UNIX Systems Programming. Prentice-Hall. ISBN 978-0-13-042411-2.
 
Riga 99 ⟶ 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><span data-key="title" title="The title of the book; displays in italics" class="cx-template-editor-param-desc"></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><span data-key="last" title="The surname of the author; don't wikilink, use 'authorlink'; can suffix with a numeral to add additional authors" class="cx-template-editor-param-desc"></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><span data-key="last2" title="The surname of the second author; don't wikilink, use 'authorlink2'; can suffix with a numeral to add additional authors" class="cx-template-editor-param-desc"></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><span data-key="publisher" title="Name of the publisher; displays after title" class="cx-template-editor-param-desc"></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><span data-key="year" title="Year of the source being referenced; use 'date' instead, if month and day are also known" class="cx-template-editor-param-desc"></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><span data-key="isbn" title="International Standard Book Number; use the 13-digit ISBN where possible" class="cx-template-editor-param-desc"></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><span data-key="last-author-amp" title="When set to any value, changes the separator between the last two names of the author list to 'space ampersand space'" class="cx-template-editor-param-desc"></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><span data-key="title" title="The title of the book; displays in italics" class="cx-template-editor-param-desc"></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><span data-key="last" title="The surname of the author; don't wikilink, use 'authorlink'; can suffix with a numeral to add additional authors" class="cx-template-editor-param-desc"></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><span data-key="last2" title="The surname of the second author; don't wikilink, use 'authorlink2'; can suffix with a numeral to add additional authors" class="cx-template-editor-param-desc"></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><span data-key="publisher" title="Name of the publisher; displays after title" class="cx-template-editor-param-desc"></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><span data-key="year" title="Year of the source being referenced; use 'date' instead, if month and day are also known" class="cx-template-editor-param-desc"></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><span data-key="isbn" title="International Standard Book Number; use the 13-digit ISBN where possible" class="cx-template-editor-param-desc"></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><span data-key="last-author-amp" title="When set to any value, changes the separator between the last two names of the author list to 'space ampersand space'" class="cx-template-editor-param-desc"></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]