Is std::bind Reentrant? A Brief Overview of Reentrancy in C++ Functions

  • Context: C/C++ 
  • Thread starter Thread starter ORF
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around the reentrancy of std::bind in C++, exploring whether it can be safely called in concurrent contexts or during its own execution. Participants examine the implications of bound arguments and the behavior of std::bind in various scenarios, including recursion and multi-threading.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Some participants question whether std::bind is reentrant, suggesting that bound values must be stored somewhere.
  • One participant asserts that all information is stored in the object returned by std::bind.
  • Another participant expresses uncertainty about how to retrieve bound arguments from the returned object.
  • A participant recalls past debates regarding the recursive use of std::bind and its unclear consequences.
  • Some participants discuss the implications of not being able to retrieve bound arguments and whether this affects reentrancy.
  • It is noted that std::bind does not call itself, which may contribute to its reentrancy in multi-threaded contexts.
  • Concerns are raised about the use of global variables and non-reentrant functions in determining the reentrancy of a function.

Areas of Agreement / Disagreement

Participants express differing views on the reentrancy of std::bind, with some suggesting it is reentrant under certain conditions, while others remain uncertain about the implications of bound arguments and the behavior of std::bind in various contexts. The discussion does not reach a consensus.

Contextual Notes

Participants mention that the implementation details of std::bind may vary, particularly in relation to specific compilers like Visual C++ 2017. There are also references to the need for further testing and analysis to determine reentrancy based on the use of global variables and other factors.

ORF
Messages
169
Reaction score
19
Technology news on Phys.org
I believe all information is stored in the object that std::bind returns
 
Hello,

willem2 said:
I believe all information is stored in the object that std::bind returns

I am a kind of noob... how can you retrieve the bound arguments from the returned object?
http://www.cplusplus.com/reference/functional/bind/

Thank you for your time.

Regards,
ORF
 
Can we go back a bit? What are you trying to accomplish? Are you using recursion?

I do know there was some debate about whether or not std::bind can be called recursively. You can do it, of course, but the consequences were not clear a couple of years back. I remember a github entry for std::bind on the subject.
 
  • Like
Likes   Reactions: QuantumQuest
Hello,

No, I'm not using recursion.

I was told that std::bind is not reentrant because the bound (copied) arguments may not be retrievable.

I didn't find any information about how to retrieve the bound arguments, and that is my question: is std::bind reentrant? (or, can you retrieve the bound arguments somehow?)

Thank you for your time.

Regards,
ORF
 
ORF said:
I am a kind of noob... how can you retrieve the bound arguments from the returned object?
Some of the stuff isn't stored at all. The implementation of this isn't a part of the standard BTW, this is for visual c++ 2017.
Room is reserved on the stack for 8 bytes for a function pointer and 8 bytes for each argument. Only the arguments that are given a value are stored here. The compiler seems to know the rest. Note that it isn't allowed to change the order of the paramaters, or which ones are bound to a value
C:
auto f1 = std::bind(myfunc, _1, _2, 3)
f1 = std::bind(myfunc, _2, _1, 3)   //ERROR
f1 = std::bind(myfunc, _1, 2, 3)   //ERROR
f1 = std::bind(myfunc, _1, _2, 4)  //Only this is ok.
mentor add code tags
If I turn on optimization, the entire std::bind call is optimized to nothing. It still reserves room for f1 on the stack, but it isn't used ever.
That should be pretty reentrant.
 
Last edited by a moderator:
  • Like
Likes   Reactions: QuantumQuest
Hello,

willem2 said:
That should be pretty reentrant.

but, if once it is initialized, you can not know the bound arguments... that is also reentrant? (probably I have a misconception of what reentrancy is)

Thank you very much for your time.

Regards,
ORF
 
ORF said:
but, if once it is initialized, you can not know the bound arguments... that is also reentrant? (probably I have a misconception of what reentrancy is)

Reentrant is if it is OK to call std::bind again while it is still executting. This could happen in a multi-threaded program or if std::bind somehow calls itself.
The first one should be ok because each of the calls only works on its local data on the stack. std::bind calling itself shouldn't happen because std::bind doesn't call anything. Even if you try to pass std::bind to itself, it won't call itself.
 
  • Like
Likes   Reactions: QuantumQuest and ORF
Hello,

willem2 said:
Reentrant is if it is OK to call std::bind again while it is still executting. This could happen in a multi-threaded program or if std::bind somehow calls itself.
The first one should be ok because each of the calls only works on its local data on the stack. std::bind calling itself shouldn't happen because std::bind doesn't call anything. Even if you try to pass std::bind to itself, it won't call itself.

Thank you very much, that was the answer I needed! :D

Cheers!
ORF
 
  • #10
ORF said:
but, if once it is initialized, you can not know the bound arguments... that is also reentrant? (probably I have a misconception of what reentrancy is)

In addition to what willem2 said in post #8 in order to decide for a function if it is reentrant or not you can test it and analyze its behavior, regarding global variables (if they are used) , call of non-reentrant functions and self code modification. You can google these things in order to see and learn the details and / or post here for further help. Also, C++ documentation leaves many things to implementation(s). Besides the links you gave in previous posts it might be of help to take also a look here.
 
Last edited:

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K