C++ multithreaded program not compiling on windows 8 with g++

62 Views Asked by At

I am trying to implement a multithreaded C++ program on Windows 8. I wrote the following code and it is not compiling on my Windows 8 machine.

test.cpp

#include <iostream>
#include <thread>

void thread_function()
{
    std::cout << "thread function\n";
}

int main()
{
    std::thread t(&thread_function);   // t starts running
    std::cout << "main thread\n";
    t.join();   // main thread waits for the thread t to finish
    return 0;
}

I am getting

D:\Madhusudhana H V\home\StrideSimulator>g++ test.cpp
test.cpp: In function 'int main()': test.cpp:11:35: error: no matching function for call to 'std::thread::thread(void (*)())'    11 |     std::thread t(&thread_function);   // t starts running
      |                                   ^ In file included from c:\program files\gcc\include\c++\11.1.0\thread:43,
                 from test.cpp:2: c:\program files\gcc\include\c++\11.1.0\bits\std_thread.h:157:5: note: candidate : 'std::thread::thread(std::thread&&)'   157 |     thread(thread&&
__t) noexcept
      |     ^~~~~~ c:\program files\gcc\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
      |            ~~~~~~~~~^~~ c:\program files\gcc\include\c++\11.1.0\bits\std_thread.h:121:5: note: candidate : 'std::thread::thread()'   121 |     thread() noexcept = default;
      |     ^~~~~~ c:\program files\gcc\include\c++\11.1.0\bits\std_thread.h:121:5: note:   candidate expects 0 arguments, 1 provided

enter image description here

0

There are 0 best solutions below