Thread: Accessing a linked list via multiple threads

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    47

    Accessing a linked list via multiple threads

    Hello,

    I have a conceptual question regarding the use of mutexes to access a shared resource, a linked list in this instance.

    Say I have two threads that can access a linked list.

    I understand that if I want to make changes to the linked list from either thread, that I need to use a mutex to have exclusive access while making changes to the list.

    So for example, I obtain a lock, then add or delete a record, then unlock.

    However I am thinking that if I am just reading the list, from either thread, that I don't need exclusive access and thus I don't need to obtain a lock.

    Just wanting to confirm if my thinking here is correct; or if not, get some idea on why not / considerations.

    Thanks

    VW

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    224
    It is common to have two-stage locknig...

    Idle - nobody is accessing the data.

    Shared - everybody cn access, but nobody can change.

    Exclusive - only one thread can access the structure.

    The key idea is that when somebody requests the exclusive lock no more shared locks are issued while the exclusive lock is pending.

    All of the shared locks drain away, and the exclusive lock is granted, the update made, and the the exclusive lock released allowing forward progress.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    775
    The locking concept you are looking for is called a readers-writer lock, which allows for either any number of readers or a single writer, but not both simultaneously. That's basically what hamster_nz described.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-07-2020, 07:10 AM
  2. Sorted Linked List with multiple heads.
    By S15_88 in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 11:01 PM
  3. Is this safe - linked list with multiple types
    By pronecracker in forum C Programming
    Replies: 18
    Last Post: 05-30-2007, 01:08 PM
  4. delete multiple numbers from linked list
    By ronenk in forum C Programming
    Replies: 17
    Last Post: 01-18-2005, 12:41 PM

Tags for this Thread