Skip to content

Commit 94a8daf

Browse files
committed
Proper termination of detached thread
Wait for proper termination of detached thread, and thereby prevent ZMQ socket problem.
1 parent e321b7a commit 94a8daf

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

run/O2PrimaryServerDevice.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ class O2PrimaryServerDevice final : public FairMQDevice
153153
}
154154
std::unique_ptr<FairMQMessage> request(channel.NewSimpleMessage(-1));
155155
while (mState != O2PrimaryServerState::Stopped) {
156-
LOG(INFO) << "READY TO RECEIVE INFO REQUEST";
157-
if (channel.Receive(request) > 0) {
156+
int timeout = 100; // 100ms --> so as not to block and allow for proper termination of this thread
157+
if (channel.Receive(request, timeout) > 0) {
158158
LOG(INFO) << "INFO REQUEST RECEIVED";
159159
if (*(int*)(request->GetData()) == (int)O2PrimaryServerInfoRequest::Status) {
160160
LOG(INFO) << "Received status request";
@@ -172,6 +172,7 @@ class O2PrimaryServerDevice final : public FairMQDevice
172172
}
173173
}
174174
}
175+
mInfoThreadStopped = true;
175176
};
176177
threads.push_back(std::thread(lambda));
177178
threads.back().detach();
@@ -360,7 +361,11 @@ class O2PrimaryServerDevice final : public FairMQDevice
360361
timer.Stop();
361362
auto time = timer.CpuTime();
362363
LOG(INFO) << "COND-RUN TOOK " << time << " s";
363-
// return mState.load() != O2PrimaryServerState::Stopped; // will be taken down by external driver
364+
}
365+
// wait for info thread
366+
while (!mInfoThreadStopped) {
367+
LOG(INFO) << "Waiting info thread";
368+
sleep(1);
364369
}
365370
}
366371

@@ -553,6 +558,7 @@ class O2PrimaryServerDevice final : public FairMQDevice
553558

554559
std::atomic<O2PrimaryServerState> mState{O2PrimaryServerState::Initializing};
555560
std::atomic<int> mWaitingControlInput{0};
561+
std::atomic<bool> mInfoThreadStopped{false};
556562

557563
bool mAsService = false;
558564
};

0 commit comments

Comments
 (0)