mercredi 21 juillet 2021

simple spawning of function as std::thread

Comrades!

In an attempt to inspect code that gcc will generate, I'm trying to do some multi-threaded stuff. A reduced testcase is here:

#include <iostream>
#include <thread>

int  n;
void embiggen(void) { ++n; }

void foo(void)
{
  std::thread t1(embiggen);
  t1.join();
  std::cout << "n = " << n << '\n';
};

Which of course, generates this failure:

/xtools/aarch64-unknown-elf/bin/aarch64-unknown-elf-g++ -ggdb3 -std=c++11 -Wall -Wextra -Wno-unused-parameter -O0 -lpthread -march=armv8.1-a foo.cc -c foo.o;
foo.cc: In function 'void foo()':
foo.cc:9:26: error: no matching function for call to 'std::thread::thread(void (&)())'
    9 |   std::thread t1(embiggen);
      |                          ^
/xtools/aarch64-unknown-elf/aarch64-unknown-elf/include/c++/11.1.0/bits/std_thread.h:157:5: note: candidate: 'std::thread::thread(std::thread&&)'
  157 |     thread(thread&& __t) noexcept
      |     ^~~~~~
/xtools/aarch64-unknown-elf/aarch64-unknown-elf/include/c++/11.1.0/bits/std_thread.h:157:21: note:   no known conversion for argument 1 from 'void()' to 'std::thread&&'
  157 |     thread(thread&& __t) noexcept
      |            ~~~~~~~~~^~~
/xtools/aarch64-unknown-elf/aarch64-unknown-elf/include/c++/11.1.0/bits/std_thread.h:121:5: note: candidate: 'std::thread::thread()'
  121 |     thread() noexcept = default;
      |     ^~~~~~
/xtools/aarch64-unknown-elf/aarch64-unknown-elf/include/c++/11.1.0/bits/std_thread.h:121:5: note:   candidate expects 0 arguments, 1 provided

I believe that I've attempted code like this, and this, to no avail.

Q: What is the incantation to spawn a function as a separate thread?

EDIT: I am using gcc-11.1.0, created with crosstool-NG. Perhaps it was built incorrectly? How could it not understand threading...?

Aucun commentaire:

Enregistrer un commentaire