多线程的程序是比较常见的,更何况是网络程序。随着多核CPU的发展,想提高程序的性能,只能靠多线程工作了。下面就带你看看第二人生是怎么创建多线程工作的,每个线程又是做什么样的工作。由于线程类是主动类,线程的工作结构就是程序的工作结构了,写一个好的程序,往往就是线程类的结构安排,也就是整个程序的架构实现。并且每个线程之间的交互,都需要小心地进行着,一着不慎就全盘皆输,造成多个线程之间死锁的出现,因此写多线程程序,总要如履薄冰,小心翼翼。 
  
  
  
    
    #001 bool LLAppViewer::initThreads() 
  
  
  
    
    #002 { 
  
  
  
    
    定义是否允许跟内存。 
  
  
  
    
    #003 #if MEM_TRACK_MEM 
  
  
  
    
    #004  
   
    
           static const bool enable_threads = false;
    
  
  
    
    #005 #else 
  
  
  
    
    #006  
   
    
           static const bool enable_threads = true;
    
  
  
    
    #007 #endif 
  
  
  
    
    创建虚拟文件系统线程。 
  
  
  
    
    #008  
   
    
           LLVFSThread::initClass(enable_threads && true);
    
  
  
    
    创建本地文件系统线程。 
  
  
  
    
    #009  
   
    
           LLLFSThread::initClass(enable_threads && true);
    
  
  
    
    #010  
  
  
  
    
    创建图像解码线程。 
  
  
  
    
    #011  
   
    
           // Image decoding
    
  
  
    
    #012  
   
    
           LLAppViewer::sImageDecodeThread = new LLWorkerThread("ImageDecode", enable_threads && true);
    
  
  
    
    创建纹理缓存线程。 
  
  
  
    
    #013  
   
    
           LLAppViewer::sTextureCache = new LLTextureCache(enable_threads && true);
    
  
  
    
    创建获取纹理线程。 
  
  
  
    
    #014  
   
    
           LLAppViewer::sTextureFetch = new LLTextureFetch(LLAppViewer::getTextureCache(), enable_threads && false);
    
  
  
    
    #015  
   
    
           LLImageWorker::initClass(LLAppViewer::getImageDecodeThread());
    
  
  
    
    #016  
   
    
           LLImageJ2C::openDSO();
    
  
  
    
    #017  
  
  
  
    
    #018  
   
    
           // *FIX: no error handling here!
    
  
  
    
    #019  
   
    
           return true;
    
  
  
    
    #020 } 
  
  
  
    
    在程序创建时,先创建了一个界面线程,再加上面创建的线程,相互协作地工作,实现了更好的运行性能,并且每部份逻辑上比较独立分离,写起程序来也比较方便,容易理解程序的工作方式。 
  
  
    
                
  

