How to concatenate LibTorch tensors created with a multi-thread process std::thread in C++?

A multi-thread process in C++ returns tensors and I want to concatenate them into one tensor in order.

In C++ I have a single function which returns a single 1x8 tensor.
I call this function multiple times simultaneously with std::thread and I want to concatenate the tensors it returns into one large tensor. For example, I call it 12 times I expect to have a 12x8 tensor when it’s finished.

I’m aware I could just have the function return a 12x8 tensor, but I need to solve the problem of how to grab the tensors produced during the multithreading process.

In my attempt below I try to concatenate the tensors into the all_episode_steps tensor but this returns an error.
If you comment out the all_episode_steps line and put std::cout << one; in the get_tensors function above the return statement you see it appears to be using multithreading to create the tensors with no problems.

#include <torch/torch.h>

torch::Tensor get_tensors(int id) {
    torch::Tensor one = torch::rand({8});
    return one.unsqueeze(0);

torch::Tensor all_episode_steps;
int main() {

    std::thread ths[100];

    for (int id=0; id<12; id++) {

        ths[id] = std::thread(get_tensors, id);

        all_episode_steps = torch::cat({ths[id], all_episode_steps});
    for (int id=0; id<12; id++) {


I believe it is because you’re not initializing all_episode_steps before trying to cat it so there is nothing to cat with whatever is returned from the threads.