# Retrieve a value within a do block

**URL:** <https://discourse.julialang.org/t/retrieve-a-value-within-a-do-block/131121>\
**Category:** General Usage\
**Tags:** question, jump\
**Created:** [July 28, 2025, 1:54am UTC](https://discourse.julialang.org/t/retrieve-a-value-within-a-do-block/131121 "2025-07-28T01:54:27Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [July 28, 2025, 5:30am UTC](https://discourse.julialang.org/t/retrieve-a-value-within-a-do-block/131121/2 "2025-07-28T05:30:59Z")

</div>

Is my “feasible” method above correct? I’m a bit unclear now, since `do` is a hard local block.

```julia
julia> function f(af)
           a = 3
           println("inside function: a = $a")
       end
f (generic function with 1 method)

julia> a = 0;

julia> for i = 1:1
           a = 2
           f() do
               a = 7
           end
           println("inside for: a = $a")
       end
inside function: a = 3
inside for: a = 2

julia> a
2

```

Given this fact, how can my following analogous code be made correct?

```julia
julia> Threads.nthreads()
4

julia> using JuMP

julia> my_lock = Threads.ReentrantLock();

julia> model = Model();

julia> JuMP.@variable(model, x[1:3]);

julia> for k = 1:1
           Threads.@threads for i = 1:3
               # The following line is indispensable. If it's absent, ERROR is seen
               cut = "the initial cut inside for i = $i"
               con = @build_constraint(x[i] <= i)
               Threads.lock(my_lock) do
                   cut = JuMP.add_constraint(model, con)
               end
               println("at the end of i = $i, cut = $cut")
           end
       end
at the end of i = 1, cut = x[1] <= 1
at the end of i = 3, cut = x[3] <= 3
at the end of i = 2, cut = x[2] <= 2

julia> print(model)
Feasibility
Subject to
 x[3] <= 3
 x[1] <= 1
 x[2] <= 2

```

---

_[View the full topic](https://discourse.julialang.org/t/retrieve-a-value-within-a-do-block/131121)._
