Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8742737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:33:04+00:00 2026-06-13T11:33:04+00:00

I have a class instance that is used by several other classes in other

  • 0

I have a class instance that is used by several other classes in other threads to communicate.

This class uses a slim reader/writer lock (WinAPI’s SRWLOCK) as a synchronization object and a couple of RAII helper classes to actually lock/unlock the thing:

static unsigned int readCounter = 0;

class CReadLock
{
public:
    CReadLock(SRWLOCK& Lock) : m_Lock(Lock) { InterlockedIncrement(&readCounter); AcquireSRWLockShared(&m_Lock);  }
    ~CReadLock() {ReleaseSRWLockShared(m_Lock); InterlockedDecrement(&readCounter);}

private:
    SRWLOCK& m_Lock;
};

class CWriteLock
{
public:
    CWriteLock(SRWLOCK& Lock) : m_Lock(Lock) { AcquireSRWLockExclusive(&m_Lock); }
    ~CWriteLock() { ReleaseSRWLockExclusive(&m_Lock); }

private:
    SRWLOCK& m_Lock;
};

The problem is the whole thing deadlocks all the time. When I pause the deadlocked program, I see:

  • one thread stuck in AcquireSRWLockExclusive();
  • two threads stuck in AcquireSRWLockShared();
  • readCounter global is set to 3.

The way I see it, the only way for this to happen is CReadLock instance’s destructor hasn’t been called somehow somewhere so the lock is perpetually stuck. However, the only way for this to happen (as far as I know) is because an exception has been thrown. It wasn’t. I checked.

What might be the problem? How should I go about fixing (or at least locating the reason of) this thing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:33:05+00:00Added an answer on June 13, 2026 at 11:33 am

    Are you using Read lock in recursive manner?

    void foo()
    {
        CReadLock rl(m_lock);
        ...
        bar();
    }
    void bar()
    {
        CReadLock rl(m_lock);
        ...
    }
    void baz()
    {
        CWritedLock rl(m_lock);
        ...
    }
    

    if foo() and baz() are called simultaneously you may get deadlock:

    1. (Thread A) foo locks
    2. (Thread B) baz asks to create write lock now all read locks would block until all are released - waits.
    3. (Thread A) bar tries to lock and waits because there is pending write lock
    

    The fact that you have 2 threads stuck on Read lock and Read Lock counter is 3 that most likely shows that you have a recursion in one of the locks – i.e. one thread had tried to acquired read lock twice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a static class that creates several worker threads in its constructor. If
I have a class called Sprite, and ballSprite is an instance of that class.
I have a factory class, DocumentLoaderFactory , which simply returns an instance that implements
I have a class that desccends from ListviewItem. When I create an instance of
Example: I have an class that inherits from UIImageView. An object creates an instance
I have an instance of Class A that I want to refer to in
I have a custom class that has as an instance variable of a coordinate:
I have a function that accepts a class (not an instance) and, depending on
I have an Activity class which has an attribute that references an AsyncTask instance
Assuming I have a instance of class A (name it AI) that has an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.