# ThreadsafeCounter

© 2012 John Abbott
GNU Free Documentation License, Version 1.2

- User documentation for ThreadsafeCounter
  - Constructors
  - Operations on ThreadsafeCounters
- Maintainer documentation
- Bugs, shortcomings and other ideas
- Main changes

CoCoALib Documentation Index

## User documentation for ThreadsafeCounter

A `ThreadsafeCounter` is simply a counter (based on a `long`) which may be incremented in a threadsafe manner.

### Constructors

There is only one constructor, the default constructor:

- `ThreadsafeCounter()` -- create new counter starting at zero.

### Operations on ThreadsafeCounters

There is only one operation:

- `TCS.myAdvance(n)` -- increment the counter by `n`, and return the value of the counter prior to incrementing.

Note that `myAdvance` is likely to be quite slow as it uses mutexes.

A `ThreadsafeCounter` may be printed; this is intended primarily to permit debugging.

## Maintainer documentation

I copied the code from a BOOST example. It's so simple there are obviously no deficiencies!

`operator<<` is probably not threadsafe (but does that matter?)

## Bugs, shortcomings and other ideas

No check for overflow!

You cannot query the counter's value without incrementing it.

## Main changes

2012

- 05-May (v0.9951): first version