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
diner.C
Package: threads-2.0.tar.gz [view]
Upload User: shtangtang
Upload Date: 2007-01-04
Package Size: 167k
Code Size: 1k
Category:
Linux-Unix program
Development Platform:
Unix_Linux
- //
- // This is a philosopher demo, using process scoping.
- #include <thread.h>
- #define MAX_PHILOSOPHERS 5
- int 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,%dn", _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: sleepingn", _nr);
- p_fork[lfork].unlock();
- p_fork[rfork].unlock();
- sleep(2);
- }
- cout << _nr << " has finished dining." << endl;
- sem->trywait();
- return 0;
- }