Results 1 to 5 of 5

Thread: problem in using pthread in QT

  1. #1
    Join Date
    Aug 2012
    Posts
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default problem in using pthread in QT

    I am trying to implement a single server & multiclient application for that i am using pthread.My server is a GUI in which all the connected clients should be shown ..ultimatly i have to do some file transfer to multiple clients parallaly.But i am facing problems.

    In server side,I have created thread in class MainWindow

    Qt Code:
    1. void MainWindow::new_connect()
    2. {
    3.  
    4. ui->label->setText("connected");
    5. sock1=server->nextPendingConnection();
    6. pthread_t t;
    7. if(pthread_create (&t,NULL,&myserver::func,this)<0)
    8. {
    9. qDebug()<<"error in thread";
    10. }
    11. else
    12. {
    13. qDebug()<<"thread create";
    14. }
    To copy to clipboard, switch view to plain text mode 

    then i have added one class myserver where thread function "func" is defined,

    Qt Code:
    1. void* myserver::func(void * parm)
    2.  
    3. {
    4.  
    5. try
    6. {
    7.  
    8. pthread_mutex_lock(&mtx);
    9.  
    10. char *bb="server";
    11.  
    12. MainWindow *mn = (MainWindow*)parm;
    13. sleep(4);
    14. if(mn->sock1->write(bb))//first "server" string is send to the client
    15. {
    16. mn->sock1->waitForBytesWritten(3000);
    17. qDebug()<<"server send";
    18. }
    19. else
    20. {
    21. qDebug()<<"server not send";
    22. }
    23. while(1)
    24. {
    25. sleep(2);
    26. memset(rr,0,sizeof(rr));
    27. if(mn->sock1->read(rr,mn->sock1->bytesAvailable())) //Now it will receive client host name
    28. {
    29. QString str=(char *)rr;
    30. QString time=QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm");
    31. n=n+1;
    32. // ***Connected Clients are Shown in the GUI with date and time****
    33. mn->ui->textEdit->append(QString::number(n)+": "+str+"------ "+time);
    34.  
    35. break;
    36.  
    37. }
    38.  
    39. else{
    40.  
    41. mn->ui->textEdit->setText("not recv"); break;}
    42.  
    43. }
    44.  
    45. catch (const char* e)
    46. {
    47. cout << "Exception caught : " << e << endl;
    48. }
    49.  
    50. pthread_mutex_unlock(&mtx);
    51.  
    52. }
    To copy to clipboard, switch view to plain text mode 

    What have i done mistake ? plz help
    Last edited by wysota; 26th September 2012 at 19:57. Reason: missing [code] tags

  2. #2
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: problem in using pthread in QT

    can you plz mention what problem your facing ....

  3. #3
    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: problem in using pthread in QT

    Quote Originally Posted by rinku View Post
    I am trying to implement a single server & multiclient application for that i am using pthread.My server is a GUI in which all the connected clients should be shown ..ultimatly i have to do some file transfer to multiple clients parallaly.But i am facing problems.
    Is there any particular reason why you are using pthreads and not QThread?

    While we're at it... is there any particular reason why you are using threads at all?

    With your current construction you'll have lots of trouble because you can't access widgets from within worker threads (regardless if you use mutexes or not).

    Networking in Qt can (and usually should) be done in a single thread since Qt's networking routines are non-blocking.
    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.


  4. #4
    Join Date
    Aug 2012
    Posts
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem in using pthread in QT

    @pradeep m facing problems like sometime my server got crash or sometimes in synchronization.
    @wysota there is no such reason.. i have just tried..i am using thread to handle multiple clients.. i tried this application using QList also. but i faced problem.

    void MainWindow::new_connect()

    {
    ui->label->setText("connected");
    QTcpsocket *client=server->nextPendingConnection();
    clientconnection.append(client);
    mainwindow m;
    m.send_message(client);
    }

    and In send_message() i want to display the connected client name but my GUI become hang.. so doesnt print anything..

  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: problem in using pthread in QT

    Quote Originally Posted by rinku View Post
    @wysota there is no such reason.. i have just tried..i am using thread to handle multiple clients..
    Then don't use threads.

    void MainWindow::new_connect()

    {
    ui->label->setText("connected");
    QTcpsocket *client=server->nextPendingConnection();
    clientconnection.append(client);
    mainwindow m;
    m.send_message(client);
    }

    and In send_message() i want to display the connected client name but my GUI become hang.. so doesnt print anything..
    Your code above doesn't make any sense.
    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.


Similar Threads

  1. Qthread faster than pthread???
    By lsantos in forum Qt Programming
    Replies: 1
    Last Post: 16th April 2012, 21:58
  2. error in creating QThread and Pthread
    By shakthi in forum Qt Programming
    Replies: 4
    Last Post: 30th July 2011, 16:36
  3. qt-4.5.3 linking issue (excessive pthread)
    By Ronayn in forum Qt Programming
    Replies: 1
    Last Post: 7th April 2011, 14:48
  4. -pthread havoc
    By Cruz in forum Qt Programming
    Replies: 8
    Last Post: 10th March 2010, 12:22
  5. pthread instead QThread
    By brevleq in forum Qt Programming
    Replies: 8
    Last Post: 23rd December 2008, 07:16

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.