QSqltablemodel make my app crash in SMP
-
i have new a Qsqltablemodel object in my codel .
but the no-main thread will allways crash my program when my main thread is using the model.
the code is like this:
@
QSqlTableModel* model = new QSqlTableModel;
void* test(void * p)
{
for(int i = 0; i < 10000; i++)
{
model->setTable("T_table");
model->setData(/ignore here/);//mean use model to do some read or write;
}}
int main()
{
QApplication app(argc, argv);pthread_create(&id1,NULL,test,0); for(int i = 0; i < 10000; i++) { model->setTable("T_table"); model->setData(/*ignore here*/);//mean use model to do some read or write; } return app.exec();
}
@
I thought it may work well if i lock the read/write lines.@
QMutex lock;
QSqlTableModel* model = new QSqlTableModel;void func()
{
lock.lock();
model->setTable("T_table");
model->select();
model->setData(/ignore here/);//means use model to do some read or write;
.....
lock.unlock();
}
void* test(void * p)
{
for(int i = 0; i < 10000; i++)
{
func();
}}
int main()
{
QApplication app(argc, argv);pthread_create(&id1,NULL,test,0); for(int i = 0; i < 10000; i++) { func(); } return app.exec();
}
@
but i'am wrong, it crashed again.
what's reason to make it crash?
sorry for my poor English.
hope you understand the meaning and help me out of the trouble .
Any help is appreciated -
Hi,
take a look at the link below. maybe what is looking.
But if you encapsulate all access to the database in a single thread
this might workfor example:
http://qt-project.org/doc/qt-4.8/threads-modules.html#threads-and-the-sql-moduleThreads and the SQL Module
A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported.
In addition, the third party libraries used by the QSqlDrivers can impose further restrictions on using the SQL Module in a multithreaded program. Consult the manual of your database client for more information
-
thx,that is really what i am looking for.
You are a great help--
because of bad language skill about English,it almost impossible for me to find technical documentation directly. -
Hi,
That I thank you for giving me this opportunity to help youBy the way, what is your native language?