My program has two threads:

  • Thread A scans the filesystem for files matching certain criteria, and adds them to an internal catalog.
  • Thread B scans the internal catalog for entries matching certain other criteria, and deletes them from both the catalog and the filesystem.

I've got locking that should ensure the program does the correct thing if both threads decide to operate on the same item at the same time, rather than, for example, creating a "ghost" catalog entry that refers to a deleted file. How can I verify this in an automated way?

The "correct thing" here is either "thread A notices the file, notices it's in the catalog, and skips it, then thread B notices the catalog entry, deletes the file and catalog entry, and continues" or "thread A notices the file, then thread B notices the catalog entry and deletes the file and catalog entry, then thread A notices the file has been deleted and skips it". Ideally a test would verify that both correct interleavings of operations are possible, and that no incorrect interleavings are possible.

  • 1
    What programming language are you using? Different languages provide different APIs to check and modify the state of a thread. In some cases you can also be notified when the thread reach a specific state (e.g. it's waiting for IO or for a lock). So, depending on the language, the strategy can change. – Davide Visentin Nov 11 '20 at 10:29
  • @DavideVisentin, I'm using wxWidgets, which is more or less a wrapper around pthreads (so, somewhere between C and C++ for the language). – Mark Nov 11 '20 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.