linux-sysprog · beginner · ~15 min

Forgotten joins

Catch the resource-leak mistake.

Challenge

Every joinable thread you forget to join leaks resources until the process exits. Count the forgotten ones.

Task

Implement int forgotten_joins(int created, int joined) that returns how many joinable threads were never joined.

Input

  • created: joinable threads created.
  • joined: how many were joined.

Output

created - joined, floored at 0.

Example

forgotten_joins(5, 3)   ->   2
forgotten_joins(4, 4)   ->   0

Edge cases

  • joined >= created: return 0.

Input format

created: joinable threads created; joined: joined count.

Output format

created - joined, floored at 0.

Constraints

Result is never negative.

Starter code

int forgotten_joins(int created, int joined) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.