⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 semaphore.3

📁 多线程库
💻 3
字号:
.\" 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 3 "10 Feb 2000" "Threads 2.0" "Threads C++ Library".SH NAMEsemaphore \- A class, for process synchronisation..SH SYNOPSIS.B        #include <thread.h>.sp 2.B        .....sp 0.B        semaphore sem(attributes::process_private);,so 0.B        .....SH DESCRIPTIONThe.I semaphoreclass is a part of the threads C++ library that focuses onsimplifying program threading.The semaphore class is a complete class, that provides the primitivemethod of semaphore synchronisation of processes.  It can work ina shared, as well as local environment.Semaphores can be created using either.I attributes::process_sharedinitialisation or.I attributes::process_privateeach of which will give it a different working scope.  In a sharedenvironment, the scope of the semaphore, is open to all processeswho have permission and address it at equal resource identification.A semaphore, is a class that initially has a count of zero and whereother processes or threads, can wait until it enters a state of non zero.  Thus synchronizing the state of the waiting processwith the one posting the value to the semaphore.  A few methodsare provided to create this functionality..LPThese methods are listed here, with some explanation on how theyfunction..TP 12.B post()This will post incriment the count of the semaphore, by one.  If anyprocesses are waiting on the semaphore, when its state is updated, theywill be awakened..TP 12.B wait()Wait for the semaphore to reach a value of non-zero, and then decrimentits count by one.  Return the actual count of the semaphore, upon return..TP 12.B trywait()This will evaluate the state of the semaphore, and if it is non-zerothe semaphore will be decremented by one.  In each case, the processwill return immediately with the actual semaphore count.  A value of-1 means, that the semaphore is already at zero..LPIn a shared environment, it may be desired that semaphoreresources be addressed differently from other resources belongingto a process..TP 12.B project_part(const char *)Give all resources that are of type semaphore, a namethat identifies them seperately, yet still branched from themain process.  The passed arguement is a branch name, that willidentify all created semaphores.  This is a staticmethod, that will apply to all semaphores created afterthe call..LPMaybe the most useful case for a semaphore, is in the initalizationof a threaded class.One should note, that a threaded class will upon creation run itsinitalization method and the.I thread(void *)method at the same time.  This will mean, that upon start some variables that will be initialized in the constructor are notavailable at the beginning of the thread.  To make the threadactually wait for the constructor, a.I semaphoreis a good choice.  A simple example follows:.nf       #include <thread.h>       class mythread : public pthread {         private:            semaphore sem;            int variable;         public:           mythread() {             variable = <some value>;             sem.post();     // Last instruction in constructor         };         ~mythread() { };         int thread(void *) {           sem.wait();       // wait for constructor           // variables are now initialized         }       }.fi.LPIn the above example, the thread has secured that its local variablesare initialized before it continues its processing.  In many cases, thethread doesn't need any global variables, it may be using localvariables that it doesn't want to share.  In this case, the user maywant the thread to run off immediately, where the above methodis redunant.But in many cases, there are several variables that will be sharedbetween threads.  In these cases, synchronized access to thesevariables are a necessity for a successful operation..LPSuggestions and questions about the threads library should bedirected to.IPkdb-list@brevet.nu.LPIr, to the specified author 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 + -