RU/2: Форум. Общение пользователей и разработчиков OS/2 (eCS). : Ответить на сообщение
Имя:
e-mail:
FIDO:
Home page:
сохранить данные о вас
Тема:
> Example 2: A thread termination under C/2 with the standard function _endthread() > > (1.) Prototype definition: > > #include <stdlib.h> /* also defined in <process.h> */ > > void _endthread(void); > > In the header file stdlib.h there are the definitions for the function _endthread() > > > (2.) Language Level: Extension > > The _endthread function explicitly ends a thread anywhere where it is called in the function of the thread where you previously created it with _beginthread before the thread has completed its function. > > For example, if some condition is not met, _endthread terminates the thread. When the thread has finished, it automatically ends itself with an implicit call to _endthread. > In other words: A thread can terminate itself either by returning from the main thread > function or by using and _endthread() anywhere in the execution of the thread. > > Note: > > 1. When using the _beginthread and _endthread functions, you must specify the > /Gm+ compiler option to use the multithread libraries. > > 2. If you use DosCreateThread, you must explicitly call _endthread to > terminate the thread. > > There is no return value of the function _endthread(). > > > (3.)Programming example: > > > > /************************************************************************ > * > In this example, the main program creates two threads, bonjour > and au_revoir. The thread bonjour is forcibly terminated by a > call to _endthread, while the au_revoir thread ends itself with an > implicit call to _endthread. > > Note: To run this example, you must compile it using the /Gm+ > compile-time option. > > * > ************************************************************************/ > > > > > #define INCL_DOS > #include <os2.h> > #include <stdio.h> > #include <stdlib.h> > #include <process.h> > > void bonjour(void *arg) > { > int i = 0; > while ( i++ < 5 ) > printf("Bonjour!\n"); > > _endthread(); /* This thread ends itself explicitly */ > puts("thread should terminate before printing this"); > } > > void au_revoir(void *arg) > { > int i = 0; > while ( i++ < 5 ) /* This thread makes an implicit */ > printf("Au revoir!\n"); /* call to _endthread */ > } > > int main(void) > { > unsigned long tid1; > unsigned long tid2; > > tid1 = _beginthread(bonjour, NULL, 8192, NULL); > tid2 = _beginthread(au_revoir, NULL, 8192, NULL); > > if ( (tid1 == -1) || (tid2 == -1) ) > { > printf("Unable to start threads.\n"); > exit(-1); > } > > DosWaitThread( &tid2, DCWW_WAIT ); /* wait until threads 1 and 2 */ > DosWaitThread( &tid1, DCWW_WAIT ); /* have been completed */ > > return 0; > > /* Possible output could be: > > Bonjour! > Au revoir! > Bonjour! > Au revoir! > Bonjour! > Au revoir! > Bonjour! > Au revoir! > Bonjour! > Au revoir! */ > > } > > > Remarks: > > The return value of -1 from the function _beginthread means that the function ended > with an error. > > In this example there are two thread functions definded. It is not sure in which order > the printout will happen! This case of two or more threads and its order of execution > will be shown in a further lesson. > > A thread will also be terminated with a call of abort, return, DosExit, and a DosKillThread. > > For the definitions of _beginthread() and DosWaitThread() see this reference: http://os2.in.ru/forum/m019628.html > > > >
_, _, _, _, _ _ _,_
(_ | / \ |\ | | |_/
, ) | , \ / | \| | | \
~ ~~~ ~ ~ ~ ~ ~ ~
Programmed by
Dmitri Maximovich
,
Dmitry I. Platonoff
,
Eugen Kuleshov
.
25.09.99 (c) 1999,
RU/2
. All rights reserved.
Rewritten by
Dmitry Ban
. All rights ignored.