concurrency - c++ future call local variable -
when using c++ future call function, if define 2 future objects a,b
, call same function foo
= async(launch::async,foo); b = async(launch::async,foo);
is same running function twice?
foo() foo()
i.e. a
, b
each running private copy of foo
?
they use same function example shows.
void foo() { static int counter=0; cout<<counter++<<endl; return; } int main() { std::future<void> resulta(async(launch::async,foo)); resulta.get(); std::future<void> resultb(async(launch::async,foo)); resultb.get(); return 0; }
output:
0
1
this shows same static counter variable used bcz output not 0,0 0,1 (incremented goes foo)
hope helps,
Comments
Post a Comment