, . , , . () , (control flow), . (thread) , , -.
. 10.1. .
, , , , , . , . . , . , -, .
:
( ). (lightweight processes), . , , UNIX, shell ( ls , . , ( ) (heavyweight).
. .
. , , , . , , , , .
. , . , , , , .
, , -, 1970- . "-1", 1979 , , . , "" . , "" !
|
|
, -, 1980- . UNIX . UNIX AT&T, , , Solaris. POSIX, , UNIX.
, 1990- . Windows NT, .
API . , , , , , .
-, C++, , . C++ - .
Java Java -, 1995 . Java . , Java Thread, , .
. NET, 2000 ., , Java.
, . Java . NET, . , , , , .
. , , . . , (user threads) . , , :
POSIX Pthreads , POSIX POSIX - ( );
Mac C-threads MacOS;
Solaris threads Solaris ( ).
|
|
, , (kernel threads). . , . , :
Windows 95 /98/NT/2000/XP/2003/2008/7;
Solaris;
Tru64 UNIX;
BeOS;
Linux.
. ( ) :
- / (many-to-one) . , (, ).
. 10.2. " / ".
- / (one-to-one) - . , , - Windows 95 /98/NT/2000/XP/2003/2008/7; OS/2.
. 10.3. " / ".
- / (many-to-many) , . . , , Solaris, Windows NT / 2000 / XP / 2003 / 2008 / 7 ThreadFiber.
. 10.4. " / ".
, , , , . .
fork() exec(). , UNIX fork "" , "", . , , , .
. : , , ? , , ? .
. UNIX . : SIGSEGV - ( , ); SIGKILL kill . - signal. : ? : , , . (, Windows 2000 Windows), - , (exception). , , , . Java . NET.
. , , , , , , . , , Java ( ThreadGroup). (ThreadPool) . NET.
|
|
(thread-local storage - TLS) , . , , . .
. , , . .
(deadlocks) . , (.. deadlock), . .
POSIX (Pthreads)
POSIX (, Portable Operating Systems Interface of uniX kind UNIX). POSIX IEEE 1003.1c, API . , POSIX - API . POSIX - . POSIX - UNIX, , , , , Solaris Windows NT.
POSIX : pthread_t ; pthread_attr_t .
POSIX :
pthread_create():
pthread_exit(): ( )
pthread_cancel():
pthread_join(): ,
pthread_detach(): ( , )
pthread_attr_init():
pthread_attr_setdetachstate(): , ,
pthread_attr_destroy(): ( ).
POSIX - (mutexes) (conditional variables) :
- pthread_mutex_init() ;
- pthread_mutex_destroy() ;
- pthread_mutex_lock() ;
- pthread_mutex_trylock() ( , , );
- pthread_mutex_unlock() ;
- pthread_cond_init() ;
- pthread_cond_signal() ;
- pthread_cond_wait() .
|
|
POSIX - .
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
static void wait_thread(void)
{
time_t start_time = time(NULL);
while (time(NULL) == start_time)
{
// , 1 .
}
}
static void *thread_func(void *vptr_args)
{ int i;
for (i = 0; i < 20; i++) {
fputs(" b\n", stderr);
wait_thread();
}
return NULL;
}
int main(void)
{ int i;
pthread_t thread;
if (pthread_create(&thread, NULL, thread_func, NULL)!= 0) {
return EXIT_FAILURE;
}
for (i = 0; i < 20; i++) {
puts("a");
wait_thread();
}
if (pthread_join(thread, NULL)!= 0) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
, "a", , (stderr) "b". (pthread_create), ( thread_func) (pthread_join).