'"error: attempt to use a deleted function" when attempting multithreading in C++

I have a program that is compute intensive that I would like to multithread. Here is the code:

#include <iostream>
#include <math.h>
#include <thread>
#include "print_binary.h"
#include "bit_at.h"

bool process(unsigned int start, unsigned int end) {
    for (unsigned int i = 0; i < (sizeof(unsigned int)*8); i++)
    {
        for (unsigned int j = start; j < end; j++)
        {
            bit_at(i, j);
        }
    }
    return true;
}

int main() {

    unsigned int start {5000};
    unsigned int end {11000};

    std::thread thread_1 (process(start, end));

    thread_1.join();

    return 0;
}

When I attempt to compile this using g++20 on my M1 Mac, I get this error:

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/thread:286:5: error: attempt to use a deleted function
    _VSTD::__invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
    ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__config:856:15: note: expanded from macro '_VSTD'
#define _VSTD std::_LIBCPP_ABI_NAMESPACE
              ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/thread:297:12: note: in instantiation of function template specialization 'std::__thread_execute<std::unique_ptr<std::__thread_struct>, bool>' requested here
    _VSTD::__thread_execute(*__p.get(), _Index());
           ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/thread:313:54: note: in instantiation of function template specialization 'std::__thread_proxy<std::tuple<std::unique_ptr<std::__thread_struct>, bool>>' requested here
    int __ec = _VSTD::__libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
                                                     ^
and_threaded.cpp:23:17: note: in instantiation of function template specialization 'std::thread::thread<bool, void>' requested here
    std::thread thread_1 (process(start, end));
                ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/type_traits:1916:5: note: '~__nat' has been explicitly marked deleted here
    ~__nat() = delete;
    ^
1 error generated.

Could someone explain to me what exactly error: attempt to use a deleted function means? My understanding is that std::thread converts the arguments passed to the function as rvalues, but the process() function should accept that. I have tried passing rvalues to process() like: std::thread thread_1(process(0, 5000)), as well as using rvalue references in the argument list of the function declaration. It all gives me the same error. I have also tried passing by reference using std::ref(start).

Any help at all is greatly appreciated.



Solution 1:[1]

process(start, end) calls the function process. And you pass the bool result to the std::thread constructor as a thread function.

You want to pass a pointer to the function itself, and its arguments, as separate arguments to the std::thread constructor:

std::thread thread_1 (&process, start, end);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Some programmer dude