PDA

View Full Version : Multi-threading



shamik
23rd November 2006, 12:43
hii friends

i have opened a Qt project in kdevelop.
it works fine.
but when i create any thread from anywhere in the project may it the main function or from any other class then it enters the thread and then immediately gives
"segmentation fault".

i think it is because the function in which i am creating the thread terminates and so the thread also has to terminate.

but i dont know the exact reason for this.

i have included the header file ---- pthread.h and unistd.h
my Qt supports multi threading
and i am creating the thread using pthread_create(&dispthread1,NULL,thread2update,NULL)....this is the command i use to create thred.

please tell me how to solve this segmentation fault error.
also how to do multithreading in kdevelop.

thanks and regards
shamik

wysota
23rd November 2006, 15:17
Why not use QThread instead of pthreads? And what does KDevelop have to do with multithreading? It's just a highly advanced text editor...

shamik
24th November 2006, 04:55
what difference it makes using QThread rather than pthread??
please tell me i dont know this.

also tell me how to do multi threading using QThread also. i tried alot yesterday using qthread but dint worked. the program which is given in Qt manuals is working but i dont understand how can i know that which thread has entered the run() function.

regards
shamik

sunil.thaha
24th November 2006, 05:07
Hi,

First of all, you seems to be confused about Kdevelop. It's just a text editor and has got nothing to do with Qt.

For the QThread. You can go though the QThread examples given in the examples folder. Also read through the Thread section in the docs. You can also go though the Thread chapter in the C++ Gui Programing Book. It starts from the basics

AFAIK, In order to create a thread,
1. Subclass QThread
2. Override the run virtual function
3. When the Thread executes, whatever needs to be executed has to be put in the run()
4. Create a Thread object from some where outside
5. call thread.start();


Difference between QThread and pthread,
pthread is not OOP. :eek: and platform dependent

wysota
24th November 2006, 09:52
what difference it makes using QThread rather than pthread??
please tell me i dont know this.

With Qt3 it didn't make much of a difference, but the concept of object and threads canged in Qt4. Now every object is owned by a thread, signals and slots can work accross threads, etc. so it's just safer to use QThreads if you can (if you already use Qt for other things). QThreads use pthreads internally, so the functionality will be the same.


First of all, you seems to be confused about Kdevelop. It's just a text editor and has got nothing to do with Qt.
I already said that yesterday...


Difference between QThread and pthread,
pthread is not OOP. :eek: and platform dependent
So? Does it mean you can't use pthreads anymore?
Neither java nor oop, nor Qt nor any programming approach is the "only truly way" to develop applications.

shamik
24th November 2006, 10:31
thanks again.

but still many of my questions remain unanswered.
please tell me how to do multithreading using pthread. i stress on pthreads because i have to use them (dont ask y) and not Qthreads.

i have created an application in which i create the thread using pthread_create in main.
the program enters the thread and then terminates immediately within a fraction of a second saying "segmentation fault".

i think its because the main gets terminated the thread also forcibly has to get terminated and so its segmentation fault. but still i m not sure about it. i dont use pthread_exit in main but m trying on different combinations and possiblities till i get a reply from u friends.

jacek
24th November 2006, 10:36
i have created an application in which i create the thread using pthread_create in main.
the program enters the thread and then terminates immediately within a fraction of a second saying "segmentation fault".
Can you show us the code that starts the thread? What should your thread do?

shamik
24th November 2006, 11:35
hii there

the application contains three dialogs one of which contains QDataTable whose data comes from mysql.
the first dialog which is set as main widget under main is a login dialog after entering correct user name and password the second dialog is shown. this contains the QDataTable.
now i want that this QDataTable should get updated (i.e refresh itself) automatically without unloading and then loading itself whenever the database is changed.
the following is the code of main function below which is the code for the thread.


#include <qapplication.h>
#include <qstring.h>
#include <pthread.h>
#include <unistd.h>
#include <qdatatable.h>
#include <qsqldatabase.h>
#include <stdio.h>
#include <iostream.h>

void * thread2update(void *);
pthread_t dispthread1;
static int k=0;

int main ( int argc, char ** argv)
{

QApplication a ( argc, argv);
cout<<"main entered"<<endl;
QSqlDatabase *db = QSqlDatabase::addDatabase(“ QMYSQL3”);
db->setDatabaseName(“logindb”);
db->setUserName(“root”);
db->setPassword(“”);
db->setHostName(“localhost);
db->open();

loginfrm w;
a.setMainWidget(&w);
w.show();
cout<<"creating thread"<<endl;
pthread_create(&dispthread1,NULL,thread2update,NULL);
a.connect(&a, SIGNAL(lastWindowClosed(),&a,SLOT(quit()));

cout<<”leaving main”<<endl;
return a.exec();

}

void * thread2update(void *)
{
cout<<”entering thread”<<endl;
while(g_end)
{
usleep(5000*1000);
update();
cout<<”executed “<<k+1<<”times”<<endl;
k++;
};
cout<<”leaving thread”<<endl;
}

(please ignore any syntax or spelling errors coz this is not pasted code its hand written.)
g_end is a global variable and its value is changed in the second form on specific action.

when i compile this program it tell "update undeclared".
but in the code which i have got previously has this function to update the contents of the table. and when i comment that line of update() then it compiles well.

while running the program (with update commented) the output is something like this:
entering main
creating thread
leaving main
executed 1 times
executed 2 times
executed 3 times

and goes on till i end the program. this is the latest one and now it is not giving the error of segmentation fault.
but the problem is still not solved. i want that after login my QDataTable should get updated automatically. i.e. whenever any change is made in the database then it should display that change immediately.

tell me what function to use 4 this.
if u want i can mail the entire project folder. it is very small and has just three dialogs and three .ui.h files of code.

and finally let me clear to all that i had a bit confusion with kdevelop but now it is totally clear. afterall every newbie has it. but that doesnt mean that all should keep on saying that rather than concerntrating on the issue.


thanks and regards.
shamik

wysota
24th November 2006, 13:01
What does update() from the thread do? If it operates on some Qt widget, then you just found your problem - you can't access the gui from within worker threads.

shamik
24th November 2006, 13:34
ya exactly

i want the update to refresh or update the QDataTable widget in editfrm which is the second form to get loaded so that it displays the fresh database contents everytime they get modified.

how to do it??

regards
shamik

jacek
24th November 2006, 14:57
What is the difference between this thread and this one (http://www.qtcentre.org/forum/f-qt-programming-2/t-refresh-qdatatable-4595.html)?

shamik
27th November 2006, 04:28
almost same
it started with some other topic but both ended up in 1. answere this one rather coz it contains the code also and the other one has just nothing but only arguments.

as one of the replies earlier says that update() operates on qwidget and thats correctly. but still what is the problem and how can i solve it??
if i cant access gui from worker threads then wht else can i do? i dont want to use QThreads and no timers. only pthreads.

the thing i have noticed is when i create the thread from main, then even if the main terminates then also then thread continues to go on until it finishes. so now is there any hope that i can solve this problem??
it is very simple but i m not getting it!!


shamik

sunil.thaha
27th November 2006, 13:59
I already said that yesterday...

Really? :p. Forgot to mention " As wysota had mentioned ". Excusez-moi
Wanted to stress that again. Since its a basic mistake



So? Does it mean you can't use pthreads anymore?

No, Never. Should be used when it has to be..., But when Qt is used in a project, What would be the justification for using pthreads ? Afterall ( as u said earlier;) ) it's a wrapper over pthread ?



Neither java nor oop, nor Qt nor any programming approach is the "only truly way" to develop applications.

I agree. But OOP does bring in a clarity & maintainability doesn't it ? I am not talking anti-procedural but just stressing that OOP scales up well.

ederbs
27th November 2006, 16:07
Hi,

It does not need to use pthread,

If you desire to effect refreshs of the QDataTable you can make with the code example here.

http://200.193.29.195/qt/AcompanhamentoForm.h
http://200.193.29.195/qt/AcompanhamentoForm.cpp

shamik
28th November 2006, 05:59
the code is too complex to understand
but hey where is the ui file?
anyways in this code also the function used to refresh the table is : dataTable->refresh(QDataTable::RefreshAll)

which i have tried. it too is not refreshing the qdatatable. the function to which the timer points is getting executed every 3secs but this line although present in that function doesnt work i.e. the table is not showing the updated data from the database.

please help

shamik
:( :eek:

jacek
28th November 2006, 10:22
Parallel thread: http://www.qtcentre.org/forum/f-qt-programming-2/t-refresh-qdatatable-4595-post24361.html#post24361