1

For instance if I'm using a class like this:

class Class
{
    static int f(int a, int b, int c) { return a + b + c; }
    int(*A)(int, int, int);
    int(*B)(int, int, int);
    Class() { A = f; B = f; }
};

I was told that static function will make a silly error if function A and B runs simultaneously, but I think there is no other special way to define those function pointers. Does it really make problems in multithread programmings? If it does, how should I change the code to prevent such accidents?

In short, if A and B runs simultaneously then will there be a different return value which is not expected?

user5876164
  • 471
  • 3
  • 15
  • The problem is only if you access shared data (writing from one thread while reading or writing from another). There is no shared data accessed by `f` so no problem. Of course, there would be a problem if you call `A(...)` from one thread while assigning `A` from another. – interjay Nov 13 '18 at 13:39
  • I don't think function pointers is the issue, as such. The issue will be that the resources are the same in both cases - so both threads will potentially write to the same data and you have a race condition. You can use mutual exclusion (for example) to avoid this - or structure your code so that the resources aren't shared. It's not really clear (to me) what problem you're trying to solve though. – Rags Nov 13 '18 at 13:40
  • Could you ask the person who told you about that "silly error" to elaborate what that alleged "silly error" actually is? "Silly error" isn't very descriptive, so it's difficult to be entirely sure we actually address the issue this person was trying to warn you about. – Max Vollmer Nov 13 '18 at 13:45
  • @MaxVollmer he told me that single static function should not run at a same time (wishing that you got what I mean). But looking at above comments, I guess this is okay to use! – user5876164 Nov 13 '18 at 13:52
  • @user5876164 - I would say, no, it's not remotely safe to use - if you're accessing resources concurrently. You have a race condition, and you have no idea what's going to happen. if by 'silly error' the person meant 'unpredictable, unrepeatable and potentially catastrophic behaviour' - then yes, I suppose you could go ahead! (It won't hurt the machine - but it can crash your program in odd ways, and you'll have a very hard time figuring what's going on.) – Rags Nov 13 '18 at 13:57
  • 1
    @Rags In the example given there is neither a race condition, nor are there any shared resources. Each thread has its own stack. `a`, `b` and `c` are distinct in each call. – Max Vollmer Nov 13 '18 at 14:03
  • *"he told me that single static function should not run at a same time"* But *why* not? – Max Vollmer Nov 13 '18 at 14:05
  • @MaxVollmer in this instance, true. I was assuming (perhaps incorrectly) that the point of having two threads running through a static function in a class would be to access static class data. I accept that this example doesn't actually do that (although might well be extended to in the future.) I may be guilty of looking beyond the mark. – Rags Nov 13 '18 at 14:07
  • 1
    @Rags Yeah, that would be a problem. That's why I am trying to probe OP for the reason that other person gave. The example is fine, but of course there are scenarios where you have race conditions. However that doesn't depend on the function being static or not, it depends on what data is being accessed by that function - and how. – Max Vollmer Nov 13 '18 at 14:09
  • (This question might be too broad as it is, because there are so many factors that change the answer from yes to no to yes to no.) – Max Vollmer Nov 13 '18 at 14:11
  • @MaxVollmer probably because of safety issues. I'm very confused with my short english and knowledge, and I don't think I can't be more specific in describing this. – user5876164 Nov 13 '18 at 14:11
  • @user5876164 I guess then you should remain on the cautious side and just assume it's unsafe until you have acquired the knowledge and experience to understand when it is safe. – Max Vollmer Nov 13 '18 at 14:12
  • Different language, but the concept is the same and the answers might be useful to you: [What if a static method is called from multiple threads?](https://stackoverflow.com/questions/3037637/c-sharp-what-if-a-static-method-is-called-from-multiple-threads) – Max Vollmer Nov 13 '18 at 14:13

0 Answers0