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 9085179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:10:58+00:00 2026-06-16T21:10:58+00:00

I have a simple data class that gets called from another class. Data Class:

  • 0

I have a simple data class that gets called from another class.

Data Class:

class Data
{
public:
    QString getName() const
    {
        return this->mName;
    }

    void setName(AccessData* access, const QString& name)
    {
        this->mName = name;
        access->emitNameChanged(this);
    }
private:
    QString mName;
    QReadWriteLock mLock;
};

And here’s the class I am using to get/set a new name that also handles the locking:

class AccessData : public QObject
{
public:
    QString getName(Data* data)
    {
        QReadLocker lock(&data->mLock);
        return data->getName();
    }

    void setName(Data* data, const QString& name)
    {
        QWriteLocker lock(&data->mLock);
        data->setName(this, name);
    }

    void emitNameChanged(Data* data)
    {
        emit this->nameChanged(data);
    }
signals:
    void nameChanged(AccessData* access, Data* data);
};

What happens is this:
I use the AccessData class to read and write the name of a Data instance. The AccessData class is responsible for locking for read/write. However, the Data class as you can see, in it’s setName() method calls back the AccessData instance to properly emit a signal about the change. NOTE: This is just pseudo code, in reality it is more complex that’s why the Data class needs to be able to emit signals through it’s caller.

And here’s the problem:

Say I have an instance of “Data” called “d”: Data* d;
I am now using an “AccessData” instance “a” to change the name: a->setName(d, “new name”);
At the same time, I am conncected to the nameChanged() signal with this code:

...
void nameChanged(AccessData* access, Data* data)
{
    // Read the new name
    QString newName = access->getName();
}

And here’s the issue:

  1. Calling a->setName(d, “new name”)
  2. “d” is now locked by “a” (Write lock)
  3. “d” emits a signal about the name change though still locked
  4. My method connected to the nameChanged signal tries to access getName()
  5. This will cause another QReadLock issued which simply results in a deadlock

What can I do to properly handle this? There’s two things that came up to me:

  1. Emit the signal delayed (aka non-blocking) to get it into the loop.
    This is NOT what I want because I want the signals to be pushed immediately.

  2. Move the lock/unlock stuff within the Data class and first unlock, then emit the signal.
    This is NOT what I want because I want to keep the Data class completely free from locking stuff.

Any idea? Do I have a miss conception?

thanks a lot
Alex

  • 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-16T21:10:59+00:00Added an answer on June 16, 2026 at 9:10 pm

    You need to make your mind about what the objects in your model represent. The philosophy of Data is suspicious. It owns the lock (has-a composition), but you don’t want it to be self-lockable. If Data is meant to be a simple data wrapper, then it shouldn’t own the lock. So either allow it to handle its own lock (and then you can unlock before emiting), or move the lock and the emitting too away from Data to AccessData.

    If for some reason you want to keep the presented design, you can “solve” this with initializing mLock as QReadWriteLock::Recursive. Then the same thread can lock it multiple times over – given that you still call an equivalent amount of unlock(). But my personal experience is that reentrant locking is a sure sign of runaway/misunderstood call flow and a creeping misconcept which will bite back hard. While i do read about theoretical concepts which supposedly cannot be solved without reentrant locks, i still have to see one practically unavoidable.

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

Sidebar

Related Questions

I have a simple class in a Django app called project from django.test import
Pretty simple question this time around. I have an application that communicates with another
Say you have a simple class with some storage data structure (list, vector, queue,
I have simple SSIS package which reads data from flat file and insert into
I have simple SSIS package where I import data from flat file into SQL
I have a simple jasper report which shows some data from database. The jasper
I have a class that outputs a simple report file. It reads some record
I am developing an Android App that gets some data from a web server,
I have a method that gets a nested array as parameter: Number[][] data where
I have a simple object that is deserialized from JSON into a server-side object.

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.