Блог пользователя BitwiseXOR

Автор BitwiseXOR, история, 3 месяца назад, По-английски

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?

Полный текст и комментарии »

  • Проголосовать: нравится
  • -11
  • Проголосовать: не нравится