• <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>

            我住包子山

            this->blog.MoveTo("blog.baozishan.in")

            翻譯一篇文章Introduction to Multi-threaded Code 多線程編程的一些代碼 已經全譯好了

            Someone recently asked me what I recommend for synchronizing worker threads and I suggested setting an event. This person's response was that you could not do that since worker threads do not support a message pump (UI threads are required to support messages). The confusion here is that events and messages are different animals under windows.

            我忘記了我從哪里copy的這些例子代碼,他們可是非常簡單而有趣的。如果有人知道這些代碼的作者,我一定要好好感謝你和這位作者。

            注意這里有很多對于沒有提及的MFC的支持。像_beginthread(一個C運行時庫調用)的API可以在MFC應用程序中替換成AfxBeginThread

            無同步(No Synchronization)

            這第一個例子描述了兩個互不同步的線程。進程中的首要線程--主函數循環,輸出全局整形數組的內容。還有一個線程“Thread”不停的給數組每個元素+1。
            ?The thread called "Thread" continuously populates the global array of integers.

            				  #include <process.h>
            				  #include <stdio.h>
            				int a[ 5 ];
              
              void Thread( void* pParams )
              { int i, num = 0;
              
                while ( 1 )
                { 
                   for ( i = 0; i < 5; i++ ) a[ i ] = num;
                   num++;
                }
              }
              
              int main( void )
              { 
                 _beginthread( Thread, 0, NULL );
              
                 while( 1 )
                    printf("%d %d %d %d %d\n", 
                           a[ 0 ], a[ 1 ], a[ 2 ],
                           a[ 3 ], a[ 4 ] );
              
               return0;
              }
            

            注意這個例子的輸出,紅色的數處在一個主線程搶先于Thread工作過程中執行的打印動作

            81751652 81751652 81751651 81751651 81751651
            81751652 81751652 81751651 81751651 81751651
            83348630 83348630 83348630 83348629 83348629
            83348630 83348630 83348630 83348629 83348629
            83348630 83348630 83348630 83348629 83348629

            ?

            關鍵區域/臨界區域 對象(Critical Section Objects)

            如果你想讓主線程等待Thread線程處理好全局數組再做打印,一種解決方法是使用關鍵區域對象。
            關鍵區域對象提供同步于使用互斥器(Mutex)對象很相似, 除了關鍵區域對象之能在一個進程內發揮效用。Event, mutex,?以及 semaphore?對象也可以用在單進程的應用程序中, 但是關鍵區域對象提供一個相對快捷更加高效的同步機制. 就像互斥器一樣, 一個關鍵區域對象只能同時被一個線程擁有, 這個關鍵區域能夠在同時發生的數據存取時保護共享資源. 獲取關鍵區域的先后順序不定,可是不用太擔心,系統對于每一個線程都是平等的。

            ???  
              CRITICAL_SECTION cs;
              int a[ 5 ];
              
              void Thread( void* pParams )
              {
                int i, num = 0;
              
                while ( TRUE )
                {
                   EnterCriticalSection( &cs );
                   for ( i = 0; i < 5; i++ ) a[ i ] = num;
                   LeaveCriticalSection( &cs );
                   num++;
                }
              }
              
              int main( void )
            { InitializeCriticalSection( &cs ); _beginthread( Thread, 0, NULL ); while( TRUE ) { EnterCriticalSection( &cs ); printf( "%d %d %d %d %d\n", a[ 0 ], a[ 1 ], a[ 2 ], a[ 3 ], a[ 4 ] ); LeaveCriticalSection( &cs ); } return 0; }

            If you are running Windows 9x/NT/2000, you can run this program by clicking here.

            互斥器(Mutex Objects)

            一個互斥器是一個信號狀態的同步對象,當它不屬于任何一個線程時就用信號來體現,當被擁有時他的信號狀態就為None. 同一時刻只有一個線程可以擁有互斥器, 互斥器這個名字來自于他們對于并列的線程存取共享資源時表現出的行為。舉個例子,避免兩個線程同時寫入一個共享內存,每一個線程當需要執行存取共享資源的代碼時首先等待直到自己獲得擁有權. 在存取共享資源之后,線程釋放對互斥器的擁有權。

            兩個或以上的進程可以調用CreateMutex 來建立同樣名字的互斥器. 實際上第一個進程建立的這個互斥器, 隨后的進程只是得到了那個存在的互斥器的句柄. 這能使多進程共用一個互斥器, 當然用戶應該有確保建立互斥器的進程首先啟動的責任. 使用這種技術,你應該將這個 bInitialOwner標記設置成FALSE; 否則, 它可以因不同的進程最初擁有它而帶來困難.

            多進程可以有同一個mutex對象的句柄, 讓mutex對象能夠用于多進程間同步. 下面的對象共享機制是適用的:

            • 一個子進程通過CreateProcess?函數被建立,當CreateMutex的lpMutexAttributes?參數給予相應的mutex對象指針它可以繼承到一個mutex對象的句柄.
            • 一個進程可以在DuplicateHandle 函數中指定一個mutex對象句柄來建立一個句柄的拷貝由其他進程使用.
            • 一個繼承可以指定一個mutex的名字通過 CreateMutex 函數得到這個mutex對象的句柄.

            總的來說, 如果你想要進行線程同步,臨界區域更高效些.

            				#include <windows.h>
            				#include <process.h>
            				#include <stdio.h>
              
              HANDLE hMutex;
              int a[ 5 ];
              
              void Thread( void* pParams )
              { 
                 int i, num = 0;
              
                 while ( TRUE )
                 { 
                    WaitForSingleObject( hMutex, INFINITE );
                    for ( i = 0; i < 5; i++ ) a[ i ] = num;
                    ReleaseMutex( hMutex );
                    num++;
                 }
              }
              
              int main( void )
              {
                 hMutex = CreateMutex( NULL, FALSE, NULL );
                 _beginthread( Thread, 0, NULL );
              
                 while( TRUE )
            { WaitForSingleObject( hMutex, INFINITE ); printf( "%d %d %d %d %d\n", a[ 0 ], a[ 1 ], a[ 2 ], a[ 3 ], a[ 4 ] ); ReleaseMutex( hMutex ); } return0; }

            If you are running Windows 9x/NT/2000, you can run this program by clicking here.

            Event Objects事件對象

            若我們想要強制第二線程在主線程完成全局數組的內容輸出時執行該如何?這樣的話每行的輸出就只是遞增1。

            一個事件對象也是一個可以通過SetEvent or PulseEvent 函數設置像信號般的狀態的同步對象. 下面是兩種類型的事件對象.

            Object Description
            Manual-reset event
            手動激發對象
            只有使用ResetEvent 函數才可以將其設置為無激發狀態. 當它在激發狀態時, 它會激發所有正在等待的線程, 執行對相同 event對象的線程會立即從wait函數返回.
            Auto-reset event
            自動激發對象
            一個只相應一個線程的wait函數的事件對象(當這個對象是激發狀態),wait函數返回同時事件對象自動變成無激發狀態?,當沒有線程執行wait事件對象仍然是激發狀態.

            event object的用處就在于它可以在它發生時向等待著的線程發出信號標志從而使其wait結束.?舉個例子, 在overlapped I/O 操作時, 當異步操作完成時系統設置了那個由程序員指定(specified)的事件對象為信號狀態. A 一個單一線程可以指定許多不同的事件對象在許多同時發生的overlapped 操作運作, 調用一個多對象的wait函數可以當任意一個event object激發時結束等待.

            在一個線程中可使用 CreateEvent 函數建立一個event object. 在這個線程中指定這個event object 的特性是manual-reset?或者 auto-reset . 在這個線程中也可以命名一個event object. 其他進程中的線程也可以使用 OpenEvent 通過event object的名字打開一個現存event object . 另外關于mutex, event, semaphore, 以及 timer objects的其他信息, 就參考《Interprocess Synchronization》的文章.

            一個線程能夠用 PulseEvent?函數設置一個event?object 為信號狀態而后激發當前適當數量的wait線程,之后切換為無信號狀態 .?對于一個manual-reset event object, 所有的等待線程被返回(release). 對于一個auto-reset event object, 這個函數只能釋放一個等待的線程, 即使有更多線程在等待. 如果沒有線程在函數調用時等待, PulseEvent 只是簡單的將事件狀態設為無信號并且返回(個人注釋,這應該是跟setevent最不相同的地方!).

            Collapse
            				  #include <windows.h>
            				  #include <process.h>
            				  #include <stdio.h>
              
              HANDLE hEvent1, hEvent2;
              int a[ 5 ];
              
              void Thread( void* pParams )
              {
                 int i, num = 0;
            
                 while ( TRUE )
                 {
                    WaitForSingleObject( hEvent2, INFINITE );
                    for ( i = 0; i < 5; i++ ) a[ i ] = num;
                    SetEvent( hEvent1 );
                    num++;
                 }
              }
              
              int main( void )
              {
                 hEvent1 = CreateEvent( NULL, FALSE, TRUE, NULL );
                 hEvent2 = CreateEvent( NULL, FALSE, FALSE, NULL );
              
                 _beginthread( Thread, 0, NULL );
              
                 while( TRUE )
                 { 
                    WaitForSingleObject( hEvent1, INFINITE );
                    printf( "%d %d %d %d %d\n", 
                            a[ 0 ], a[ 1 ], a[ 2 ],
                            a[ 3 ], a[ 4 ] );
                    SetEvent( hEvent2 );
                 }
                 return0;
              }
            

            If you are running Windows 9x/NT/2000, you can run this program by clicking here.

            Summary of Synchronization Objects

            The MSDN News for July/August 1998 has a front page article on Synchronization Objects. The following table is from that article:

            Name Relative speed Cross process Resource counting Supported platforms
            Critical Section Fast No No (exclusive access) 9x/NT/CE
            Mutex Slow Yes No (exclusive access) 9x/NT/CE
            Semaphore Slow Yes Automatic 9x/NT
            Event Slow Yes Yes 9x/NT/CE
            Metered Section Fast Yes Automatic 9x/NT/CE

            by?William T. Block


            from codeproject

            謝謝回復的補充 ~~,上面拼錯了個詞,改過。。譯完了

            posted on 2007-02-16 14:06 Gohan 閱讀(992) 評論(1)  編輯 收藏 引用 所屬分類: C++

            Feedback

            # re: 翻譯一篇文章Introduction to Multi-threaded Code 多線程編程的一些代碼(先翻一點) 2007-02-22 17:10 池鳳彬

            http://www.codeproject.com/script/profile/whos_who.asp?vt=arts&id=244

            William T. Block View details
            Status Gold. Member No. 244

            View Member's Blog.
            Awards
            Messages Posted 11 - Poster
            Articles Submitted
            3 - Contributor
            Biography Bill's recent projects include graphical displays and printing of real-time data for the Oil Industry.

            "I started programming Windows' applications right after the release of Windows 1.0 and I am now actively working with Microsoft .NET"

            He currently works for Baker Hughes in the Houston, Texas area.
            Birthday Thursday 17th November, 1949
            Location United States
            Occupation Software development
            Interests C++, MFC, Win32, C#, ASP.NET
            Member since Thursday 6th July, 2000
            (6 years, 7 months) Gold Level
            Homepage http://www.wtblock.com/resume/  回復  更多評論   

            久久久久久午夜成人影院| 欧洲精品久久久av无码电影| 热99RE久久精品这里都是精品免费| 久久久亚洲AV波多野结衣| 色狠狠久久AV五月综合| 久久精品无码一区二区日韩AV| 国产99久久久国产精免费| 亚洲中文字幕久久精品无码喷水 | 国产精品久久久99| 日本五月天婷久久网站| 免费精品99久久国产综合精品| 综合久久一区二区三区 | 久久亚洲AV成人无码软件| 久久天天躁狠狠躁夜夜躁2O2O| 人妻精品久久久久中文字幕| 久久夜色精品国产亚洲av| 久久精品亚洲福利| 久久久久久久久无码精品亚洲日韩 | 久久久久亚洲AV无码网站| 国产精品美女久久久| 精品久久久久久99人妻| 久久中文字幕人妻熟av女| 久久亚洲精品国产精品婷婷| 老色鬼久久亚洲AV综合| 久久精品中文闷骚内射| 国产高清国内精品福利99久久| 久久久人妻精品无码一区| 亚洲AV无码久久精品蜜桃| 久久本道伊人久久| 久久综合成人网| 91久久精品国产91性色也| 国产精品欧美亚洲韩国日本久久 | 久久无码中文字幕东京热| 狠狠色丁香久久婷婷综| 久久久久波多野结衣高潮| 国产日产久久高清欧美一区| 久久久高清免费视频| 亚洲а∨天堂久久精品9966| 久久久久久狠狠丁香| 久久精品综合一区二区三区| 久久最近最新中文字幕大全|