Results 1 to 16 of 16

Thread: Multi-threading

  1. #1
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Question Multi-threading

    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,NUL L)....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

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

    Default Re: Multi-threading

    Why not use QThread instead of pthreads? And what does KDevelop have to do with multithreading? It's just a highly advanced text editor...

  3. #3
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Multi-threading

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multi-threading

    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. and platform dependent
    We can't solve problems by using the same kind of thinking we used when we created them

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

    Default Re: Multi-threading

    Quote Originally Posted by shamik View Post
    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.

    Quote Originally Posted by sunil.thaha View Post
    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. 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.

  6. #6
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Multi-threading

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multi-threading

    Quote Originally Posted by shamik View Post
    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?
    Last edited by jacek; 24th November 2006 at 11:12.

  8. #8
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Multi-threading

    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.

    Qt Code:
    1. #include <qapplication.h>
    2. #include <qstring.h>
    3. #include <pthread.h>
    4. #include <unistd.h>
    5. #include <qdatatable.h>
    6. #include <qsqldatabase.h>
    7. #include <stdio.h>
    8. #include <iostream.h>
    9.  
    10. void * thread2update(void *);
    11. pthread_t dispthread1;
    12. static int k=0;
    13.  
    14. int main ( int argc, char ** argv)
    15. {
    16.  
    17. QApplication a ( argc, argv);
    18. cout<<"main entered"<<endl;
    19. QSqlDatabase *db = QSqlDatabase::addDatabase(“ QMYSQL3”);
    20. db->setDatabaseName(“logindb”);
    21. db->setUserName(“root”);
    22. db->setPassword(“”);
    23. db->setHostName(“localhost);
    24. db->open();
    25.  
    26. loginfrm w;
    27. a.setMainWidget(&w);
    28. w.show();
    29. cout<<"creating thread"<<endl;
    30. pthread_create(&dispthread1,NULL,thread2update,NULL);
    31. a.connect(&a, SIGNAL(lastWindowClosed(),&a,SLOT(quit()));
    32.  
    33. cout<<”leaving main”<<endl;
    34. return a.exec();
    35.  
    36. }
    37.  
    38. void * thread2update(void *)
    39. {
    40. cout<<”entering thread”<<endl;
    41. while(g_end)
    42. {
    43. usleep(5000*1000);
    44. update();
    45. cout<<”executed “<<k+1<<”times”<<endl;
    46. k++;
    47. };
    48. cout<<”leaving thread”<<endl;
    49. }
    To copy to clipboard, switch view to plain text mode 

    (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
    Last edited by wysota; 24th November 2006 at 13:00. Reason: missing [code] tags

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

    Default Re: Multi-threading

    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.

  10. #10
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Multi-threading

    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

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multi-threading

    What is the difference between this thread and this one?

  12. #12
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Multi-threading

    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

  13. #13
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multi-threading

    Quote Originally Posted by wysota View Post
    I already said that yesterday...
    Really? . 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.
    We can't solve problems by using the same kind of thinking we used when we created them

  14. #14
    Join Date
    Sep 2006
    Location
    Rio de Janeiro, Brazil
    Posts
    44
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Multi-threading

    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

  15. The following user says thank you to ederbs for this useful post:

    sunil.thaha (28th November 2006)

  16. #15
    Join Date
    Sep 2006
    Posts
    102
    Thanks
    5
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Multi-threading

    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

  17. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Multi-threading


Similar Threads

  1. multi pixmap transparency
    By bluesguy82 in forum Qt Programming
    Replies: 3
    Last Post: 21st August 2006, 22:13
  2. Multi thread problem
    By sreedhar in forum Qt Programming
    Replies: 5
    Last Post: 26th June 2006, 13:33
  3. Multi frame management ... is it possible ?
    By yellowmat in forum Newbie
    Replies: 8
    Last Post: 25th January 2006, 10:41

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.