PDA

View Full Version : Mulitple threads



kishore7771
27th October 2009, 04:08
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.


for(nIndex =0 ; nIndex <strBinFileList.count(); ++nIndex)
{

MyThread *ptr1 = new MyThread();
connect(ptr1, SIGNAL(finished()), ptr1, SLOT(deleteLater()));
ptr1->setName(strBinFileList[nIndex].toStdString().c_str());
ptr1->start();

}

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.



MyThread *ptr1 = new MyThread();
connect(ptr1, SIGNAL(finished()), ptr1, SLOT(deleteLater()));
ptr1->setName(strBinFileList[0].toStdString().c_str());
ptr1->start();

MyThread *ptr1 = new MyThread();
connect(ptr1, SIGNAL(finished()), ptr1, SLOT(deleteLater()));
ptr1->setName(strBinFileList[1].toStdString().c_str());
ptr1->start();



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....

wagmare
27th October 2009, 04:26
may be in race condition of the two objects ... try using QMutex and 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` ....

kishore7771
27th October 2009, 04:37
may be in race condition of the two objects ... try using QMutex and 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.

mgoetz
27th October 2009, 09:45
Do you have a backtrace of the crash?

wysota
27th October 2009, 09:51
Do you have a separate database connection for each of the threads? And I don't mean variables but actual connection with different names.

kishore7771
27th October 2009, 11:57
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...

wysota
27th October 2009, 12:21
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.

kishore7771
27th October 2009, 15:30
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.


class Base
{
virtual InsertData();
}
class Derived :class Base
{
InsertData();
char szFileName[256];
SqliteWrapper database;

}

class Managaer
{
Base *pt;
InsertData(int format);

}
Managaer::InsertData(int format)
{
switch(format)
{
Base:
pt= new Derived();
pt->SetFileName();
pt->InsertionData();
}

}



The code in thread run method is




class MyThread : public QThread {
Q_OBJECT

private:
short nNumber;
char szName[512];
public:
MyThread();
void setNumber(short x);
void setName(const char*);
void run();
};


Code in Thread.cpp


void MyThread::run()
{
std::bitset<TOTAL_BITS_COUNT> DecodeSettings;
QTime timer;
long lTimeElapsed;
DecodeSettings.set();
timer.start();

Manager DBObject;
DBObject.SetDBFilePath(szName);
if(DBObject.CreateDatabase())
{
DBObject.InsertData();
}
}


The code in GUI .


MyThread *ptr1 = new MyThread();
connect(ptr1, SIGNAL(finished()), ptr1, SLOT(deleteLater()));
ptr1->setName(strBinFileList[0].toStdString().c_str());
ptr1->start();

MyThread *ptr1 = new MyThread();
connect(ptr1, SIGNAL(finished()), ptr1, SLOT(deleteLater()));
ptr1->setName(strBinFileList[1].toStdString().c_str());
ptr1->start();
while(ptr1->isRunning() || ptr2->isRunning() );

wysota
27th October 2009, 15:35
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?

kishore7771
27th October 2009, 15:50
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.

wysota
27th October 2009, 21:50
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?

kishore7771
17th February 2010, 06:19
Hi ALL,
My problem got solved ........ I am using QSharedMemory because of that it is crashing ...
Now it got resolved..

Thanks,
Kishore