BitwiseXOR's blog

By BitwiseXOR, history, 3 months ago, In English

I’ve been working on this problem for several days but still struggle.

The problem involves an N-ary tree (where each non-leaf node has exactly n children). The goal is to implement a Lock() operation with the following rules:

Lock(string X, int id):

If node X is already locked, you cannot lock it, so return false. If any of X's descendants are locked, you cannot lock it, so return false. If any of X's ancestors are locked, you cannot lock it, so return false. If none of the above conditions are met, lock node X with the given ID and return true.

You are required to handle M queries of the Lock() operation on a multi-core machine, allowing M threads to run the Lock() function concurrently. However, since the function involves multiple internal checks, locking two nodes simultaneously can result in race conditions.

How would you make the Lock() function thread-safe?

  • Vote: I like it
  • -11
  • Vote: I do not like it

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by BitwiseXOR (previous revision, new revision, compare).