qt - QSemaphore producer consumer problem -


This is an example of more or less Qt with some small changes

Output is PCPCPPCC ... e.t.c. I do not understand why

i.e., how am I confused about sProducer.acquire (256); Works. I believe I understand how sProducer.acquire (1); Works. I do not understand this to achieve more than 1, because I do not know that receiving more than 1 will make a difference logically. Can anyone explain this? On the surface, reading 1 byte and 1 byte is not very efficient due to the semaphore overhead ... but the acquisition of more resources does not seem to have a performance difference nor the code is understandable.

Logically, I think that acquisition and release should be both a single number (which is the number). But how can I modify this code so that I can get more (say 256) and thus reduce the semaphore overhead? I do not understand when I get the code when Receive and release is not 1.

  #include & lt; QtCore & gt; # Include & lt; Iostream & gt; # Include & lt; QTextStream & gt; //global variables. QTextStream Out (stdout); (Stdin) in QTextStream; Const int DataSize = 1024; Configure buffer size = 512; Four buffer [buffer size]; QSemaphore sProducer (buffer size); QSemaphore sConsumer (0); // ----------------------------- Class manufacturer: Public Keithheide {public: zero running (); }; Zero Producer :: Run () {for (int i = 0; i & lt; DataSize; ++ i) {sProducer.acquire (256); Buffer [i% buffer size] = 'p'; SConsumer.release (256); }} Category Consumer: Public Quathheid {Public: zero running (); }; Zero consumer :: runs () {for (int i = 0; i    

Since there is only one manufacturer and one user, they can easily move themselves Private cursor, their i variable, which they want in quantity of bytes, unless there is enough space to do this (with a 512 buffer 256 on both sides will cause a standoff ).

Basically, when a thread successfully achieves 256 bytes, it means that it can safely read or write these 256 bytes in one operation, so you can control it For the producer:

  Zero Generator :: Run () {for (int i = 0; i & lt; datatys; ++ i) {const int blockSize = 256; SProducer.acquire (blocksize); For (int j = 0; j and lt; block seas; ++ i, ++ j) {buffer [i% buffer size] = 'p'; } SConsumer.release (blockSize); }}   

and for the consumer

  zero consumer :: runs () {for (int i = 0; i    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -