0

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.

1
  • 1
    On an unrelated note, if a function always return the same value, is it really any meaning that it returns anything at all? Commented Feb 23, 2022 at 16:13

1 Answer 1

6

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Oh okay that is pretty cool, and the whole concept makes more sense than it did before. Thanks!
Might be worth mentioning that thread_1(&process,...) is interchangeable with thread_1(process,...). The OP will find many instances of the latter form, without the &, if they go looking for example source code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.