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

  • Thread starter Thread starter ORF
  • Start date Start date
AI Thread Summary
The discussion centers on the reentrancy of std::bind in C++. The initial inquiry questions whether std::bind is reentrant, with concerns about how bound values are stored and retrieved. It is clarified that std::bind does store bound values in the object it returns, but retrieving these values is not straightforward. The conversation reveals that reentrancy refers to the ability to call std::bind again while it is executing, particularly in multi-threaded contexts. It is noted that std::bind does not call itself, which supports its reentrant nature. The discussion also touches on the implementation specifics in Visual C++ 2017, mentioning that optimization can render std::bind calls effectively non-existent in terms of stack usage. Finally, the importance of understanding reentrancy in relation to global variables and non-reentrant functions is highlighted, suggesting further research for clarity.
ORF
Messages
169
Reaction score
18
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 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 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 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:
Back
Top