Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:07

0001 #include "TClass.h"
0002 #include "TThread.h"
0003 #include "TObject.h"
0004 #include "TVirtualStreamerInfo.h"
0005 #include <thread>
0006 #include <memory>
0007 #include <atomic>
0008 #include <cassert>
0009 #include <iostream>
0010 
0011 void printHelp(const char* iName, int iDefaultNThreads)
0012 {
0013   std::cout << iName <<" [number of threads] \n\n"
0014             <<"If no arguments are given "<<iDefaultNThreads<<" threads will be used"<<std::endl;
0015 }
0016 
0017 int parseOptionsForNumberOfThreads(int argc, char** argv)
0018 {
0019   constexpr int kDefaultNThreads = 4;
0020   int returnValue = kDefaultNThreads;
0021   if( argc == 2 ) {
0022     if(strcmp("-h",argv[1]) ==0) {
0023       printHelp(argv[0],kDefaultNThreads);
0024       exit( 0 );
0025     }
0026     
0027     returnValue = atoi(argv[1]);
0028  }
0029   
0030   if( argc > 2) {
0031     printHelp(argv[0],kDefaultNThreads);
0032     exit(1);
0033   }
0034   return returnValue ;
0035 }
0036 
0037 int main(int argc, char** argv)
0038 {
0039   const int kNThreads = parseOptionsForNumberOfThreads(argc,argv);
0040   
0041   std::atomic<bool> canStart{false};
0042   std::vector<std::thread> threads;
0043   
0044   std::atomic<int> classWasGotten{0};
0045   std::atomic<int> firstMethodGotten{0};
0046   
0047   TThread::Initialize();
0048   //When threading, also have to keep ROOT from logging all TObjects into a list
0049   TObject::SetObjectStat(false);
0050 
0051   //Have to avoid having Streamers modify themselves after they have been used
0052   TVirtualStreamerInfo::Optimize(false);
0053   
0054   
0055   for(int i=0; i<kNThreads; ++i) {
0056     threads.emplace_back([&canStart,&classWasGotten,&firstMethodGotten]() {
0057         static thread_local TThread guard;
0058         ++classWasGotten;
0059         ++firstMethodGotten;
0060         while(not canStart) {}
0061         auto thingClass = TClass::GetClass("edmtest::Simple");
0062         --classWasGotten;
0063         while(classWasGotten !=0) {}
0064         
0065         TMethod* method = thingClass->GetMethodWithPrototype("id","",true /*is const*/, ROOT::kConversionMatch);
0066         --firstMethodGotten;
0067         while(firstMethodGotten !=0) {}
0068         TMethod* method2 = thingClass->GetMethodWithPrototype("operator=","edmtest::Simple const&",false /*is const*/, ROOT::kConversionMatch);
0069         
0070         assert(nullptr != method);
0071         assert(nullptr != method2);
0072       });
0073   }
0074   canStart = true;
0075   
0076   for(auto& thread: threads) {
0077     thread.join();
0078   }
0079   
0080   return 0;
0081 }