Hi. We have weird trouble related to the multi-threading.
Does anyone have ideas how to resolve or avoid?
Sample Code
// sample.hpp
class Sample {
public:
Sample();
~Sample();
void Init();
void Dealloc();
void OnEvent();
private:
std::mutex sample_mutex_;
int counter = 1;
}
// sample.cpp
#include "sample.hpp"
Sample::Sample() {}
Sample::~Sample() {}
void Sample::Init() {
std::async([]() {
std::this_thread::sleep_for(std::chrono::seconds(2));
this->OnEvent();
});
}
void Sample::Dealloc() {
std::lock_guard<std::mutex> lock(sample_mutex_);
counter = 0;
}
void Sample::OnEvent {
// called from another thread
std::lock_guard<std::mutex> lock(sample_mutex_);
counter += 1;
}
// obj_sample.h
@interface ObjSample : NSObject
- (id _Nonnull)init;
@property(nonatomic, readonly) std::shared_ptr<Sample> sample;
@end
// obj_sample.mm
@implementation ObjSample
- (id _Nonnull)init{
if (self = [super init]) {
_sample = std::make_shared<Sample>();
_sample->Init();
}
return self;
}
- (void)dealloc {
sample->Dealloc();
}
@end
What happens
the deadlock happens.
according to the debug navigator with the Xcode, we figured out 2 facts below.
OnEvent
does not end although it looks completed.
void Sample::OnEvent {
// called from another thread
std::lock_guard<std::mutex> lock(sample_mutex_);
counter += 1;
} // <= Thread 35: stop here
Sample::Dealloc
is run on the same thread ofOnEvent
.
// debug navigator
Thread 35
2 Sample::Dealloc <- this is weird.
3 Sample::OnEvent
We guess they causes the deadlock.
probability
- less than 10%
Environment
- MacOS: 14.3.1(Apple M1)
- Xcode: 15.3
- iOS Simulator: 17.0.1