Results 1 to 12 of 12

Thread: Mulitple threads

  1. #1
    Join Date
    Oct 2009
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Mulitple threads

    HI
    I am using QT4.01 on windows.

    Our application has to fetch data from files and push into database.
    I have written a class subclassing QThread, it feteches and pushes into database.
    In GUI i am providing option to select multiple files.
    Qt Code:
    1. for(nIndex =0 ; nIndex <strBinFileList.count(); ++nIndex)
    2. {
    3.  
    4. MyThread *ptr1 = new MyThread();
    5. connect(ptr1, SIGNAL(finished()), ptr1, SLOT(deleteLater()));
    6. ptr1->setName(strBinFileList[nIndex].toStdString().c_str());
    7. ptr1->start();
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 
    For single file it is working fine.
    When i am selecting two files it is crashing.
    i have tried with only two files with out using for loop.

    Qt Code:
    1. MyThread *ptr1 = new MyThread();
    2. connect(ptr1, SIGNAL(finished()), ptr1, SLOT(deleteLater()));
    3. ptr1->setName(strBinFileList[0].toStdString().c_str());
    4. ptr1->start();
    5.  
    6. MyThread *ptr1 = new MyThread();
    7. connect(ptr1, SIGNAL(finished()), ptr1, SLOT(deleteLater()));
    8. ptr1->setName(strBinFileList[1].toStdString().c_str());
    9. ptr1->start();
    To copy to clipboard, switch view to plain text mode 

    Even then also it is crashing.

    In backend two databases are created around 30% of data is inserted in to db.
    Each files executes independent of other. There is no dependency on input files and output databases.

    Thanks in Advance....

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mulitple threads

    may be in race condition of the two objects ... try using QMutexand QWaitCondition ... for the common global variables but i think this is not the problem any how u try it also try give different names for the thread objects .. not same name` ....
    "Behind every great fortune lies a crime" - Balzac

  3. #3
    Join Date
    Oct 2009
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mulitple threads

    Quote Originally Posted by wagmare View Post
    may be in race condition of the two objects ... try using QMutexand QWaitCondition ... for the common global variables but i think this is not the problem any how u try it also try give different names for the thread objects .. not same name` ....
    There is no common variales between two databases.
    For single file it is taking around 30 seconds. i am trying for mulitple files simultaneously to improve the performance.

  4. #4
    Join Date
    Oct 2009
    Posts
    37
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Mulitple threads

    Do you have a backtrace of the crash?
    Disclaimer: Although I work on Qt for Nokia, anything I post here is personal

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mulitple threads

    Do you have a separate database connection for each of the threads? And I don't mean variables but actual connection with different names.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Oct 2009
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mulitple threads

    Quote Originally Posted by wysota View Post
    Do you have a separate database connection for each of the threads? And I don't mean variables but actual connection with different names.
    I dont have back trace of crash...
    how to do backtrace crash ??

    All the file names are different.
    with different connections ...

    It is behaving indeterministic...
    some times it is crashing in the begining of the data...

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mulitple threads

    I don't mean the file names. I mean the database connections. Could you show us the code where you connect to the database in all places? Remember to obfuscate the password.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Oct 2009
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Mulitple threads

    Quote Originally Posted by wysota View Post
    I don't mean the file names. I mean the database connections. Could you show us the code where you connect to the database in all places? Remember to obfuscate the password.
    Here i am using Sqlite database as plain files. http://www.sqlite.org/download.html.
    Its project constraint.
    I am also using wrapper SQLiteWrapper.

    Its working fine with single thread.

    My data pusing class is having wrapers on it.so i cant post all the class code.
    In pseudo code i am posting.

    Qt Code:
    1. class Base
    2. {
    3. virtual InsertData();
    4. }
    5. class Derived :class Base
    6. {
    7. InsertData();
    8. char szFileName[256];
    9. SqliteWrapper database;
    10.  
    11. }
    12.  
    13. class Managaer
    14. {
    15. Base *pt;
    16. InsertData(int format);
    17.  
    18. }
    19. Managaer::InsertData(int format)
    20. {
    21. switch(format)
    22. {
    23. Base:
    24. pt= new Derived();
    25. pt->SetFileName();
    26. pt->InsertionData();
    27. }
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 



    The code in thread run method is



    Qt Code:
    1. class MyThread : public QThread {
    2. Q_OBJECT
    3.  
    4. private:
    5. short nNumber;
    6. char szName[512];
    7. public:
    8. MyThread();
    9. void setNumber(short x);
    10. void setName(const char*);
    11. void run();
    12. };
    To copy to clipboard, switch view to plain text mode 


    Code in Thread.cpp

    Qt Code:
    1. void MyThread::run()
    2. {
    3. std::bitset<TOTAL_BITS_COUNT> DecodeSettings;
    4. QTime timer;
    5. long lTimeElapsed;
    6. DecodeSettings.set();
    7. timer.start();
    8.  
    9. Manager DBObject;
    10. DBObject.SetDBFilePath(szName);
    11. if(DBObject.CreateDatabase())
    12. {
    13. DBObject.InsertData();
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 


    The code in GUI .

    Qt Code:
    1. MyThread *ptr1 = new MyThread();
    2. connect(ptr1, SIGNAL(finished()), ptr1, SLOT(deleteLater()));
    3. ptr1->setName(strBinFileList[0].toStdString().c_str());
    4. ptr1->start();
    5.  
    6. MyThread *ptr1 = new MyThread();
    7. connect(ptr1, SIGNAL(finished()), ptr1, SLOT(deleteLater()));
    8. ptr1->setName(strBinFileList[1].toStdString().c_str());
    9. ptr1->start();
    10. while(ptr1->isRunning() || ptr2->isRunning() );
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 27th October 2009 at 15:36. Reason: missing [code] tags

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mulitple threads

    Please preview your post before sending it next time, ok?

    If you are using SQLite why not use the database driver for it that comes with Qt?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Oct 2009
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mulitple threads

    Quote Originally Posted by wysota View Post
    Please preview your post before sending it next time, ok?

    If you are using SQLite why not use the database driver for it that comes with Qt?
    Thanks .

    We need to encrypt the data base for security purpose that why we are not using QT driver.
    Still it is under progress.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mulitple threads

    In that case there is no way telling if the problem is caused by your code or the database driver. You have to run the app under a debugger and try to detect where the code fails. Are you sure the database driver is thread-safe or at least reentrant?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Oct 2009
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mulitple threads

    Hi ALL,
    My problem got solved ........ I am using QSharedMemory because of that it is crashing ...
    Now it got resolved..

    Thanks,
    Kishore

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Why do some QWidgets create own threads?
    By donglebob in forum Qt Programming
    Replies: 5
    Last Post: 6th July 2010, 17:01
  3. No context switch between Threads and parent ?
    By donglebob in forum Qt Programming
    Replies: 15
    Last Post: 29th October 2008, 22:49
  4. Once more: Threads in Qt4
    By high_flyer in forum Qt Programming
    Replies: 5
    Last Post: 9th August 2006, 18:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.