Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling C++ threads

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?

like image 592
dv_ Avatar asked Jun 23 '15 19:06

dv_


People also ask

Can you create threads in C?

Creating a threadThe name of the function that the thread starts to execute. If the function's return type is void * , then its name is simply written; otherwise, it has to be type-cast to void * . This is the argument that the starting routine takes. If it takes multiple arguments, a struct is used.

How C files are compiled?

Compilation process in C involves four steps: pre-processing, compiling, assembling, and linking. The preprocessor tool helps in comments removal, macros expansion, file inclusion, and conditional compilation. These commands are executed in the first step of the compilation process.

Can I compile C with GCC?

GCC stands for GNU Compiler Collections which is used to compile mainly C and C++ language. It can also be used to compile Objective C and Objective C++.

Can I compile C in CMD?

We usually use a compiler with a graphical user interface, to compile our C program. This can also be done by using cmd. The command prompt has a set of steps we need to perform in order to execute our program without using a GUI compiler.


2 Answers

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.

like image 184
Barry Avatar answered Sep 22 '22 07:09

Barry


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

like image 44
marom Avatar answered Sep 18 '22 07:09

marom