Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-04 05:18:46

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