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

  • Thread starter ORF
  • Start date
In summary: Hello,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.
  • #1
ORF
170
18
Technology news on Phys.org
  • #2
I believe all information is stored in the object that std::bind returns
 
  • #3
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
 
  • #4
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
  • #5
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
 
  • #6
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
  • #7
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
 
  • #8
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
  • #9
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:

1. What is std::bind?

std::bind is a function template in the C++ standard library that allows for the creation of callable objects, such as functions or function objects, with predefined arguments.

2. Is std::bind reentrant?

No, std::bind is not reentrant. This means that it is not safe to call std::bind from multiple threads simultaneously, as it may lead to unexpected behavior or errors.

3. Can std::bind be used with member functions?

Yes, std::bind can be used with both member functions and non-member functions. However, when using it with member functions, an additional argument for the object instance must be provided.

4. How does std::bind differ from std::function?

While both std::bind and std::function allow for the creation of callable objects, they differ in their functionality. std::bind allows for the binding of arguments to a callable object, while std::function can store any callable object without binding any arguments.

5. Is std::bind part of the C++ standard library?

Yes, std::bind is part of the C++ standard library and is defined in the header file. It was introduced in the C++11 standard.

Similar threads

  • Programming and Computer Science
Replies
2
Views
649
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
812
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
11
Views
947
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
5K
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top