# Issues using multithreading with Combinatorics

**URL:** <https://discourse.julialang.org/t/issues-using-multithreading-with-combinatorics/85318>\
**Category:** General Usage\
**Tags:** multithreading, combinatorics\
**Created:** [August 4, 2022, 9:56pm UTC](https://discourse.julialang.org/t/issues-using-multithreading-with-combinatorics/85318 "2022-08-04T21:56:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philip\_Stuckey](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/philip_stuckey/32/4000_2.png) [@Philip\_Stuckey](https://discourse.julialang.org/u/Philip_Stuckey)\
**Post date:** [August 4, 2022, 9:56pm UTC](https://discourse.julialang.org/t/issues-using-multithreading-with-combinatorics/85318/1 "2022-08-04T21:56:09Z")

</div>

When I try to run the following code

```julia
using Combinatorics
using Base.Threads
five_letter_words = ["taxis", "deans", "krone", "unset", "other", "ovens", "spilt", "yanks", "flout", "clang"]
@threads :static for combo in combinations(five_letter_words, 5)
                     if length(intersect(combo)==0)
                         println(combo)
                     end
                 end

```

I get the following error

```julia
ERROR: TaskFailedException
Stacktrace:
 [1] wait
   @ ./task.jl:334 [inlined]
 [2] threading_run(func::Function)
   @ Base.Threads ./threadingconstructs.jl:38
 [3] top-level scope
   @ ./threadingconstructs.jl:97

    nested task error: MethodError: no method matching firstindex(::Base.Generator{Combinatorics.Combinations, Combinatorics.var"#10#13"{Combinatorics.var"#reorder#11"{Vector{String}}}})
    Closest candidates are:
      firstindex(::Any, ::Any) at ~/packages/julias/julia-1.7/share/julia/base/abstractarray.jl:396
      firstindex(::Tuple) at ~/packages/julias/julia-1.7/share/julia/base/tuple.jl:25
      firstindex(::Number) at ~/packages/julias/julia-1.7/share/julia/base/number.jl:88
      ...
    Stacktrace:
     [1] (::var"#83#threadsfor_fun#8"{Base.Generator{Combinatorics.Combinations, Combinatorics.var"#10#13"{Combinatorics.var"#reorder#11"{Vector{String}}}}})(onethread::Bool)
       @ Main ./threadingconstructs.jl:70
     [2] (::var"#83#threadsfor_fun#8"{Base.Generator{Combinatorics.Combinations, Combinatorics.var"#10#13"{Combinatorics.var"#reorder#11"{Vector{String}}}}})()
       @ Main ./threadingconstructs.jl:52

```

Is this a multi-threading problem, a problem with the combinatorics package, or a problem between chair and keyboard?

Thank you for your help

---

<div class="post-metadata">

**Author:** ![palday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palday/32/12640_2.png) [@palday](https://discourse.julialang.org/u/palday)\
**Post date:** [August 4, 2022, 11:44pm UTC](https://discourse.julialang.org/t/issues-using-multithreading-with-combinatorics/85318/2 "2022-08-04T23:44:56Z")

</div>

`combinations` returns a lazy iterator, so there isn’t a defined `length`. The `@threads` macro partitions jobs based on the total number, i.e. `length`. If you call `collect(combinations(...))`, that will force the iterator to be realized as a vector.

However, if you’re looking to do something like the recent StandUpMaths video and want all 5-element combinations of a huge set, then you should be aware that the vector will be quite huge. So big in fact that the allocation might be more expensive than any boost you get from multithreading.
