• <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 閱讀(641) 評論(0)  編輯 收藏 引用 所屬分類: C++
            環(huán)形緩沖是可以不用加鎖的。下面是一個實現(xiàn)的環(huán)形緩沖。使用c實現(xiàn),可能會出現(xiàn)write滿情況,這種情況導(dǎo)致的結(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)重復(fù)判斷
             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é)都沒考慮進去
            人妻丰满?V无码久久不卡| 久久精品亚洲中文字幕无码麻豆| 精品久久久久久无码免费| 久久精品无码一区二区日韩AV| 久久精品国产精品亚洲精品| 久久亚洲AV无码精品色午夜| 69久久夜色精品国产69| 伊人久久精品线影院| 色婷婷综合久久久久中文字幕| 热99RE久久精品这里都是精品免费| 人妻无码久久一区二区三区免费 | 久久精品国产色蜜蜜麻豆| 国产69精品久久久久9999| 精品永久久福利一区二区 | 中文字幕精品无码久久久久久3D日动漫 | 国产精品美女久久久久AV福利| 日韩精品无码久久一区二区三| 久久99热只有频精品8| 青青草国产97免久久费观看| 国产国产成人精品久久| 久久亚洲国产精品成人AV秋霞 | 久久99中文字幕久久| 亚洲国产婷婷香蕉久久久久久| 久久亚洲欧美国产精品| 久久综合九色综合欧美就去吻| 精品久久久久一区二区三区| 日韩人妻无码精品久久免费一 | 国产美女久久精品香蕉69| 无夜精品久久久久久| 欧美日韩中文字幕久久伊人| 99久久人妻无码精品系列 | 国产精品9999久久久久| 亚洲色大成网站www久久九| 色婷婷综合久久久久中文一区二区 | 午夜福利91久久福利| 欧美久久综合性欧美| 色成年激情久久综合| 国产成人精品久久一区二区三区av | 久久精品男人影院| 日本一区精品久久久久影院| 久久久久av无码免费网|