ndmspc  v1.2.0-0.1.rc3
NThreadData.cxx
1 #include <iostream>
2 #include <vector>
3 #include <thread>
4 #include <TString.h>
5 #include "NLogger.h"
6 #include "NThreadData.h"
7 
9 ClassImp(Ndmspc::NThreadData);
11 namespace Ndmspc {
12 std::mutex NThreadData::fSharedMutex;
13 NThreadData::NThreadData() : TObject(), fItemCount(0), fCoordSum(0), fThreadId(), fIdSet(false), fAssignedIndex(0)
14 {
18 }
19 
21 {
25 }
26 void NThreadData::Process(const std::vector<int> & coords)
27 {
28 
29  NLogTrace("Processing coordinates in thread %llu",
30  (unsigned long long)std::hash<std::thread::id>{}(std::this_thread::get_id()));
31  // Use renamed members with lowercase 'f'
32  if (!fIdSet) {
33  fThreadId = std::this_thread::get_id();
34  fIdSet = true;
35  }
36  fItemCount++;
37  for (int val : coords) {
38  fCoordSum += val;
39  }
40  std::this_thread::sleep_for(std::chrono::milliseconds(500));
41 }
42 
43 void NThreadData::Print(Option_t * /*option*/) const
44 {
45  NLogTrace("NThreadData [Index: %zu, ThreadId: %llu, Items: %lld, Sum: %lld]", fAssignedIndex,
46  (unsigned long long)std::hash<std::thread::id>{}(fThreadId), fItemCount, fCoordSum);
47 }
48 
49 } // namespace Ndmspc
Thread-local data object for NDMSPC processing.
Definition: NThreadData.h:21
bool fIdSet
Flag to indicate if the thread ID is set.
Definition: NThreadData.h:139
size_t fAssignedIndex
Index assigned to this object.
Definition: NThreadData.h:140
virtual ~NThreadData()
Virtual destructor.
Definition: NThreadData.cxx:20
long long fCoordSum
Sum of coordinates.
Definition: NThreadData.h:137
virtual void Print(Option_t *option="") const
Print method override.
Definition: NThreadData.cxx:43
std::thread::id fThreadId
Thread ID.
Definition: NThreadData.h:138
virtual void Process(const std::vector< int > &coords)
Process method for thread data.
Definition: NThreadData.cxx:26
NThreadData()
Default constructor.
Definition: NThreadData.cxx:13
long long fItemCount
Number of items processed.
Definition: NThreadData.h:136
static std::mutex fSharedMutex
Shared mutex for thread-safe operations.
Definition: NThreadData.h:98