Results 1 to 14 of 14

Thread: thread syncronization

  1. #1
    Join Date
    Nov 2012
    Posts
    48
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    4

    Default thread syncronization

    Hi every body,

    I am new to qt, and now I am writing a network application program with 3 threads additional to the main gui thread.
    one thread sniff the lan and recieve ethernet packets, one of the other thread process the data and inorder the recieved data and the last one is sending data to another computer through LAN by TCP sockets.

    The problem that I have is that, I dont know how I should inform the other thread that the data is completed and it should send it through the LAN????


    Thanks
    Regards,

  2. #2
    Join Date
    Dec 2012
    Posts
    197
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    25
    Thanked 41 Times in 33 Posts

    Default Re: thread syncronization

    When the thread is done processing data and ready to send emit a signal, catch it in the other thread or your main thread and then throw it again for the specific thread, depending on your implementation and design. So basically its a matter of Signals and Slots.

    Good Luck.
    Last edited by toufic.dbouk; 11th November 2013 at 20:54. Reason: Typo

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: thread syncronization

    Are you sure you want that many threads?

    E.g. why have a thread for sending?

    Anyway, there are a number of ways to move data between threads, the most convenient one is Qt signal/slots.

    Cheers,
    _

  4. #4
    Join Date
    Nov 2012
    Posts
    48
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    4

    Default Re: thread syncronization

    yes I need this number of thread, I need a thread to send data through LAN to a remote computer by tcp protocol.
    I dont want to send data between threads, I just want to inform the send thread about the completion of process task. and after informing the send thread force it to send the data.


    Added after 15 minutes:


    I have already use signal and slot but unfortunately it does not work. The precess thread emit but the solt in the send thread does not work.
    Last edited by havij000; 12th November 2013 at 11:12.
    Regards,

  5. #5
    Join Date
    Dec 2012
    Posts
    197
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    25
    Thanked 41 Times in 33 Posts

    Default Re: thread syncronization

    Yes Signals and Slots is a way to inform about status of threads example when a thread is done its task inform other threads that it is done.
    Quote Originally Posted by havij000
    I have already use signal and slot but unfortunately it does not work. The precess thread emit but the solt in the send thread does not work.
    Well you have something wrong with your code , so please post a minimal code reproducing the problem in order to help you.

    Good Luck.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: thread syncronization

    Quote Originally Posted by havij000 View Post
    yes I need this number of thread, I need a thread to send data through LAN to a remote computer by tcp protocol.
    Any why do you need a separate thread for that?
    Any specific reason why the main thread can't do that?

    Quote Originally Posted by havij000 View Post
    I dont want to send data between threads, I just want to inform the send thread about the completion of process task. and after informing the send thread force it to send the data.
    You don't have to send the data through the signal, a signal without arguments will do as well if it only triggers data fetching in the receiver thread.

    Quote Originally Posted by havij000 View Post
    I have already use signal and slot but unfortunately it does not work. The precess thread emit but the solt in the send thread does not work.
    Is the receiver thread's event loop running?

    Cheers,
    _

  7. #7
    Join Date
    Nov 2012
    Posts
    48
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    4

    Default Re: thread syncronization

    yes, both the loop of receive thread and the process thread are running and I use emit in the process thread.
    You don't have to send the data through the signal, a signal without arguments will do as well if it only triggers data fetching in the receiver thread.
    I dont send data through signal I just send the index of a common data through signal.

    Any why do you need a separate thread for that?
    Any specific reason why the main thread can't do that?
    Since I will have an scope in the GUI to show a copy of data, I need a seprate thread to send the data.


    Added after 4 minutes:


    this is the emit of the signal
    Qt Code:
    1. emit Q_Sample_Completed(data_prcss_child_1, indx);
    To copy to clipboard, switch view to plain text mode 

    this is the connect part.

    Qt Code:
    1. connect(prcs_thrd_child, SIGNAL(Q_Sample_Complete(Data_Class*,int)), this, SLOT(onQ_Sample_Completed(Data_Class*,int)));
    To copy to clipboard, switch view to plain text mode 

    this is on of the slot codes

    Qt Code:
    1. void Client_Thread::onQ_Sample_Completed(Data_Class *mdata, int index_num)
    2. {
    3. data_class_child_2 = mdata;
    4.  
    5. int i = index_num;
    6.  
    7. /*
    8.   //----------network section--------------------
    9.   int sock;
    10.   struct sockaddr_in server, localAddr;
    11.   //char message[1000] ;
    12.   char server_reply[2000];
    13.  
    14.  
    15.  
    16.  
    17.  
    18.   this->msleep(1);
    19.  
    20.   //Create socket
    21.   sock = socket(AF_INET , SOCK_STREAM , 0);
    22.   if (sock == -1)
    23.   {
    24.   printf("Could not create socket");
    25.   }
    26.   puts("Socket created");
    27.  
    28.   server.sin_addr.s_addr = inet_addr("192.168.100.150");
    29.   server.sin_family = AF_INET;
    30.   server.sin_port = htons( 8888 );
    31.  
    32.  
    33.   //local address
    34.   localAddr.sin_family = AF_INET;
    35.   localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    36.   localAddr.sin_port = htons(8888);
    37.  
    38.   int rc;
    39.   rc = bind(sock, (struct sockaddr *) &localAddr, sizeof(localAddr));
    40.  
    41.   if(rc < 0)
    42.   {
    43.   printf("cannot bind port TCP\n");
    44.   perror("error ");
    45.   exit(1);
    46.   }
    47.  
    48.   //Connect to remote server
    49.   if (::connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
    50.   {
    51.   perror("connect failed. Error");
    52.   return ;
    53.   }
    54.   puts("Connected\n");
    55.  
    56.   //keep communicating with server
    57.  
    58.   //--------------end of network section------------------------
    59.   */
    60.  
    61. char message_1[8442];
    62.  
    63.  
    64. //--------------send section--------------------------
    65. memcpy(message_1, &data_class_child_2->Q_smpl_pckt_ddc_1[i].total_hdr, 6);
    66. memcpy(&message_1[6], &data_class_child_2->Q_smpl_pckt_ddc_1[i].A2Dhdr, 36);
    67. memcpy(&message_1[42], &data_class_child_2->Q_smpl_pckt_ddc_1[i].CmpltData_prt_1, 1400);
    68. memcpy(&message_1[1442], &data_class_child_2->Q_smpl_pckt_ddc_1[i].CmpltData_prt_2, 1400);
    69. memcpy(&message_1[2842], &data_class_child_2->Q_smpl_pckt_ddc_1[i].CmpltData_prt_3, 1400);
    70. memcpy(&message_1[4242], &data_class_child_2->Q_smpl_pckt_ddc_1[i].CmpltData_prt_4, 1400);
    71. memcpy(&message_1[5642], &data_class_child_2->Q_smpl_pckt_ddc_1[i].CmpltData_prt_5, 1400);
    72. memcpy(&message_1[7042], &data_class_child_2->Q_smpl_pckt_ddc_1[i].CmpltData_prt_6, 1400);
    73.  
    74. if( send(sock , message_1 , strlen(message_1) , 0) < 0)
    75. {
    76. puts("Send failed\n");
    77. return ;
    78. }
    79.  
    80. printf("SEND a complete Q Pkt\n");
    81.  
    82. //&&&&&&&&&&&&&&&&&&&&&&&&&&& set the flag as the buffer is free &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    83.  
    84. data_class_child_2->Q_smpl_pckt_ddc_1[i].valid = false;
    85. data_class_child_2->Q_smpl_pckt_ddc_1[i].total_set = false;
    86.  
    87. data_class_child_2->Q_smpl_pckt_ddc_1[i].flag_scop = false;
    88. data_class_child_2->Q_smpl_pckt_ddc_1[i].flag_snd = false;
    89.  
    90. data_class_child_2->Q_smpl_pckt_ddc_1[i].part1_set = false;
    91. data_class_child_2->Q_smpl_pckt_ddc_1[i].part2_set = false;
    92. data_class_child_2->Q_smpl_pckt_ddc_1[i].part3_set = false;
    93. data_class_child_2->Q_smpl_pckt_ddc_1[i].part4_set = false;
    94. data_class_child_2->Q_smpl_pckt_ddc_1[i].part5_set = false;
    95. data_class_child_2->Q_smpl_pckt_ddc_1[i].part6_set = false;
    96. data_class_child_2->Q_smpl_pckt_ddc_1[i].part7_set = false;
    97.  
    98.  
    99. data_class_child_2->Q_smpl_pckt_ddc_1[i].arvd_pkts = 0;
    100.  
    101. printf("FREE Q sample buffer number: %d \n", i);
    102.  
    103. //&&&&&&&&&&&&&&&&&&&&&&&&&&& end of set part &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    104.  
    105. //--------------------------------------------------end of the send section---------------------------------------
    106.  
    107.  
    108.  
    109.  
    110.  
    111. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by havij000; 16th November 2013 at 08:11.
    Regards,

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: thread syncronization

    That connect is that in the QThread subclass? Do you want the slot be executed in the thread context or the main thread context?

    Also: the data access code seems to miss any proper thread synchronisation.
    One other thing that looks weird is usage of blocking socket API instead of using event driven QTcpSocket.

    Can you create a simple example where the reader thread just uses a QTimer to create random data?

    Cheers,
    _

  9. #9
    Join Date
    Nov 2012
    Posts
    48
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    4

    Default Re: thread syncronization

    the data access code seems to miss any proper thread synchronisation.
    Its not the complete code.
    Regards,

  10. #10
    Join Date
    Nov 2012
    Posts
    48
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    4

    Default Re: thread syncronization

    In any cae how can I tell another thread to run just for a while?

    Regards,

  11. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: thread syncronization

    QThread instances do that by default, i.e. they run their own event loop.

    If you derive from QThread and reimplement run(), then you can call exec() to start the event loop manually.

    Cheers,
    _

  12. #12
    Join Date
    Dec 2012
    Posts
    197
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    25
    Thanked 41 Times in 33 Posts

    Default Re: thread syncronization

    In any case when you run the thread it will run in its own event loop and hence it will run just for a while till the process its executing finishes.

  13. #13
    Join Date
    Nov 2012
    Posts
    48
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    4

    Default Re: thread syncronization

    you mean that in each iteration I should use exec() to start the other thread once?
    Regards,

  14. #14
    Join Date
    Dec 2012
    Posts
    197
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    25
    Thanked 41 Times in 33 Posts

    Default Re: thread syncronization

    Not really , exec() will start the thread that will run in its own event loop.
    Do you mean you want to start the thread in a loop where you iterate several times ? and on each iteration the exec() function will be executed ?

Similar Threads

  1. Replies: 1
    Last Post: 25th October 2012, 15:10
  2. Replies: 11
    Last Post: 12th September 2012, 16:25
  3. Replies: 1
    Last Post: 4th September 2012, 14:13
  4. Replies: 5
    Last Post: 22nd February 2011, 21:21
  5. QByteArray container and Timer syncronization
    By nagabathula in forum Newbie
    Replies: 4
    Last Post: 8th January 2011, 13: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.