• <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>
            posts - 124,  comments - 29,  trackbacks - 0

            using System;
            using System.Collections.Generic;
            using System.Text;
            using System.Threading;
            using System.Collections;

            namespace MulThead
            {
                class signel
                {
                    public Semaphore singleRead;
                    public Semaphore singleWrite;
                    public Semaphore singleMutex;
                    private int size;

                    public signel(int size)
                    {
                        this.size = size;
                        singleRead = new Semaphore(0,size);         //一共size個read資源,目前有0個可用
                        singleWrite = new Semaphore(size, size);    //一共size個write資源,目前有size個可用
                        singleMutex = new Semaphore(1, 1);
                    }
                }
                class Reader
                {
                    private signel sgl;
                    private Queue<int> que;
                    private Form1 f1;
                    //保存信號量和隊列的一個引用
                    public Reader(signel sgl, Queue<int> que,Form1 f1)
                    {
                        this.sgl = sgl;
                        this.que = que;
                        this.f1 = f1;
                    }
                    public void Process()
                    {
                        int readvalue;
                        while(true)
                        {
                            sgl.singleRead.WaitOne();
                            sgl.singleMutex.WaitOne();
                            readvalue = que.Dequeue();
                            f1.txtReader.Text += readvalue.ToString();
                            f1.txtReader.Text += " ";
                            sgl.singleMutex.Release();
                            sgl.singleWrite.Release();     //釋放寫資源
                        }
                    }
                   
                }
                class Writer
                {
                    private signel sgl;
                    private Queue<int> que;
                    private Form1 f1;

                    public Writer(signel sgl, Queue<int> que,Form1 f1)
                    {
                        this.sgl = sgl;
                        this.que = que;
                        this.f1 = f1;
                    }
                    public void Process()
                    {

                        int i = 0;
                        while(i < 50)
                        {
                            i++;
                            sgl.singleWrite.WaitOne();
                            sgl.singleMutex.WaitOne();
                            f1.txtWriter.Text += i.ToString();
                            f1.txtWriter.Text += " ";
                            que.Enqueue(i);
                            sgl.singleMutex.Release();
                            sgl.singleRead.Release();         //釋放讀資源
                        }
                    }

                }
            }






                    private void Form1_Load(object sender, EventArgs e)
                    {
                        CheckForIllegalCrossThreadCalls = false;  //這樣在不是創建該控件(此form里的控件)的線程里也可以用這個控件了
                        //信號量
                        signel sgl = new signel(10);

                        //新建一個隊列緩沖區
                        Queue<int> que = new Queue<int>(10);
                        Reader readerThread = new Reader(sgl,que,this);
                        Writer writerThread = new Writer(sgl,que,this);
                        //開啟兩個線程
                        Thread tRead = new Thread(readerThread.Process);
                        Thread tWrite= new Thread(writerThread.Process);
                        tRead.Start();
                        tWrite.Start();
                    }

            posted on 2008-09-10 13:07 天書 閱讀(561) 評論(0)  編輯 收藏 引用

            <2008年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            常用鏈接

            留言簿(5)

            隨筆檔案

            文章分類

            文章檔案

            好友的Bolg

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久免费的精品国产V∧| 色综合久久无码五十路人妻 | 色综合久久88色综合天天 | 亚洲精品国产成人99久久| 精品无码久久久久久久久久| 中文字幕久久亚洲一区| 激情伊人五月天久久综合| 中文字幕一区二区三区久久网站| 久久这里有精品视频| 久久人人爽人人爽人人片AV不| 国产精品久久久久久影院 | 久久久久久精品无码人妻| 狠狠色丁香久久婷婷综合五月| 久久久久九九精品影院| 日本久久久久亚洲中字幕| 久久久精品日本一区二区三区| 久久久无码精品亚洲日韩按摩| 久久精品视屏| 99热热久久这里只有精品68| 久久这里只有精品首页| 久久精品国产一区二区| 久久精品国产亚洲欧美| 无码国产69精品久久久久网站| 人人狠狠综合88综合久久| 国内精品久久久久| 久久精品国产亚洲AV大全| 精品伊人久久久| 亚洲精品久久久www| 91精品日韩人妻无码久久不卡| 99久久婷婷国产综合亚洲| 天堂久久天堂AV色综合| 国产色综合久久无码有码| 久久久久国产精品人妻| 久久久久久久91精品免费观看| 久久亚洲中文字幕精品一区| 精品久久久久久无码中文字幕| 亚洲午夜久久久精品影院| 久久不射电影网| 岛国搬运www久久| 久久免费大片| 亚洲Av无码国产情品久久|