Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JMutex.cc File Reference
#include "JLang/JMutex.hh"
#include <iostream>
#include <stdexcept>
#include <cstdlib>
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

void withdraw (int &account, int amount)
 
void deposit (int &account, int amount)
 
int main (int argc, char **argv)
 

Function Documentation

◆ withdraw()

void withdraw ( int & account,
int amount )

Definition at line 16 of file JMutex.cc.

17{
18 if (amount < 0)
19 throw std::runtime_error("Wrong amount!");
20
21 if (account < amount)
22 throw std::runtime_error("Your account can't provide the requested amount!");
23
24 account -= amount;
25}

◆ deposit()

void deposit ( int & account,
int amount )

Definition at line 27 of file JMutex.cc.

28{
29 if (amount < 0)
30 throw std::runtime_error("Wrong amount!");
31
32 account += amount;
33}

◆ main()

int main ( int argc,
char ** argv )

Definition at line 35 of file JMutex.cc.

36{
37 using namespace std;
38
39 int debug;
40
41 try {
42
43 JParser<> zap("Example program to test mutex messaging.");
44
45 zap['d'] = make_field(debug) = 0;
46
47 zap(argc, argv);
48 }
49 catch(const exception &error) {
50 FATAL(error.what() << endl);
51 }
52
53 JMutex mutex;
54 int credit_card = 0;
55
56 cout << "Trying to deposit 1000 eur\n";
57
58 {
59 JMutex::JScopedLock lock(mutex);
60 deposit(credit_card, 1000);
61 }
62
63 cout << "Trying to put a negative amount\n";
64
65 try {
66 JMutex::JScopedLock lock(mutex);
67 deposit(credit_card, -200);
68 } catch (runtime_error const& e) {
69 cout << e.what() << '\n';
70 }
71
72 cout << "Let the wife do shopping\n";
73
74 try {
75 while (credit_card) {
76 JMutex::JScopedLock lock(mutex);
77
78 int const cost = rand() % 100;
79 cout << "Paying " << cost << "for something\n";
80 withdraw(credit_card, cost);
81 }
82 } catch (runtime_error const& e) {
83 cout << e.what() << '\n';
84 }
85}
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
void withdraw(int &account, int amount)
Definition JMutex.cc:16
void deposit(int &account, int amount)
Definition JMutex.cc:27
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
Utility class to parse command line options.
Definition JParser.hh:1698