Shows how to use lock_guard_ class.
#include <xtd/xtd>
namespace examples {
class account : public object {
public:
explicit account(int initial) : balance(initial) {}
void do_transactions() {
for (auto i = 0; i < 100; ++i)
withdraw(random.next(1, 100));
}
private:
int withdraw(int amount) {
if (balance < 0) throw system_exception {"Negative Balance"};
if (balance < amount) return 0;
console::write_line("Balance before Withdrawal : {0}", balance);
console::write_line("Amount to Withdraw : -{0}", amount);
balance = balance - amount;
console::write_line("Balance after Withdrawal : {0}", balance);
return amount;
}
return 0;
}
int balance = 0;
};
class program {
public:
static void main() {
auto account = examples::account {1000};
for (auto i = 0; i < 10; ++i)
thread_pool::queue_user_work_item({account, &account::do_transactions});
thread_pool::close();
}
};
}
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet c...
Definition random.hpp:44
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:168
#define lock_guard_(object)
The lock_guard_ keyword marks a statement block_guard as a critical section by obtaining the mutual-e...
Definition lock_guard.hpp:64