• <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>
            隨筆 - 96  文章 - 255  trackbacks - 0
            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            E-mail:zbln426@163.com QQ:85132383 長期尋找對戰略游戲感興趣的合作伙伴。

            常用鏈接

            留言簿(21)

            隨筆分類

            隨筆檔案

            SDL相關網站

            我的個人網頁

            我的小游戲

            資源下載

            搜索

            •  

            積分與排名

            • 積分 - 493120
            • 排名 - 39

            最新評論

            閱讀排行榜

            評論排行榜

            還是先看一段非多線程的程序。我們用TestClass1表示在線程中創建的對象類,用TestClass2表示與創建線程的操作在同一定義域(也就是同一對{}之中的)的局部變量的對象類。
            之所以提出TestClass2這種類,是因為在實際編程中我們會遇到這種情況:我們不可預知這個類何時創建以及創建多少;這個類的對象是一個新線程的參數。比如,在sokcet中的TCP server端就會有這種情況:如果每一個新連接的client都用創建一個新的線程去處理,我們不可預知在什么時候會有多少客戶端連過來。
            我們先觀察沒有多線程的時候對象的生命周期:
            #include <iostream>
            #include 
            "windows.h"

            class TestClass1{
            private:
                
            int x;
            public:
                
            explicit TestClass1(int to_x):x(to_x)
                {}
                
            ~TestClass1()
                {
                    std::cerr 
            << "destruction: 1." << std::endl;
                }
                
            void show() const
                {
                    std::cerr 
            << x << std::endl;
                }
            };

            class TestClass2{
            private:
                
            int* pX;
            public:
                
            explicit TestClass2(int to_x)
                {
                    pX 
            = new int;
                    
            *pX = to_x;
                }
                
            ~TestClass2()
                {
                    delete pX;
                    std::cerr 
            << "destruction: 2."  << std::endl;
                }
                
            const int& value() const
                {
                    
            return *pX;
                }
            };

            DWORD WINAPI thread_func(LPVOID pN)
            {
                Sleep(
            200);
                TestClass1 test((
            *((TestClass2*)pN)).value());
                test.show();
                
            return 0;
            }

            int main(int argc, char* argv[])
            {
                
            for (int i = 0; i < 3++i) {
                    TestClass2 n(
            5);
                    
                    thread_func((LPVOID)
            &n);
                    std::cerr 
            << "loop: " << i+1 << std::endl;
                }

                Sleep(
            2000);

                std::cout 
            << "main() ok." << std::endl;

                
            return 0;
            }
            這是標準的C++模式,對象的生命周期是可以預見的:
            5
            destruction: 
            1.
            loop: 
            1
            destruction: 
            2.
            5
            destruction: 
            1.
            loop: 
            2
            destruction: 
            2.
            5
            destruction: 
            1.
            loop: 
            3
            destruction: 
            2.
            main() ok.
            請按任意鍵繼續. . .
            如果我們改成線程調用:
            #include <iostream>
            #include 
            "windows.h"

            class TestClass1{
            private:
                
            int x;
            public:
                
            explicit TestClass1(int to_x):x(to_x)
                {}
                
            ~TestClass1()
                {
                    std::cerr 
            << "destruction: 1." << std::endl;
                }
                
            void show() const
                {
                    std::cerr 
            << x << std::endl;
                }
            };

            class TestClass2{
            private:
                
            int* pX;
            public:
                
            explicit TestClass2(int to_x)
                {
                    pX 
            = new int;
                    
            *pX = to_x;
                }
                
            ~TestClass2()
                {
                    delete pX;
                    std::cerr 
            << "destruction: 2."  << std::endl;
                }
                
            const int& value() const
                {
                    
            return *pX;
                }
            };

            DWORD WINAPI thread_func(LPVOID pN)
            {
                Sleep(
            200);
                TestClass1 test((
            *((TestClass2*)pN)).value());
                test.show();
                
            return 0;
            }

            int main(int argc, char* argv[])
            {
                
            for (int i = 0; i < 3++i) {
                    TestClass2 n(
            5);
                    
                    HANDLE hThrd;
                    DWORD thrdId;
                    hThrd 
            = CreateThread(    NULL,
                                            
            0,
                                            thread_func,
                                            (LPVOID)
            &n,
                                            
            0,
                                            
            &thrdId);
                    
                    std::cerr 
            << "loop: " << i+1 << std::endl;
                }

                Sleep(
            2000);

                std::cout 
            << "main() ok." << std::endl;

                
            return 0;
            }
            可以看到函數返回了錯誤的值(至于為什么每次都是36我還不清楚,但是至少不是正確的數字5),這是因為在線程調用TestClass2的對象之前已經被析構的緣故。
            loop: 1
            destruction: 
            2.
            loop: 
            2
            destruction: 
            2.
            loop: 
            3
            destruction: 
            2.
            36
            destruction: 
            1.
            36
            destruction: 
            1.
            36
            destruction: 
            1.
            main() ok.
            請按任意鍵繼續. . .
            所以,如果我們設想構造一個類,這個類的對象可以調用包含this的線程,那么這個對象一定不能是局部變量,或者說,我們必須在循環的{}對之前先把這些對象構造出來。這與我們的需求不符合——我們并不知道需要多少對象以及如何構造(比如構造TCP的通訊socket需要accept()接受客戶端的信息),在這種情況下,我們只能在線程中去構造對象,這樣的對象生命周期跟線程函數一樣。
            或者說,如果我們希望用類來封裝線程,那么這些可以調用線程的對象必須是全局的。相關內容請參考本人前面的教程“初試多線程”等。
            posted on 2010-06-05 21:06 lf426 閱讀(802) 評論(0)  編輯 收藏 引用 所屬分類: 語言基礎、數據結構與算法Win32與VC
            97精品国产91久久久久久| 久久被窝电影亚洲爽爽爽| 久久天天躁狠狠躁夜夜躁2014| 久久综合色老色| 亚洲第一极品精品无码久久| 久久婷婷五月综合色奶水99啪| 国产成人久久777777| 亚洲欧美久久久久9999| 韩国免费A级毛片久久| 欧美成a人片免费看久久| 久久偷看各类wc女厕嘘嘘| 久久综合亚洲色HEZYO国产 | 久久精品a亚洲国产v高清不卡| 91亚洲国产成人久久精品网址| 久久精品国产亚洲AV香蕉| 久久高清一级毛片| 久久久久99精品成人片试看| 久久青青草视频| 精品欧美一区二区三区久久久| 久久久久久人妻无码| 国产69精品久久久久APP下载| 国产精品欧美亚洲韩国日本久久| 一本色道久久88—综合亚洲精品| 久久午夜综合久久| 久久99精品久久久久久水蜜桃| 99re久久精品国产首页2020| 国色天香久久久久久久小说| 久久综合久久伊人| 久久婷婷色香五月综合激情| 久久丝袜精品中文字幕| 国产高清国内精品福利99久久| 久久精品国产亚洲AV麻豆网站| 久久精品国产亚洲av麻豆蜜芽| 午夜精品久久久久9999高清| 久久无码人妻精品一区二区三区| 久久99精品九九九久久婷婷| 国产亚洲成人久久| 欧美久久综合九色综合| 亚洲国产精品狼友中文久久久| 亚洲国产成人精品女人久久久| 久久精品亚洲男人的天堂|