Results 1 to 5 of 5

Thread: problem in using pthread in QT

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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
  •  
Qt is a trademark of The Qt Company.