E
E
Evgeny Yakushov2020-10-16 13:15:06
Qt
Evgeny Yakushov, 2020-10-16 13:15:06

Why is a variable sometimes taken incorrectly in a thread?

The bottom line is, there is some simulation of the model in which flows are created to simulate the flight of an object

void Simulator::Start(ModuleRls &RLS)
{
    for (uint i = 0; i < flyObjVector.size(); i++)
    {
        StreamFly *obj = new StreamFly(flyObjVector, RLS, i + 1);
        obj->start();
    }
}


The stream itself:

void StreamFly::run()
{
    for (int i = 0; i < 50; i++)
    {
        qDebug() << id << "[" << QThread::currentThreadId() << "]" << RLS->getX();
    }
}


Here I take the RLS coordinate to check the work, it is equal to 50 (let's say)
In the log, I see the following picture:
1 [ 0x1a88 ] 50
1 [ 0x1a88 ] 50
1 [ 0x1a88 ] 50
1 [ 0x1a88 ] 50
1 [ 0x1a88 ] 50
1 [ 0x1a88 ] 50
1 [ 0x1a88 ] 50
1 [ 0x1a88 ] 50
1 [ 0x1a88 ] 50
1 [ 0x1a88 ] 50
1 [ 0x1a88 ] 50
1 [ 0x1a88 ] 50
6 [ 0x1cf8 ] 2675932
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
6 [ 0x1cf8 ] 49351064
2 [ 0x1c78 ] 50
6 [ 0x1cf8 ] 6900
2 [ 0x1c78 ] 6900
6 [ 0x1cf8 ] 6900
2 [ 0x1c78 ] 6900
2 [ 0x1c78 ] 6900
6 [ 0x1cf8 ] 6900
2 [ 0x1c78 ] 6900
6 [ 0x1cf8 ] 6900
7 [ 0x1d00 ] 6900
4 [ 0x1de8 ] 6900
6 [ 0x1cf8 ] 6900
4 [ 0x1de8 ] 6900
6 [ 0x1cf8 ] 6900
2 [ 0x1c78 ] 6900
4 [ 0x1de8 ] 6900
6 [ 0x1cf8 ] 6900
7 [ 0x1d00 ] 6900
4 [ 0x1de8 ] 6900
8 [ 0x1d0c ] 6900
6 [ 0x1cf8 ] 6900


Some threads get the parameter correctly, but sometimes even in one thread the coordinate can be naughty.
Let's say stream number 1 and in it half is correctly displayed and half is not correct.
I don't understand what the problem is. I started working with threads on QT recently, don't judge strictly :)
And one more question, for some reason, after the end of the simulation, it eats up memory by 1-2 mb, depending on how many objects are running, let's say 1800. Yes, it sounds crazy from the code, I generate 1800 threads... But that's not the point. Why after each launch, these 1-2 mb are added, the vector of objects is transferred everywhere by reference.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ighor July, 2020-10-16
@IGHOR

without the getX() code, nothing is clear
This function is probably not thread safe

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question