S
S
Sazoks2020-08-26 10:53:27
Qt
Sazoks, 2020-08-26 10:53:27

Is it correct to call the same function from different threads?

There are objects of class A, each of which stores a reference to 1 object of class B.
I put all these objects in threads using moveToThread ().
Each object of class A, in the course of its work, calls a method from an object of class B.
Tell me, is this a normal practice, or can there be some kind of error in the case when 2 objects A simultaneously call a method from B?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-08-26
@Sazoks

Is this a normal practice, or can there be some kind of error in the case when 2 objects A simultaneously call a method from B?

Depends on that as it is arranged and where this method addresses. If it directly or indirectly accesses mutable global data without synchronizing or using atomic access, it may be a bug.
For example, accessing any of the global/class_static/function_static variables in the following code is a potential error:
int global;

class B
{
    static int class_static;
public:
    void f()
    {
        static int function_static;
    }
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question