Getting Started with gen_server

I’m going to throw you in at the deep end. Here’s the simple three-point plan for writing a gen_server callback module:

  1. Decide on a callback module name.

  2. Write the interface functions.

  3. Write the six required callback functions in the callback module.

This is really easy. Don’t think—just follow the plan!

Step 1: Decide on the Callback Module Name

We’re going to make a simple payment system. We’ll call the module my_bank.

Step 2: Write the Interface Routines

We’ll define five interface routines, all in the module my_bank.

start()

Open the bank.

stop()

Close the bank.

new_account(Who)

Create a new account.

deposit(Who, Amount)

Put money in the bank.

withdraw(Who, Amount)

Take money out, if in credit.

Each of these ...

Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.