0

I want to know if thread1, calls a class function via passed object 'obj->fun()' and that object is deleted in the background by some other thread say thread2 what happens to function execution by thread1.

Example:

ClassA {
  int functionA() {
    ...condition(started_execution);
    int a=0;
    a++;
    printf(....a);
    return a;
  }
};
void startExecution(void *arg) {
/*casting code object A*/
  A->functionA();
}

int main() {
  ClassA *A = new ClassA();
  pthread_create(....,startExecution,(void *)A);
  ....wait_for(started_execution);
    delete A;//just want to know the behaviour and not join method.
}

Question: In the above scenario, A->functionA calls function functionA. If the function is executing, what will the impact of delete A on function execution since object A invoked it? functionA is not working on shared data?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 3
    For starters, please consider using [`std::thread`](http://en.cppreference.com/w/cpp/thread/thread) instead. And as for your problem, what you want to do is *not* okay, you're basically pulling the rug out from under your own feet while shooting yourself in your feet. It's bad. – Some programmer dude Oct 03 '18 at 09:52
  • 1
    If `ClassA` has any non-static data members used by `functionA`, or if `functionA` is virtual, you definitely have a problem. If neither of those things are true, you might be ok, but you also don't need `ClassA` or instance `*A` to exist in the first place – Useless Oct 03 '18 at 09:53
  • if you are starting to throw a grenade and it explodes in your hand, will your be able to finish throw after explosion (assuming you was not damaged by explosion) ? – Andrew Kashpur Oct 03 '18 at 09:57
  • In practice, and since you use pthread, I suppose you compile with gcc or clang (MSVC empty object ABI treatment is very different), A is empty so `functionA` implied object argument will not be accessed. So your code will work as you expect it. But it is undefined behavior according to the c++ standard: you cannot call a member function of an object after it has been destroyed. – Oliv Oct 03 '18 at 10:01
  • 1
    We can have a fun academic argument about what _would_ happen here as a curiosity (answer will almost certainly depend on compiler, phase of the moon, etc.). However, you _should_ **never ever do this**. – JMAA Oct 03 '18 at 10:18

1 Answers1

2

If the function is executing, what will the impact of delete A on function execution since object A invoked it?

If the executing function uses this in any way, the impact would be undefined behaviour. By using this I mean access any data member or base sub object, call any member function, indirecting this or passing this to some function that would do any of those things.

Your functionA doesn't appear to use this in any way after setting the condition variable, so there would be no impact whatsoever - assuming the condition variable access itself is properly synchronised.

However, it's not a very good idea to do this, since it is not visible in the definition of functionA that no member must be accessed. It would be easy for a programmer to not follow that requirement when changing the function later.

As far as I can tell, this situation is analogous to the case of delete this; which is considered to conform to standard, but is potentially dangerous: Is delete this allowed?

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Yeah - it's one of those things that nag at you, even if they work fine. Every time something strange happens somewhere, you feel the need to go back and check that class function hasn't somehow gained a dead object reference from somewhere and so started to act up. Development is difficult enough as it is without avoidable worries:) – Martin James Oct 03 '18 at 11:39