-
Notifications
You must be signed in to change notification settings - Fork 865
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
Remove global variable #1913
Remove global variable #1913
Conversation
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.
CI build failure. Not all places of use were updated.
Please build with -DENABLE_EXPERIMENTAL_BONDING=ON
to see more places.
2443cb3
to
473ad03
Compare
Thanks for the review, I missed that part. A new version was pushed with double check by grep 's_UDTUnited'. |
473ad03
to
544bbce
Compare
af8a2f5
to
89de092
Compare
Finally CI build success on all platforms. |
What exactly problem does this solve? |
Trying to avoid non-plain-old-data type global variables. The second commit is an example of what kind of problem does non-POD global variable have. And there are other kind of issues like exception handling. |
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.
@quink-black Could you please update the PR to the latest master (resolve conflicts)?
srtcore/core.cpp
Outdated
static void createInstance() | ||
{ | ||
s_UDTUnited = new CUDTUnited(); | ||
} |
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.
How would the CUDTUnited
object be destructed?
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.
I don't know how to delete the singleton automatically in a portable way.
CUDT::cleanup
is a good entry to trigger delete, then another global lock is needed to protect CUDT::startup() and CUDT::cleanup() called by multiple threads.
What's your suggestion?
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.
How about this:
static CUDTUnited *getInstance()
{
static CUDTUnited instance;
return &instance;
}
static void createInstance() {
getInstance();
}
CUDTUnited* srt::CUDT::uglobal()
{
pthread_once(&s_UDTUnitedOnce, createInstance);
return getInstance();
}
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.
Or just do cast and remove the createInstance()
srtcore/core.cpp
Outdated
CUDTUnited* CUDT::uglobal() | ||
{ | ||
static CUDTUnited instance; | ||
return &instance; | ||
} |
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.
To reduce dereferencing, this would be a more convenient function.
CUDTUnited* CUDT::uglobal() | |
{ | |
static CUDTUnited instance; | |
return &instance; | |
} | |
CUDTUnited& CUDT::uglobal() | |
{ | |
static CUDTUnited instance; | |
return instance; | |
} |
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.
I don't think there is performance difference between pointer and reference. They are compiled to the same machine code.
If the singleton needs to be deleted as the previous review comments, then delete a reference looks odd.
Static local variable has the benefits of lazy initialization and avoids the initialization order problem of global variables.
89de092
to
c6ee4d8
Compare
Hi @quink-black. Sorry for a notable pause in the review. I am only still thinking about If you still believe a pointer is better, then it might be worth marking a function with |
Good point! I have added a new commit, with a lot of small changes from pointer to reference. |
No description provided.