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

            牽著老婆滿街逛

            嚴以律己,寬以待人. 三思而后行.
            GMail/GTalk: yanglinbo#google.com;
            MSN/Email: tx7do#yahoo.com.cn;
            QQ: 3 0 3 3 9 6 9 2 0 .

            libjingle翻譯之《Important Concepts(重要概念)之Threads(線程)》

            轉載自:http://blog.csdn.net/night_cat/article/details/3496797

            Threads(線程)

             

            libjingle 考慮到使用到此庫的應用程序的性能,libjingle內部支持多線程。其內組件使用12個全局線程:

             signaling thread  被用作創建底層(基礎)組件,

                例如:Session Management,Control,XMPP Messaging組件。

             worker thread  ( 有時稱作channel thread)用來集中處理p2p組件中的對象提交過來的大量資源,例如:數據流。之所以這樣用另外的線程單獨處理,是為了避免數據流阻塞或被XMPP/用戶界面組件阻塞。使用 worker thread的類包括ChannelManage,SocketMonitor,P2PTransportChannel 和 屬于Port類的對象。

            若起用worker thread,使之工作,在應用中必須創建一個Thread類對象,并把此對象當作SessionManager的構造函數的參數。(如果SessionManager類對象在創建時,沒有傳遞給它Thread對象,則SessionManager類將在內部創建一個線程,當作worker thread)CallClient::InitPhone示范了如何為底層組件(low-level components)創建一個worker thread方法。

             

            另外、libjingle提供了一個基類SignalThread。擴展此類可以讓一個擴展類對象存在于它自身代表的線程,此擴展類對象可以被實例化,啟動,單獨離開,結束時自釋放。更多信息請查看signalthread.h/cc

             

            注意:盡管libjingle支持多線程,但是只有幾個函數通過呼叫方線程的驗證來支持線程安全,并且極少函數做了線程鎖定。下面的片斷示范了在函數中如何安全地呼叫線程(或線程安全地被呼叫):

            // Check that being called from the channel (e.g., worker) thread.

            ASSERT(talk_base::Thread::Current() == channel_thread_);

            channel_thread_->Clear(this);

             

            libjingle中用到的所有線程,signaling threadworker thread,其它的一些線程,都是talk_base::Thread的對象(或子類的對象)。所有的Thread對象都被ThreadManager管理,當被請求時,ThreadManager會返回這些Thread對象。SessionManager被創建時通過調用ThreadManager::CurrentThread得到一個signal thread(當無worker thread 傳遞給SessionManager構造函數時,同時得到一個work thread)XmppPump類把當前線程當作它的signal thread來用(XmppPump uses the current thread for its signaling thread)。所以,應用程序必須為signal thread創建一個Thread對象(或其子類對象),并在SessionManager對象創建之前或在XmppPump工作之前,把此對象放進ThreadManager的線程池里。(Signing In to a Server(登錄服務器) 有示例)有兩種方法創建一個Thread對象:

            AutoThread    這種方式就是libjingleThread對象包裝一個操作系統中的線程,并把它當作ThreadManager線程池里的當前線程(當然,Thread::CurrentThread()被調用時,此線程會被提取出來)。

            Thread   這種方式將創建一個新線程并用Thread類包裝,比較典型就是的創建worker thread。使此線程發生作用,應用程序必須新創建一個Thread對象,調用ThreadManager::Add()ThreadManager::SetCurrent()把它丟進線程池里,并且調用Run()使之在阻塞狀態下運行或調用Start()使之處于監聽狀態。

             

            線程為對象間或對象內部的消息溝通提供了“管道”()。例如:SocketManager可以通過其它線程向自己發送銷毀一個套接字的消息,或當鏈接候選被產生時向SessionManager發送消息。Thread繼承自MessageQueue,所以Thread的對象具有了SendPost,和一些同步或異步發送消息的函數。如果要使一個對象能夠接收到MessageQueue送出的消息,那么此對象必須繼承和實現MessageHandlerMessageHandler定義了一個OnMessage函數,此函數在MessageQueue送出消息時被調用,用來接收MessageQueue送出的消息。

             

            你可以通過任何線程向繼承自talk_base::MessageHandler的任何對象發送消息。盡管能夠做到,如果你發出的消息是為了集中處理大量的數據,應用程序應該通過worker thread。調用SessionManager::worker_thread()可以得到worker thread的句柄。

            調用Session::Manager::signaling_thread()可以得到 signaling thrread的句柄。

             

            對象使用一個指定的線程有如下幾種方式:

                   對象要求一個線程指針作輸入參數,并儲存這個指針。

                   對象在創建時取得當前線程(構造函數中調用ThreadManager::CurrentThread()取得),把取得的線程存進對象內部成員變量引用它,一般應用于獲取特定的線程。(it can assume that the current thread when it is created (accessed byThreadManager::CurrentThread in its constructor) is a particular thread and cache a member pointer to it

                   對象調用SessionManger::signal_thread()  SessionManager::worker_thread()獲取線程。

                   以上三種方法,libjingle均有用到。

             

            因為一個對象可以被任意線程使用,對象可能需要驗證當前調用是來自哪個線程的方法。應用可以調用Thread::Current()得到當前線程的句柄,然后與對象內部保存線程的數據成員進行比較,此數據成員的值可以是從SessionManager中暴露在外面的線程,或是對象在創建時通過構造函數傳進去的初始化值。

            這是一個對象通過其它線程調用自身函數時而廣范使用的范例:

            // Note that worker_thread_ is not initialized until someone 
            // calls PseudoTcpChannel::Connect
            // Also note that this method *is* thread-safe. 
            bool PseudoTcpChannel::Connect(const std::string& channel_name) {
              ASSERT(signal_thread_->IsCurrent());
              CritScope lock(&cs_);
                if (channel_)
                  return false;
                ASSERT(session_ != NULL);
                worker_thread_ = session_->session_manager()->worker_thread();
            ...
            }
             
            void PseudoTcpChannel::SomeFunction(){
              ...
              // Post a message to yourself over the worker thread.
              worker_thread_->Post(this, MSG_PING); // <- Goes in here....
              ...
            }
             
            // Handle queued requests.
            void PseudoTcpChannel::OnMessage(Message *pmsg) {
              if (pmsg->message_id == MSG_SORT)
                OnSort();
              else if (pmsg->message_id == MSG_PING) // -> And comes out here!
                // Check that we're in the worker thread before proceding.
                ASSERT(worker_thread_->IsCurrent());
                OnPing();
              else if (pmsg->message_id == MSG_ALLOCATE)
                OnAllocate();
              else
                assert(false);
            }



            posted on 2013-09-01 16:29 楊粼波 閱讀(483) 評論(0)  編輯 收藏 引用

            一本久久a久久精品vr综合| 91精品国产综合久久精品| 武侠古典久久婷婷狼人伊人| 夜夜亚洲天天久久| 18禁黄久久久AAA片| 国产精品久久久久久吹潮| 国产午夜精品久久久久九九电影| 性做久久久久久免费观看| 色综合久久综合中文综合网| 天天久久狠狠色综合| 久久精品人人做人人爽电影| 亚洲午夜久久久精品影院| 久久免费看黄a级毛片| 久久亚洲欧美日本精品| 亚洲国产精品无码久久久不卡| 亚洲午夜久久影院| 久久青青草原亚洲av无码app| 国产精品九九久久免费视频 | 9999国产精品欧美久久久久久| 久久无码国产| 久久青草国产精品一区| 麻豆AV一区二区三区久久| 青青草国产97免久久费观看| 99热成人精品免费久久| 国产精品久久永久免费| 伊人久久综合成人网| 日韩精品久久久久久久电影| 狠狠色综合网站久久久久久久| 九九久久99综合一区二区| 亚洲AV成人无码久久精品老人| 久久强奷乱码老熟女网站| 亚洲人成网站999久久久综合 | 久久精品一本到99热免费| 无码乱码观看精品久久| 人人狠狠综合久久亚洲高清| 97热久久免费频精品99| 狠狠色丁香久久综合婷婷| 99久久成人国产精品免费| 精品久久久久香蕉网| 国产精品久久毛片完整版| 久久发布国产伦子伦精品 |