Bind SIGTERM to a member function - c++

I would like to create a simple class to signals handling (just for study) with std::bind. However, I could not compile this code:
#include <iostream>
#include <functional>
#include <csignal>
using namespace std;
class SignalHandler
{
public:
void handler(int value)
{
cout << value << endl;
}
SignalHandler()
{
auto callback = std::bind(&SignalHandler::handler, this, std::placeholders::_1);
sighandler_t ret = std::signal(SIGTERM, callback);
if (SIG_ERR == ret) {
throw;
}
}
};
int main() {
SignalHandler handler;
raise(SIGTERM);
return 0;
}
(GCC) Compiler exit:
prog.cpp: In constructor 'SignalHandler::SignalHandler()':
prog.cpp:21:51: error: cannot convert 'std::_Bind(SignalHandler*, std::_Placeholder<1>)>' to '__sighandler_t {aka void ()(int)}' for argument '2' to 'void ( signal(int, __sighandler_t))(int)'
sighandler_t ret = std::signal(SIGTERM, callback);

You can use static methods to handle SIGTERM, et al. I've done that before. static was the key to get signatures to match.

Related

boost signals2 - error when pass a slot to disconnect

I want to pass a slot to disconnect like in the boost signals2 documentation:
Pass slot to disconnect: in this interface model, the disconnection of
a slot connected with sig.connect(typeof(sig)::slot_type(slot_func))
is performed via sig.disconnect(slot_func).
#include <iostream>
#include <boost/signals2.hpp>
class Test {
public:
Test();
using Signal = boost::signals2::signal<void ()>;
void slot();
void test();
};
Test::Test() {
boost::function<void ()> boundSlot = boost::bind(&Test::slot, this);
Signal sig;
sig.connect(Signal::slot_type(boundSlot));
sig();
sig.disconnect(boundSlot);
};
void Test::slot() {
std::cout << "slot()" << std::endl;
};
int main()
{
Test aTest;
return 0;
}
... but I got an error when calling disconnect (see http://cpp.sh/45q6):
error: ambiguous overload for 'operator=='
(operand types are 'boost::signals2::slot >::slot_function_type
{aka boost::function}'
and 'const boost::function')
What do I wrong?
Problem is, that std::function has no operator == for function compare, only for function and nullptr, but it's required, so you just can use boost::function, which has compare operator and boost::bind.

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?
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.
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

std::function as sighandler_t

How to specify lambda, std::bind result or any other std::function as argument for unix signal function?
I'm trying the following
std::function<void(int)> handler1 = std::bind(&cancellation_token::cancel, &c);
std::function<void(int)> handler2 = [&c](int) { c.cancel(); };
but it doesn't work, because both
handler1.target<void(int)>()
and
handler2.target<void(int)>()
return null
It works if I initialize handler with free function pointer
void foo(int) { ... }
std::function<void(int)> handler = foo;
but this is absolutely useless. I need to capture some local variables, so I need either bind or lambda.
Actually I understand why it doesn't work. Documentation says that target function returns a pointer to the stored function if target_type() == typeid(T), otherwise a null pointer. I don't understand how to make it work.
Any suggestions?
Since it's constructed by bind, or lambda with captured-data, you cannot convert it to free function, since target function works by typeid, std::function saves it in runtime, not for type T, with which function is templated. For std::bind it will be some library-type and for lambda it will be some unnamed type.
You can use a dispatcher-like approach associating signal numbers to std::functions through a map.
You just need a map to hold the std::functions accesible from a free function:
std::unordered_map<int, std::function<void(int)>> signalHandlers;
And a generic handler (free function) to map the signal number to the function:
void dispatcher(int signal) {
// this will call a previously saved function
signalHandlers.at(signal)(signal);
}
Implementation example
main.cpp
#include <iostream>
#include <thread>
#include <csignal>
#include "cppsignal.hpp"
int main() {
bool stop = false;
// set a handler as easy as this
CppSignal::setHandler(SIGINT, [&stop] (int) { stop = true; });
while (!stop) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
std::cout << "Bye" << std::endl;
return 0;
}
cppsignal.cpp
#include <cstring> // strsignal
#include <csignal>
#include <string>
#include <stdexcept>
#include <unordered_map>
#include <mutex>
#include "signal.hpp"
namespace CppSignal {
std::timed_mutex signalHandlersMutex;
std::unordered_map<int, std::function<void(int)>> signalHandlers;
// generic handler (free function) to set as a handler for any signal
void dispatcher(int signal) {
std::unique_lock<std::timed_mutex> lock(signalHandlersMutex, std::defer_lock);
if (!lock.try_lock_for(std::chrono::seconds(1))) {
// unable to get the lock. should be a strange case
return;
}
auto it = signalHandlers.find(signal);
if (it != signalHandlers.end()) {
it->second(signal);
}
}
void registerHandler(int signal, const std::function<void(int)>& handler) {
std::lock_guard<std::timed_mutex> lock(signalHandlersMutex);
signalHandlers.emplace(signal, handler);
}
// this is the only method you will use
void setHandler(int signal, const std::function<void(int)>& handler, int flags) {
// configure sigaction structure
struct sigaction action;
if (sigfillset(&action.sa_mask) == -1) {
throw std::runtime_error("sigfillset failed");
}
action.sa_flags = flags;
action.sa_handler = dispatcher;
// set handler for the signal
if (sigaction(signal, &action, nullptr) == -1 && signal < __SIGRTMIN) {
throw std::runtime_error("Fail at configuring handler for signal: " + std::string(strsignal(signal)));
}
registerHandler(signal, handler);
}
}
cppsignal.hpp
#ifndef __CPPSIGNAL_HPP
#define __CPPSIGNAL_HPP
#include <functional>
namespace CppSignal {
void setHandler(int signal, const std::function<void(int)>& handler, int flags=0);
}
#endif
sighandler_t is defined to be a pointer to a function with the following definition:
void func(int);
Since std::bind and lambdas return functors, it is not possible to directly use them as signal handler. As a workaround you can use your own wrapper functions like
class SignalHandlerBase
{
public:
virtual void operator(int) = 0;
};
template <class T>
class SignalHandler : public SignalHandlerBase
{
T t;
public:
SignalHandler(T _t) : t(_t) { }
void operator(int i)
{
t(i);
}
};
class SignalManager
{
int sig;
SignalHandlerBase *shb;
static void handlerFunction(int i)
{
shb(i);
}
public:
SignalManager(int signal) : sig(signal), shb(nullptr) { signal(signal, &handlerFunction); }
template <class T>
void installHandler(T t)
{
delete shb;
shb = new SignalHandler<T>(t);
}
};
Use global instances of SignalManager to manage individual signals
C++11 1.9 [intro.execution]/6:
When the processing of the abstract machine is interrupted by receipt of a signal, the values of objects which
are neither
of type volatile std::sig_atomic_t nor
lock-free atomic objects (29.4)
are unspecified during the execution of the signal handler, and the value of any
object not in either of these
two categories that is modified by the handler becomes undefined.
The only action you can realistically take portably in a signal handler is to change the value of a flag whose type is volatile std::sig_atomic_t or a lock-free std::atomic (Note that not all std::atomic objects are lock-free). Non-signal handling code can then poll that flag to respond to the occurrence of the signal.
N3787 has some interesting discussion about how to fix C++11 basically breaking signal handlers as a concept.

Function for the thread within a class in C++

I'm having troubles with creation of the function, that should handle new thread. When I create it outside of the class, everything works allright, but when I want to create it inside a class, I can't realize, how to call it.
I call the function with:
pthread_t thread;
pthread_create(&thread, NULL,
sendMessage, (void *) fd);
and the function itself looks like this:
void * sendMessage(void *threadid) {
string message;
const char * c;
char buffer[200];
int fd = (long) threadid;
while (true) {
cin >> message;
if (message == "exit") {
break;
}
c = message.c_str();
strncpy(buffer, c, sizeof ( buffer));
send(fd, buffer, strlen(buffer), 0);
}
}
but when I declare it within a class e.g. void * Client::sendMessage(void *threadid) , I can't even build it because I get main.cpp:90:37: error: argument of type ‘void* (Client::)(void*)’ does not match ‘void* (*)(void*)’ Does anybody have any idea, what can cause it and how to fix it?
Just a quick demonstration of how std::thread can quickly make all your woes disappear (by seemlessly integrating with std::bind style invocation):
#include <string>
#include <thread>
#include <memory>
void some_function(int i, std::string bla)
{
}
struct some_class
{
void some_member_function(double x) const
{
}
};
int main()
{
std::thread t1(&some_function, 42, "the answer");
std::thread t2;
{
some_class instance;
t2 = std::thread(&some_class::some_member_function,
std::ref(instance),
3.14159);
t2.join(); // need to join before `instance` goes out of scope
}
{
auto managed = std::make_shared<some_class>();
t2 = std::thread([managed]()
{
managed->some_member_function(3.14159);
});
// `managed` lives on
}
if (t1.joinable()) t1.join();
if (t2.joinable()) t2.join();
}

Getting error when using pthread_create [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
pthread Function from a Class
I am fairly new to c++ and I am doing a project regarding TCP.
I need to create a thread so I googled and found this.
http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
I follow its syntax but encounter errors:
argument of type ‘void* (ns3::TcpSocketBase::)()’ does not match ‘void* ()(void)’
codes:
tcp-socket-base.h:
class TcpSocketBase : public TcpSocket
{
public:
...
void *threadfunction();
....
}
tcp-socket-base.cc:
void
*TcpSocketBase::threadfunction()
{
//do something
}
..//the thread was create and the function is called here
pthread_t t1;
int temp = pthread_create(&t1, NULL, ReceivedSpecialAck, NULL); //The error happens here
return;
...
Any help would be appreciated. Thanks!
EDIT:
I took the advise and make the threadfunction a non member function.
namespaceXXX{
void *threadfunction()
int result = pthread_create(&t1, NULL, threadfunction, NULL);
NS_LOG_LOGIC ("TcpSocketBase " << this << " Create Thread returned result: " << result );
void *threadfunction()
{
.....
}
}
But I got this error instead:
initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)’ [-fpermissive]
If you'd like to continue using pthreads, a simple example is:
#include <cstdio>
#include <string>
#include <iostream>
#include <pthread.h>
void* print(void* data)
{
std::cout << *((std::string*)data) << "\n";
return NULL; // We could return data here if we wanted to
}
int main()
{
std::string message = "Hello, pthreads!";
pthread_t threadHandle;
pthread_create(&threadHandle, NULL, &print, &message);
// Wait for the thread to finish, then exit
pthread_join(threadHandle, NULL);
return 0;
}
A better alternative, if you're able to, is to use the new C++11 thread library. It's a simpler, RAII interface that uses templates so that you can pass any function to a thread, including class member functions (see this thread).
Then, the above exmaple simplifies to this:
#include <cstdio>
#include <string>
#include <iostream>
#include <thread>
void print(std::string message)
{
std::cout << message << "\n";
}
int main()
{
std::string message = "Hello, C++11 threads!";
std::thread t(&print, message);
t.join();
return 0;
}
Note how you can just pass data directly in - casts to and from void* are not needed.
You look to be passing a member function of a class to your pthread_create function. The thread function should be a non-member function that has the following signature
void *thread_function( void *ptr );
If you declare the function static it will compile.