Results 1 to 17 of 17

Thread: What is Re-Entrant

  1. #1
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default What is Re-Entrant

    Some one please explain
    What is it not safe callin reentrant function from multiple thread ?

    reentrant - Describes a function which can be called simultaneously by multiple threads when each invocation of the function references unique data. Calling a reentrant function simultaneously with the same data is not safe, and such invocations should be serialized.
    We can't solve problems by using the same kind of thinking we used when we created them

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What is Re-Entrant

    In the following piece of code, both functions f and g are not reentrant.

    Qt Code:
    1. int g_var = 1;
    2.  
    3. int f ()
    4. {
    5. g_var = g_var + 2;
    6. return g_var;
    7. }
    8.  
    9. int g ()
    10. {
    11. return f () + 2;
    12. }
    To copy to clipboard, switch view to plain text mode 

    In the above code, f depends on a global variable g_var; thus, if two processes execute it and access to g_var concurrently, then the result varies depending on the timing of the execution. Hence, f is not reentrant. Neither is g; it calls f, which is not reentrant.

    Hope its clear

    Cheers

  3. #3
    Join Date
    Jan 2006
    Location
    Berlin, Germany
    Posts
    64
    Thanks
    1
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Re: What is Re-Entrant

    Quote Originally Posted by munna
    In the following piece of code, both functions f and g are not reentrant.

    Qt Code:
    1. int g_var = 1;
    2.  
    3. int f ()
    4. {
    5. g_var = g_var + 2;
    6. return g_var;
    7. }
    8.  
    9. int g ()
    10. {
    11. return f () + 2;
    12. }
    To copy to clipboard, switch view to plain text mode 

    In the above code, jk;hfd;hw4ejkbndohf;kjlhasdjkbweuofvjknsdklbvuioh; jklhsdf;uiohjenofuijnweuojvf nkjsdhbujreuihvuo wbelh;sljkgh849ohn AZUObhn vujorhguoghv nvuhiro;gorauighnr;ogeh

    Hope its clear

    Cheers
    Thats what it looks like to me :-)
    (My fault, though, I have a hard time following things like that)

    My understanding is (put REALLY simply) reentrant code is basically code that doesn't use global variables, is that sort of correct?

    Katrina

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What is Re-Entrant

    Quote Originally Posted by katrina
    My understanding is (put REALLY simply) reentrant code is basically code that doesn't use global variables, is that sort of correct?
    I guess that definition should be something like:
    "Reentrant code is a code that doesn't use shared resources without proper locking mechanism."

    Where "shared resource" can be:
    • globally accessible variable or data structure,
    • static variable,
    • environment variable,
    • file,
    • and probably many other things.

  5. #5
    Join Date
    Jan 2006
    Location
    Socorro, NM, USA
    Posts
    29
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: What is Re-Entrant

    From my point of view the term shared resource is misleading. Think of a static method variable:

    Qt Code:
    1. const int foo(void)
    2. {
    3. static int bar(0);
    4.  
    5. bar += 42;
    6. return bar;
    7. };
    To copy to clipboard, switch view to plain text mode 

    The variable bar is not shared among other methods but only by the method foo itself. Though, foo is not reentrant. I just wanted to clarify things.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What is Re-Entrant

    Quote Originally Posted by Thomas
    The variable bar is not shared among other methods but only by the method foo itself.
    One could say it's shared among all calls to that function. Anyway, you are right, static variables can cause a lot of troubles even in a single-threaded application.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: What is Re-Entrant

    As the name itself suggests, a "reentrant" function (or object or whatever) is a function (or object or whatever) which can be re-entered (entered again) when "there already is something inside" (like the flow). Hence reentrancy is important not only for multithreaded environments, but also for single threaded ones.

    Example 1:

    Qt Code:
    1. void funcA(int x){
    2. static int var = 7;
    3. operateOn(var);
    4. if(conditionMet) funcB(x-1);
    5. operateAgainOn(var);
    6. }
    7.  
    8. void funcB(int x){
    9. funcA(x);
    10. }
    11.  
    12. // A call to funcA() yields a call to funcB() which calls funcA()
    13. // again => funcA is RE-ENTERED as the same thread is
    14. // "inside" funcA() two times at once
    To copy to clipboard, switch view to plain text mode 

    Example 2: every recursive function.

  8. #8
    Join Date
    Jan 2006
    Location
    Berlin, Germany
    Posts
    64
    Thanks
    1
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Talking Re: What is Re-Entrant

    My grandmother had a bedroom in her house that was non-re-entrant. If you shut the door on your way out, it would automatically lock and you couldn't get back in without taking the door off the hinges or climbing through the window... I do wonder why she never just bought another door knob... (My dad had to take her door off a couple of times lol) oh well!

    Katrina

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: What is Re-Entrant

    That's more likely a deadlock Reentrancy is about having more than one entity operating on the same object (so reentrant room is when more than one person can fit there without spreading havoc).

  10. #10
    Join Date
    Jan 2006
    Location
    Socorro, NM, USA
    Posts
    29
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: What is Re-Entrant

    Quote Originally Posted by wysota
    That's more likely a deadlock Reentrancy is about having more than one entity operating on the same object (so reentrant room is when more than one person can fit there without spreading havoc).
    Hmm, I would say, reentrancy is a room with more than one door.

  11. #11
    Join Date
    Jan 2006
    Posts
    32
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: What is Re-Entrant

    Quote Originally Posted by Thomas
    Hmm, I would say, reentrancy is a room with more than one door.
    nice definition, I think it fits quite well

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: What is Re-Entrant

    Quote Originally Posted by Thomas
    Hmm, I would say, reentrancy is a room with more than one door.
    Hmmm... I wouldn't agree... That would be two entry points, not reentrancy. Better would be to say that reentrancy is a bedroom with more than one bed. Why? Because it is not about entering the function, but about behaviour inside it. For me a room with more than one door is an example of a locking mechanism, like a semaphore.

  13. #13
    Join Date
    Jan 2006
    Location
    Socorro, NM, USA
    Posts
    29
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: What is Re-Entrant

    Exactly, wysota.

    It depends on the point of view what the bedrooom and the bed are. If the bed is a kind of member in the method bedroom, the doors represent bedroom using threads. In this way, every bed would represent another member inside the bedroom.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: What is Re-Entrant

    The point is reentrancy is not about threads. An object may not be reentrant even in a single threaded environment.

    But let's leave that, we've come waaaaay offtopic (I like that smiley...)

  15. #15
    Join Date
    Jan 2006
    Location
    Socorro, NM, USA
    Posts
    29
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: What is Re-Entrant

    I know. It was meant as an example for other objects trying to call bedroom.

  16. #16
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What is Re-Entrant

    Guys,
    While I am going searching about this topic. I stumbled upon this link. Hope it will be useful
    http://publibn.boulder.ibm.com/doc_l..._safe_code.htm
    We can't solve problems by using the same kind of thinking we used when we created them

  17. #17
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Re: What is Re-Entrant

    It's a long time... Since the last post . But I would like to post the Two definitions

    According to the C++ Gui programming Book

    Thread-safe : A function is said to be thread-safe when it can safely be called from different threads simultaneously. If two thread-safe functions are called from different threads on the same shared data, the result is always defined. By extension, a class is said to be thread-safe when all of its functions can be called from different threads simultaneously without interfering with each other, even when operating on the same object.

    Reentrant : A class is reentrant if different instances of the class can be used simultaneously in different threads. However, accessing the same reentrant object in multiple threads simultaneously is not safe,and such accesses should be protected with a mutex. Reentrant classes are marked as such in the Qt reference documentation. Typically, any C++ class that doesn t reference global or otherwise shared data is reentrant. QObject is reentrant, but none of Qt s (3.x) QObject subclasses are reentrant.
    We can't solve problems by using the same kind of thinking we used when we created them

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.