Reputation: 169
I'm trying to use threads on my C++ application.
My code is:
#include <iostream>
#include <thread>
class C
{
public:
void * code( void * param )
{
std::cout << "Code thread executing " << std::endl;
return NULL;
}
};
int main()
{
C c;
std::thread t ( &C::code, &c );
t.join();
}
When compiling, I got those errors:
In file included from /opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/bits/move.h:57:0,
from /opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/bits/stl_pair.h:61,
from /opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/bits/stl_algobase.h:65,
from /opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/bits/char_traits.h:41,
from /opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/ios:41,
from /opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/ostream:40,
from /opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/iostream:40,
from C.cpp:1:
/opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/type_traits: In instantiation of 'struct std::_Result_of_impl<false, false, std::_Mem_fn<void* (C::*)(void*)const>, C*>':
/opt/centos/devtoolset-1.0/root/usr/lib/gcc/x86_64-redhat-linux/4.7.0/../../../../include/c++/4.7.0/type_traits:1857:12: required from 'class std::result_of<std::_Mem_fn<void* (C::*)(void*)const>(C*)>'
and a lot more...
I'm compiling with:
g++ -std=c++0x C.cpp
The compiler version:
$g++ --version
g++ (GCC) 4.7.0 20120507 (Red Hat 4.7.0-5)
What am I doing wrong?
Upvotes: 8
Views: 643
Reputation: 304152
std::thread
is not the same thing as POSIX thread, it doesn't have to take a void*
argument and return a void*
. The thread
constructor can take any callable as long as you specify the right arguments.
The specific error in this case is that you are attempting to start a thread that effectively calls c.code()
(technically INVOKE(&C::code, &c)
) , but that is an invalid call since C::code
takes one argument and you are trying to call it with zero. Simply fix the signature on code()
to match what you are calling it with:
void code()
{
std::cout << "Code thread executing " << std::endl;
}
Alternatively, you can provide the void*
arg to the thread
constructor:
std::thread t ( &C::code, &c, nullptr );
^^^^^^^
Either way, make sure you compile with -pthread
.
Upvotes: 9
Reputation: 5230
Make your class C a callable object using operator()
#include <iostream>
#include <thread>
class C
{
public:
void operator()( void )
{
std::cout << "Code thread executing " << std::endl;
return NULL;
}
};
int main()
{
C c;
std::thread t (c );
t.join();
}
or turn you class into a callable object
#include <iostream>
#include <thread>
#include <functional>
class C
{
public:
void * code( void)
{
std::cout << "Code thread executing " << std::endl;
return NULL;
}
};
int main()
{
C c;
std::thread t (std::bind( &C::code, &c ));
t.join();
}
and switch to --std=c++11
Upvotes: 3