c++ - What are the consequences of having a static pointer to this -
i have class contains functions need run threads. proper way (form understand) have these functions declared static. use methods class need have instance class, create static variable initialized self in constructor. implications in efficiency , program logic?
class foo { private: foo* this_instance; foo() { this_instance=this; } void foobar() { ... } static void* bar() { if (this_instance==null) return 1; //throws not catched they? this_instance->foobar(); return 0; } }
not actual code make question clearer.
the application works , checked helgrind/memcheck , errors not related issue @ hand. i'm asking question because solutions seem workarounds, including one. others 1 mentioned doctor love, other using helper static method.
i wondering if approach result in epic failures @ point in time, reason unknown me , obvious other more experienced programmers.
you not need functions static use them in threads. bind instance functions or pass pointer, or use c++11 lambda.
if use raw threads have catch exceptions in thread - not propagate code started thread.
in c++11 can propagate exceptions, using current_exception
, rethrow_exception
. see here
edit
if have static pointer each type, can have 1 instance of it, yet code nothing prevent static pointer being reset. why bother having class instance in first place - surely pass in parameters? think it's cleaner have free functions work. if think it's not worth effort, it's code. co-workers think of design?
Comments
Post a Comment