-
Notifications
You must be signed in to change notification settings - Fork 51
Wait the thread to actually finish before the Thread destructor exits #445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…. Also use weak_ptr in ShutdownTask to prevent cyclic reference problem of shared_ptr. fixes hazelcast#429
|
Windows test FAILed. |
1 similar comment
|
Windows test FAILed. |
|
Linux test PASSed. |
1 similar comment
|
Linux test PASSed. |
|
Windows test PASSed. |
|
Linux test PASSed. |
| AbstractThread::~AbstractThread() { | ||
| if (started) { | ||
| int state = stateLatch->get(); | ||
| if (state == 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this if statement necessary. It looks like the second count is doing same thing as started boolean. I would remove the second count that tracks started.
And there seems to be still race between destructor and start method.
When both starts at the same time, it could be the case that destructor passes then thread starts.
To prevent that, ( I am asking because I am not sure about usages of abstractThread )
there needs to be a third state and started that we set on destructor so that start will be prevented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the Thread implementation to use state machine. The states are :
enum ThreadState {
UNSTARTED = 0,
STARTED = 1,
JOINED = 2,
CANCELLED = 3
};
It can go from UNSTARTED to STARTED, STARTED to JOINED or STARTED to CANCELLED states. Other transitions are not valid. When thread is cancelled, then the shutdown latch is waited.
enum ThreadState {
UNSTARTED = 0,
STARTED = 1,
JOINED = 2,
CANCELLED = 3
};
It can go from UNSTARTED to STARTED, STARTED to JOINED or STARTED to CANCELLED states. Other transitions are not valid. When thread is cancelled, then the shutdown latch is waited.
|
Windows test PASSed. |
|
Linux test PASSed. |
Wait the thread to actually finish before the Thread destructor exits. Also use weak_ptr in ShutdownTask to prevent cyclic reference problem of shared_ptr.
fixes #429