Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
thread_semaphore.h
Package: threads-2.0.tar.gz [view]
Upload User: shtangtang
Upload Date: 2007-01-04
Package Size: 167k
Code Size: 2k
Category:
Linux-Unix program
Development Platform:
Unix_Linux
- #ifndef __THREADS_SEMAPHORE_H
- #define __THREADS_SEMAPHORE_H
- #include <thread_attributes.h>
- #include <thread_spinlock.h>
- class wait_queue;
- /**
- * A semaphore, it functions in much a similar way as a mutex
- * does, but with some exceptions.
- *
- * @author Orn E. Hansen <oe.hansen@gamma.telenordia.se>
- * @short Implements a semaphore, for thread synchronisation.
- */
- class semaphore {
- protected:
- static char *s_project;
- int s_id;
- wait_queue *s_waiting;
- attributes::scope s_scope;
- struct storage {
- int s_magic;
- spinlock s_sync;
- int s_count;
- } *_s;
- public:
- semaphore(attributes::scope);
- semaphore(attributes::scope, int);
- semaphore();
- semaphore(int);
- ~semaphore();
- /**
- * Increment the semaphore count, the top most blocking thread on
- * the waiting list is restarted.
- *
- * @return The updated value, of the semaphore count.
- */
- int post();
- /**
- * Block the calling process, until the semaphore count becomes
- * greater than 1, then atomically decrement it.
- *
- * @return The current count of the semaphore.
- */
- int wait();
- /**
- * Decrement the count, if it is greater than zero. This is a
- * non-blocking call.
- *
- * @return The count of the semaphore after decrement, or -1 not.
- */
- int trywait();
- /**
- * The shared memory scheme, wants a single name to identify the
- * overall program. It is possible, and perhaps desired that
- * part cond/mutex/semaphore have their own name identification
- * tree. This is possible, by stating that it should be branch
- * of the main file name, with a give extension.
- *
- * <pre>
- * main()
- * {
- * semaphore *sv;
- *
- * pthread::set_project( "my_project" );
- * semaphore::project_par( "sem" );
- * sv = new semaphore(attributes::process_shared);
- * ...
- * }
- * </pre>
- *
- * This will set the project to my_project, and all semaphores
- * will be derived from my_project_sem.
- *
- * @param part A C string containing the semaphore part name.
- */
- static void project_part(const char *);
- };
- #endif /* __THREADS_SEMAPHORE_H */