Initializizng main thread in linux

75 views
Skip to first unread message

Peter Arnold

unread,
May 11, 2022, 12:54:37 PM5/11/22
to discuss-webrtc
Hi everyone! 

I seen peerconnection client in linux and in windows, m84 used as reference.

In main function for windows initializing of main thread for network and threads interaction becomes with this lines
 

rtc::WinsockInitializer winsock_init;
rtc::Win32SocketServer w32_ss;
rtc::Win32Thread w32_thread(&w32_ss);
rtc::InitializeSSL();
rtc::CleanupSSL();

Win32SocketServer::WakeUp - transfering messages to inside message queue of the windows, so after that all processing are happened,

I need to implement analogue functionality in linux, I saw reimplementattion of Physicalsocket in linux, for receiveing messages. But I can't understand full approach of that, in windows, I created thread manually and need to process data from it in current thread 

I post data with this lines 


thread->PostTask(RTC_FROM_HERE, [wobs = std::weak_ptr<Observer>(obs), notifier]() {                                if (auto observer = wobs.lock()) { notifier(observer);                                }                            });

And I need to process message in my main thread,  class with multithreading are represented below, I called init method of this class first , After I create a list of threads for processing, my message processed with thread name ( "plugin-client", or "signaling-service"  ) Can't say exact which of them, I make it clear later, for now it doesn't matter.

So After I'm calling thread->PostTask, but with first 5 lines of identification commented is just stay and not executed, but with first 5 lines of thread initialization for windows it worked correct. Pls can someone tell me, how to correct initialize main signaling server thread under linux, also for windows it's not blokcing, I need the same approach, can someone show me a simple example of such initialization.

_threadProvider = std::make_unique<vi::ThreadProvider>();
           _threadProvider->init();     
       _threadProvider->create({ "signaling-service", "plugin-client", "message-transport", "capture-session" });
 

class ThreadProvider    {    
       public:        ThreadProvider();
       ~ThreadProvider();
       void init();
       void destroy();
       void create(const std::list<std::string>& threadNames);
       rtc::Thread* thread(const std::string& name);
   private:        ThreadProvider(const ThreadProvider&) = delete;
       ThreadProvider(ThreadProvider&&) = delete;
       ThreadProvider& operator=(const ThreadProvider&) = delete;
   public:        void stopAll();
   private:        std::unordered_map<std::string, std::shared_ptr<rtc::Thread>> _threadsMap;                std::mutex _mutex;
       rtc::Thread* _mainThread = nullptr;
       std::atomic_bool _inited;
       std::atomic_bool _destroy; 
    };

And here is main logick
ThreadProvider::ThreadProvider() : _destroy(true), _inited(false)    {
   }

ThreadProvider::~ThreadProvider()    {     
  DLOG("~ThreadProvider()");       
 if (!_destroy) {          
  stopAll();    
    }    
}

void ThreadProvider::init()    {    
    std::lock_guard<std::mutex> lock(_mutex);
       _mainThread = rtc::ThreadManager::Instance()->CurrentThread();
       _inited = true; 
   }

void ThreadProvider::destroy()    {      
  _destroy = true;   
 }

void ThreadProvider::create(const std::list<std::string>& threadNames)    {        std::lock_guard<std::mutex> lock(_mutex);
       if (!_inited) {      
      DLOG("_inited == false");      
      return;  
      }

       for (const auto& name : threadNames) {     
       _threadsMap[name] = rtc::Thread::Create();           
_threadsMap[name]->SetName(name, nullptr);  
          _threadsMap[name]->Start();        } 
   }

void ThreadProvider::stopAll()    {       
 std::lock_guard<std::mutex> lock(_mutex);
       for (const auto& thread : _threadsMap) {  
          thread.second->Stop();    
    }        
_threadsMap.clear();
       _destroy = true;    
}

rtc::Thread* ThreadProvider::thread(const std::string& name)    {        std::lock_guard<std::mutex> lock(_mutex);
       if (name == "main") {            return _mainThread;     
  }        else if (_threadsMap.find(name) != _threadsMap.end()) {     
      return _threadsMap[name].get();     
   }
       return nullptr; 
   }

Reply all
Reply to author
Forward
0 new messages