-2

I'm approaching to the multithreads world. I've a question about QString and multithreads application.

My main application class is:

MyGeneralClass{
    public:
    std::vector< QString > hpg_lines;
    .
    .
    .

};

My class for multithread looks like this:

class MyTread:public QThread{
    private:
    QString qstr_;
    MyGeneralClass *gen_;
    .
    .
};

Each thread write something in its qstr object (a big amout of strings) and at the end of the thread will copy it on the vector hpg_lines of the main class. Can I do this or I can occur in some data lost? thank you for your help

2
  • Not sure if you are falling into that trap, but note that QThread is thread controller. It is not the thread. So it would probably be best, if you put any variables modified by the thread, and any code you want to run in the thread into a separate class. Also note, that if you want a Qt event loop running the thread (so you can connect signal's to slots in the thread), you generalliy should not subclass QThread, because there is no reason to (and many unnecessary ways you can screw things up if you do it).
    – hyde
    Sep 6, 2016 at 16:41

3 Answers 3

0

In my knowledge std::vector is not thread safe (for reading and writing). Since you need concurent access to shared data structure the most naive and easy solution whould be http://doc.qt.io/qt-5/qmutex.html . Declare a new mutex and then use:

    your_mutex.lock();
    do your stuff on the data structure;
    your_mutex.unlock();

Make sure though that your_mutex is also the same resource between the threads and not each thread with one mutex. That means that mutex should probably be glboal variable or be passed to each thread.

3
  • 3
    From a software engineering point of view it makes more sense to create your own threadsafe_vector that is basically just a wrapper around a vector which creates a std::lock_guard first to lock a std::mutex (your version breaks if an exception is thrown).
    – nwp
    Sep 6, 2016 at 13:34
  • 1
    Thank you very much. I add to MyGeneralClass a private object QMutex accesible from all the threads by the pointer *gen_. Each time I operate on the vector I add lock and unlock mutex functions. Now my code works as I expected! Sep 6, 2016 at 14:04
  • @Mike vector has a lot of member functions, redeclaring them all is not really feasible. I would just add the functions when I need them and usually end up with only 5 or 10. Alternatively you can expose the vector and expect the caller not to keep a reference past the lock, but that can go wrong.
    – nwp
    Sep 6, 2016 at 14:10
0

The safest way would be that the result would be passed to the controlling thread and that thread can copy the result to the vector. This way the vector is not accessed from multiple threads. In Qt, this can be done by a queued connection.

0

Please use mutex.lock() and mutex.unlock() functions when you are going to write string to qstr and add to to vector hpg_lines; (mutex is an object of Qmutex class)

for example

mutex.lock()
    write string to qstr;
    add qstr to hpg_lines;
mutex.unlock();

This way the other thread will not be allowed to modify qstr or hpg_lines;

Thanks Sajeesh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.