0

I have a class that contains functions that need to run as threads. The proper way to do this (form what I understand) is have these functions declared as static. To use methods from this class I need a to have an instance to that class, so I create a static variable that is initialized to self in the constructor. What are the implications in efficiency and program logic?

class Foo
{
   private: Foo* this_instance;
   Foo()
   {
      this_instance=this;
   }

   void FooBar()
   {
   ...
   }

   static void* Bar()
   {
   if (this_instance==NULL) return 1; //throws are not catched are they?
   this_instance->FooBar();
   return 0;
   }
}

Not actual code but to make my question clearer.

The application actually works and I checked it with helgrind/memcheck and the errors are not related to the issue at hand. I'm asking this question because all solutions seem like workarounds, including this one. Others are like the one mentioned by doctor love, other using helper static method.

I am wondering if my approach would result in epic failures at some point in time, for some reason unknown to me and obvious to other more experienced programmers.

LucianMLI
  • 183
  • 1
  • 11
  • 2
    The static variable is not a good approach - what if you are using multiple objects of this class at the same time ? Another approach is to pass `this` as a parameter of your thread entry point. – Nbr44 Jul 31 '13 at 09:13
  • @doctorlove the class is more like a server that listens for some events. Despite the fact that I didn't add protection against multiple instances (like this_instance check in constructor) I do use it as a single instance. The compiler on my system does not use c++11 (gcc 4.3.4), it's the default compiler on the server where the application will be deployed. – LucianMLI Jul 31 '13 at 10:23
  • The usual solution is as suggested by @Nbr44. Pass 'this' in as the one 'arg' parameter, ('The thread is created executing start_routine with arg as its sole argument'), to the static/free function and, from there, cast 'arg' to 'this' and call some 'run' method on it. No static vars required and you can have multiple instances if you wish, no problem. – Martin James Jul 31 '13 at 11:32

1 Answers1

0

You do not need functions to be static to use them in threads. You could bind instance functions or pass the this pointer, or use C++11 with a lambda.
If you use raw threads you will have to catch exceptions in the thread - they will not propagate to the code that started the thread.
In C++11 you can propagate the exceptions, using current_exception and rethrow_exception. See here


EDIT

If you have a static pointer for each type, you can only have one instance of it, yet your code does nothing to prevent the static pointer being reset. Why bother having a class instance in the first place - surely just pass in the parameters? I think it's cleaner to have free functions to do the work. If you think it's not worth the effort, it's your code. What do your co-workers think of your design?

Community
  • 1
  • 1
doctorlove
  • 18,872
  • 2
  • 46
  • 62
  • The static method approach is the solution I got reading answers here on stackoverflow. Exception handling is not an issue here, I have a in thread try-catch and a monitor thread(joinable) that gets the results from the other threads(detached) when they finish. c++11 is not available on the target machine. – LucianMLI Jul 31 '13 at 10:30
  • @LucianMLI then use boost bind or consider passing the this pointer as a parameter – doctorlove Jul 31 '13 at 10:55
  • You still didn't say what are the disadvantages/dangers of using this approach to make the changes worth the consuming the time. – LucianMLI Jul 31 '13 at 11:00
  • You have a point, but since instance check can easily be done in the constructor, and since in this particular case, my classes have only one instance each (I have two similar server like classes) there is no point in creating structures to pass both this pointer and argument. As for my coworkers, they only code PHP, I'm entrusted with creating a application that cannot be written in PHP (memory buffer stuff). So as long as it's faster (and it is much faster) and doesn't crash it's ok for them. – LucianMLI Jul 31 '13 at 12:36