Unsolved What will happen if a function was called in other threads
-
Hi
I want to know somthing as the title
e.g.int main { type *function = type::getinstance(); //is a singleton QThread thread = new qthread(this); function->moveToThread(thread); thread->start(); ... auto something = function->get_something(); //will return something }
singleton:
template <class T> class CSingleton: public QObject { class CSingletonGarbo { public: ~CSingletonGarbo() { if(m_instance) { delete m_instance; m_instance = NULL; } } }; protected: CSingleton(){} private: CSingleton(const CSingleton&){} CSingleton& operator=(const CSingleton&){} static T* m_instance; static QMutex m_Mutex; public: static T* GetInstance(); }; template<class T> QMutex CSingleton<T>::m_Mutex; template<class T> T* CSingleton<T>::m_instance = NULL; template<class T> T* CSingleton<T>::GetInstance() { static CSingletonGarbo garbo; if(NULL == m_instance) { QMutexLocker locker(&m_Mutex); if(NULL == m_instance) { T* ptmp = new T(); m_instance = ptmp; } } return m_instance; }
It seems to work but I'm not sure that there is memory leak or not.
Could you answer that and give me some suggestions?Regards
Mihan -
Not sure if the QThread you created is shutting down. If it is terminated, connect the deletion of the function using moveToThread when the thread is terminated with connect as below.
QObject :: connect (& thread, & QThread :: finished, function, & QObject :: deleteLater);Please refer to the following documents.
https://doc.qt.io/qt-5/qthread.html -
Hi
Just as a note.
Calling a QObject instance for "function" is a sure way to confuse others reading the code. :)
Also,
if you put the singleton in a thread, why do you need a singleton at all ?Could just be a normal QObject worker as normally since they cant be copied anyway and im not sure
what benefit you could possibly get from it also being a singleton. ? -
-
@Mihan said in What will happen if a function was called in other threads:
function->get_something()
What does this function do? Since you call it from the main thread, it's executed in the main thread so your thread is superfluous.
-
Thanks @Christian-Ehrlicher
Is onlyfunction->get_something()
in the main thread? Would other functions in this instance run in the thread that the instance moved to?get_something()
is an interface to get something like data. But the data will be made in this instance. -
@Mihan said in What will happen if a function was called in other threads:
Would other functions in this instance run in the thread that the instance moved to?
Why should they? Please take a look at https://doc.qt.io/qt-5/qthread.html#details and the 'see also' section there to start learning on how to use threads (and QThreads)
-
@Christian-Ehrlicher
Should I useQSharedData
to achieve it?
One thread makes data and write it intoQSharedData
, the other thread can read data fromQSharedData
-
It's not about sharing data here, it's about when is a function executed in which thread. When you want to execute a function in another thread then this object must live in the other thread (which is already the case in your example) and then trigger the execution e.g. with Signals/Slots to change the thread.