• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Kisser Leon

            這個kisser不太冷
            posts - 100, comments - 102, trackbacks - 0, articles - 0

            環(huán)形緩沖

            Posted on 2010-04-01 09:17 kk 閱讀(651) 評論(0)  編輯 收藏 引用 所屬分類: C++
            環(huán)形緩沖是可以不用加鎖的。下面是一個實現(xiàn)的環(huán)形緩沖。使用c實現(xiàn),可能會出現(xiàn)write滿情況,這種情況導致的結(jié)果就是write不成功,數(shù)據(jù)沒寫進去。
            我這里只是簡單的丟了。
              1 //
              2 
              3 #include "stdafx.h"
              4 #include <windows.h>
              5 #include <time.h>
              6 #include <stdlib.h>
              7 
              8 #define MAX_BUF_LEN 20        // buf len // 必須要大于1
              9 int nRead = 0;                // read pos
             10 int nWrite = 0;                // write pos
             11 int buf[MAX_BUF_LEN] = {0}; // loop buf
             12 
             13 #define NN 20000
             14 __int64 sum = 0;
             15 __int64 lose = 0;
             16 __int64 total = (__int64)(1+NN)*(__int64)NN/2;
             17 bool bWriteFinished = false;
             18 
             19 void WriteBuf(int n)
             20 {
             21     int rpos = nRead;
             22     int wpos = nWrite;
             23     wpos++;
             24     // buf滿(即write又追上了read), 注為了避免與初始狀態(tài)重復判斷
             25     // 所以這里最后一個buf沒有寫就認為已經(jīng)滿了。所以MAX_BUF_LEN不能為1
             26     // 這里,直接丟棄該buf
             27     if (wpos % MAX_BUF_LEN == rpos % MAX_BUF_LEN)
             28     {
             29         lose += n;
             30         return;
             31     }
             32 
             33     buf[nWrite] = n;
             34     nWrite++;
             35     if (nWrite == MAX_BUF_LEN)
             36         nWrite = 0;
             37 }
             38 
             39 int ReadBuf()
             40 {
             41     if (nWrite == nRead)
             42         return -1;
             43 
             44     int n = buf[nRead];
             45     nRead++;
             46     if (nRead == MAX_BUF_LEN)
             47         nRead = 0;
             48     return n;
             49 }
             50 
             51 DWORD WINAPI ReadThread(LPVOID lpParameter)
             52 {
             53     int tmp = 0;
             54     int count = 0;
             55     while (true)
             56     {
             57         //printf("ReadThread = %d\n", ReadBuf());
             58         tmp = ReadBuf();
             59         if (-1 != tmp)
             60         {
             61             count++;
             62             sum += tmp;
             63             if (count == NN)
             64             {
             65                 printf ("ReadThread finished!\n");
             66                 break;
             67             }
             68         }
             69         else if (bWriteFinished)
             70         {
             71             printf ("ReadThread finished222!\n");
             72             break;
             73         }
             74 
             75         Sleep(rand() % 10);
             76     }
             77     return 0;
             78 }
             79 
             80 DWORD WINAPI WriteThread(LPVOID lpParameter)
             81 {
             82     int n = 0;
             83     while (true)
             84     {
             85         n++;
             86         WriteBuf(n);
             87         if (n == NN)
             88         {
             89             printf ("WriteThread finished!\n");
             90             bWriteFinished = true;
             91             break;
             92         }
             93 
             94         Sleep(rand() % 10);
             95     }
             96     return 0;
             97 }
             98 
             99 int main(int argc, char* argv[])
            100 {
            101     srand(time(NULL));
            102 //     for (int i = 0; i < 10; i++)
            103 //     {
            104 //         printf ("rand = %d\n", rand() % 10);
            105 //     }
            106 
            107     DWORD threadid[2= {0};
            108     HANDLE hThread[2= {0};
            109     int param = 0;
            110     hThread[0= CreateThread(NULL, 0, ReadThread, &param, CREATE_SUSPENDED, &threadid[0]);
            111     ResumeThread(hThread[0]);
            112     hThread[1= CreateThread(NULL, 0, WriteThread, &param, CREATE_SUSPENDED, &threadid[1]);
            113     ResumeThread(hThread[1]);
            114 
            115     getchar();
            116 
            117     printf ("total = %lld\n", total);
            118     printf ("lose = %lld\n", lose);
            119     printf ("sum = %lld\n", sum);
            120 
            121     if (total == lose + sum)
            122     {
            123         printf ("loop buf works well!\n");
            124     }
            125     else
            126     {
            127         printf ("loop buf works wrong!\n");
            128     }
            129 
            130     return 0;
            131 }
            132 
            上面是加了測試代碼的,我測試了一下似乎沒問題。
            注意:這里只允許一個線程讀,一個線程寫。如果是多個線程讀寫的話,需要鎖!
            另外,很多細節(jié)都沒考慮進去
            亚洲狠狠婷婷综合久久蜜芽| 亚洲国产精品无码久久久久久曰 | 久久久久久国产精品无码下载 | 久久99国产精品久久99小说| 久久久无码精品亚洲日韩按摩| 久久免费国产精品一区二区| 伊人久久大香线蕉综合网站| 久久人人爽人人爽人人片AV不 | 日韩一区二区久久久久久| 秋霞久久国产精品电影院| 久久久久18| 久久久久亚洲Av无码专| 精品久久久久久国产免费了| 99久久精品影院老鸭窝| 性欧美大战久久久久久久久| 精品久久久无码中文字幕天天| 亚洲色大成网站www久久九| 91秦先生久久久久久久| 91精品免费久久久久久久久| 精品久久久久久久国产潘金莲| 精品久久777| 91精品国产91热久久久久福利| 思思久久好好热精品国产| 久久免费99精品国产自在现线| 国产亚州精品女人久久久久久 | 伊人久久精品无码二区麻豆| 久久精品国产精品亚洲人人 | 久久久久亚洲AV无码专区首JN| 久久精品嫩草影院| 久久久av波多野一区二区| 尹人香蕉久久99天天拍| 久久亚洲电影| 久久国产精品偷99| 99国产欧美精品久久久蜜芽| 久久棈精品久久久久久噜噜| 久久WWW免费人成一看片| 国产精品99久久久久久宅男小说| 久久久久亚洲AV无码去区首| 国产高清国内精品福利99久久| 久久青青草原亚洲av无码| 夜夜亚洲天天久久|