Results 1 to 3 of 3

Thread: I don't quite understand this multithread example

  1. #1
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default I don't quite understand this multithread example

    Qt Code:
    1. #include <QtCore>
    2. #include <iostream>
    3.  
    4. const int DataSize = 20;
    5. const int BufferSize = 10;
    6. char buffer[BufferSize];
    7.  
    8. QSemaphore freeSpace(BufferSize); // for producer
    9. QSemaphore usedSpace(0); // for consumer
    10.  
    11. class Producer : public QThread
    12. {
    13. public:
    14. void run();
    15. };
    16.  
    17. void Producer::run()
    18. {
    19. for (int i = 0; i < DataSize; ++i) {
    20. freeSpace.acquire();
    21. buffer[i % BufferSize] = "ACGT"[uint(std::rand()) % 4];
    22. std::cerr <<"Producer: buffer["<<i % BufferSize<<"]"<<buffer[i % BufferSize]<<std::endl;
    23. usedSpace.release();
    24. }
    25. }
    26.  
    27. class Consumer : public QThread
    28. {
    29. public:
    30. void run();
    31. };
    32.  
    33. void Consumer::run()
    34. {
    35. for (int i = 0; i < DataSize; ++i) {
    36. usedSpace.acquire();
    37. buffer[i % BufferSize] = "ACGT"[uint(std::rand()) % 4];
    38. std::cerr <<"Consumer: buffer["<<i % BufferSize<<"]"<<buffer[i % BufferSize]<<std::endl;
    39. freeSpace.release();
    40. }
    41. //std::cerr << std::endl;
    42. }
    43.  
    44. int main()
    45. {
    46. Producer producer;
    47. Consumer consumer;
    48. producer.start();
    49. consumer.start();
    50. producer.wait();
    51. consumer.wait();
    52. return 0;
    53. }
    54.  
    55.  
    56. //pro file
    57. TEMPLATE = app
    58. CONFIG += console thread
    59. CONFIG -= app_bundle
    60. SOURCES = semaphores.cpp
    61.  
    62. // one output is showed here
    63. Producer: buffer[0]C
    64. Producer: buffer[Consumer: buffer[01]]CT
    65.  
    66. Producer: buffer[Consumer: buffer[12]G]T
    67.  
    68. Producer: buffer[3]Consumer: buffer[2A
    69. ]G
    70. Producer: buffer[4Consumer: buffer[3]C
    71. ]AProducer: buffer[
    72. 5]Consumer: buffer[4A
    73. ]C
    74. Producer: buffer[6Consumer: buffer[]G5]
    75. A
    76. Producer: buffer[7Consumer: buffer[6]G
    77. ]G
    78. Producer: buffer[8Consumer: buffer[]G7]
    79. G
    80. Producer: buffer[Consumer: buffer[89]A
    81. ]GProducer: buffer[0
    82. Consumer: buffer[]C
    83. 9]Producer: buffer[A1]C
    84. Consumer: buffer[
    85. 0]Producer: buffer[2C
    86. ]C
    87. Consumer: buffer[1Producer: buffer[]3C]T
    88.  
    89. Consumer: buffer[2Producer: buffer[4]C]C
    To copy to clipboard, switch view to plain text mode 

    In this example,why would comes the output like this: Producer: buffer[Consumer: buffer[01]]CT. Isn't it should be all like this:Producer: buffer[0]C

    Thanks!

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: I don't quite understand this multithread example

    I think you should have a common semaphore for producer and consumer.
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I don't quite understand this multithread example

    It just the output puzzle me. But now it's clear. Thanks!

Similar Threads

  1. Multithread in multicore CPU
    By ^NyAw^ in forum General Discussion
    Replies: 12
    Last Post: 26th December 2017, 15:19
  2. How can we make unit test for multithread in QT
    By learning_qt in forum Qt Programming
    Replies: 8
    Last Post: 15th January 2013, 17:09
  3. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 10:12
  4. about multithread
    By Pang in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2007, 19:06
  5. Help me about multithread!
    By vql in forum Newbie
    Replies: 19
    Last Post: 8th February 2007, 16:01

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.