// Data stored for each account. struct account { int balance; }; // All accounts in the bank. The account number is the index in the array. int num_accounts; struct account *accounts; // Transfer 'amount' money from the account 'from' to the account 'to'. bool transfer(int amount, struct account *from, struct account *to) { bool ok = from->balance >= amount; if (ok) { from->balance -= amount; to->balance += amount; } return ok; } /** * The code below simulates different transfers. */ // Help-function to transfer based on account numbers. void transfer_id(int amount, int from, int to) { if (!transfer(amount, &accounts[from], &accounts[to])) printf("Transfer %d from %d to %d failed!\n", amount, from, to); } int main(void) { NO_STEP { num_accounts = 5; accounts = malloc(sizeof(struct account) * num_accounts); for (int i = 0; i < num_accounts; i++) { accounts[i].balance = 10; } } // Try different combinations of transactions here! thread_new(&transfer_id, 10, 1, 3); thread_new(&transfer_id, 10, 0, 2); transfer_id(10, 0, 2); return 0; }