📄 threads.7
字号:
.\" Copyright (c) 1999-2000 Orn E. Hansen <oe.hansen@gamma.telenordia.se>.\".\" Permission is granted to make and distribute verbatim copies of this.\" manual provided the copyright notice and this permission notice are.\" preserved on all copies..\".\" Permission is granted to copy and distribute modified versions of this.\" manual under the conditions for verbatim copying, provided that the.\" entire resulting derived work is distributed under the terms of a.\" permission notice identical to this one.\" .TH THREADS 7 "10 Feb 2000" "Threads 2.0" "Threads C++ Library".SH NAMEthreads \- C++ environment, for threaded applications..SH SYNOPSIS.B #include <thread.h>.sp 2.B class threaded : public pthread {.sp 0.B ....sp 0.B int thread(void *);.sp 0.B ....sp 0.B }.SH DESCRIPTION.I Threadsis a library of class, that make threaded programming simplerand easier to maintain, by providing an abstract class wherethe actual threaded application is a method.Several classes are provided with the library, each of whichhas a collection of methods, that provide its basic functions..TP 12.B pthreadThis class provides the abstraction for threaded applications. Wherethe thread is an abstract method.I int thread(void *)which can return a value of type integer, and accept a parameterof an unknown type, which is identified by.I void *and must be broken out by the users specification of the method..TP 12.B mutexA class for mutual exclusion of parallel processes. It providesmeans to lock and unlock the mutex, so that threads may synchronizetheir access to shared resources..TP 12.B condWhich provides means of signalling conditions between twothreads. Several threads can be waiting on a condition variableawaiting some occurrance, and the thread providng the signalcan decide wether it should awake one or all the threadswaiting on it..TP 12.B semaphoreA historical synchronisation method, which is also provided andhas a very special use. See.I semaphore(3)for a discussion on the use it can provide in a C++ threadedenvironment..LPBeyond merely providing these methods for a threaded environmentthe.I Threads version 2.0 Libraryalso provides for process scoping. A program can decide todeclare its resources as shareable and synchronise itself withanother program that may or may not be started at the sametime. The library package, comes with a rich set of exampleprograms that demonstrate its use, one of which is a programthat demonstrates the dining philosopher problem, with theuse of process scoping..nf//// This is a philosopher demo, using process scoping.#include <thread.h>#define MAX_PHILOSOPHERS 5int main(){ mutex *p_fork; semaphore *sem; bool fork_v = false; int lfork, rfork, _nr, _forks = MAX_PHILOSOPHERS, _count; pthread::set_project( "/tmp/philosophy" ); sem = new semaphore(attributes::process_shared); p_fork = new mutex[MAX_PHILOSOPHERS](attributes::process_shared); if ( (_nr = sem->post()) > MAX_PHILOSOPHERS ) { sem->trywait(); exit(0); } _count = _forks; _nr -= 1; lfork = _nr-1 >= 0 ? _nr-1 : _forks-1; rfork = lfork+1 >= _forks ? 0 : lfork+1; while(_count-- > 0) { cout.form("Philosopher %d: looking for %d,%d\n", _nr, lfork, rfork); while( fork_v == false ) { if (p_fork[lfork].trylock() == 0) { if (p_fork[rfork].trylock() == 0) fork_v = true; else p_fork[lfork].unlock(); } } cout.form("Philosopher %d: using (%d,%d).\n", _nr, lfork, rfork); sleep(2); fork_v = false; cout.form("Philosopher %d: sleeping\n", _nr); p_fork[lfork].unlock(); p_fork[rfork].unlock(); sleep(2); } cout << _nr << " has finished dining." << endl; sem->trywait(); return 0;}.fi.LPThis program starts, by creating a project name for its resources. Butthis is a file name, that will be used by the linux kernel to calculatea unique key to identify its shared pages with. Since the key iscalculated from inode numbers, it requires file names to identify them by. The command that does this is....nf pthread::set_project( "/tmp/philosophy" );.fiSuch a scheme is perfectly acceptable, in most cases, and makes itpossible for other programs to share its resources without havingthe same program filename.After having declared the shared resource name, it starts allocatingits resources. In the declaration of its resources, it passesa special attribute name.I attributeees::process_sharedto their constructor to indicate that the specified resourceshould be shared....nf sem = new semaphore(attributes::process_shared); p_fork = new mutex[MAX_PHILOSOPHERS](attributes::process_shared);.fiFrom this point beyond, these resources are sharable and startingseveral incidents of the same program ( up to 5 in this case )will make a full example of the famous dining philosopher problem.However, as in the above example it is required for other programsthat wish to share its resources to identify the resources itwants to share. In this case, it may be unwanted for a programto actually share the mutexes and only watch how many philosopherswere actually dining. But, if the program had actually declaredits mutexes prior to the semaphore, another program would actuallyhave to declare the mutexes as well before it actuall got to thememory where the semaphore was located..I A program can decide the branch nameto signify that a given set of resources, can be identifiedwith a different identity than other resources.So, if the program above would have done.nf p_fork = new mutex[MAX_PHILOSOPHERS](attributes::process_shared); semaphore::project_part( "semaphore" ); sem = new semaphore(attributes::process_shared);.fiIt would mean, that the semaphore would be identified by afilename.I /tmp/philosopher_semaphorewhich is easily identifiable, and separate from otherresources in the program..I The same can be used for cond and mutex classesto identify these as seperate branches from the mainprogram. Making each resource a separate identity.The user can even share his or her own pages of memory, if sodesired. The.I pthreadclass makes it possible for a user, to allocate their ownshared memory locations. For this purpose, there are twomethods provided.IP.I void *pthread::shalloc(size_t).LPwhich allocates shared memory of the specified size, and theopposite method.IP.I pthread::shdealloc(void *).LPwhich returns the memory, allocated by the above method,back to a pool of free shared memory.Shared memory pages, that aren't used are.I alwaysreturned immediately to the system, and destroyed when theyaren't used. And the library further installs several signal handlersto make sure that signals that normally terminate the usersapplication will also destroy the shared memory pages.Note: If the user decides to override these signal handlers,he is urged to make sure that the.B exit()system call is used to exit the application. This ispreferrable since otherwise shared memory will remain in thesystem's swap pages and may cause interference next time anapplication is run..LPSuggestions and questions about the threads library should bedirected to.IPkdb-list@brevet.nu.LPOr, to the author specified below. The threads home page is located at:.IPhttp://user.tninet.se/~dpn659b/threads.html.LP.SH AUTHORVersion 2.0Copyright (C) 1999-2000 Orn E. Hansen <oe.hansen@gamma.telenordia.se>..LPThanks to those who reported their suggestions on how toimprove the threads library.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -