C++11 threads no matching function call - multithreading

I am trying to make a threaded grabber for my OpenCV application. I am unable to figure out why this code doesn't compile. It gives me an error that I believe means that the function call is wrong. However, it is the exact same way how I start a thread using std::thread usually! I want to use std::thread to accomplish it because it will offer more platform-independent compatibility, so please don't tell me to use a platform-specific library. I also want this to be STL-based, so no Boost or DLib. In my main.cpp, I have a working thread application, the code below:
#include <iostream>
#include <fstream>
#include <string>
#include <thread>
#include <mutex>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#define read_failure_threshold 512
long grabbers_active = 0;
namespace dev
{
class grabber
{
private:
bool enabled = false;
std::mutex lock;
int capture_mode;
int capture_id;
unsigned long read_failures = 0;
std::string stream;
std::string grabber_name;
cv::Mat image;
public:
void grabber_t()
{
.......[unimportant code]........
}
grabber(std::string name, int captureMode, int captureId, std::string location)
{
.......[unimportant code]........
}
void start()
{
if(!enabled)
{
std::thread grabber_thread(grabber_t);
grabber_thread.detach();
}
enabled = true;
grabbers_active++;
}
cv::Mat getImage()
{
.......[unimportant code]........
}
};
}
[ERRORS:]
In file included from /media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/main.cpp:1:0:
/media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/template.hpp: In member function ‘void dev::grabber::start()’:
/media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/template.hpp:119:52: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>)’
std::thread grabber_thread(grabber_t);
^
/media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/template.hpp:119:52: note: candidates are:
In file included from /media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/template.hpp:4:0,
from /media/storage/programming/yash101/repos/Other/STL+OpenCV/threaded_grabber_template/main.cpp:1:
/usr/include/c++/4.8/thread:133:7: note: std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (dev::grabber::*)(); _Args = {}]
thread(_Callable&& __f, _Args&&... __args)
^
/usr/include/c++/4.8/thread:133:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘void (dev::grabber::*&&)()’
/usr/include/c++/4.8/thread:128:5: note: std::thread::thread(std::thread&&)
thread(thread&& __t) noexcept
^
/usr/include/c++/4.8/thread:128:5: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::thread&&’
/usr/include/c++/4.8/thread:122:5: note: std::thread::thread()
thread() noexcept = default;
^
/usr/include/c++/4.8/thread:122:5: note: candidate expects 0 arguments, 1 provided
make[2]: *** [CMakeFiles/build.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/build.dir/all] Error 2
make: *** [all] Error 2
The error log is also at the end of the code. The only errors I am worried about are the threading ones. The other ones are simple fixes, but require me to have the threading working.
I am in Ubuntu, using g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2. I have C++0x enabled in my CMakeLists.txt. Everything works perfectly in there
My main objective is to figure out why I am getting this error. I have been googling and trying different tricks for many hours, but nothing is working!
Thanks in advanced for your help :)

Change that :
std::thread grabber_thread(grabber_t);
Into that :
std::thread grabber_thread(&grabber::grabber_t, this);
grabber_t is a reference to non-static member function, you need to pass its address, but &grabber_t can't work as you must explicitly qualify name of member function when taking its address, thus resulting in &grabber::grabber_t.

Related

Adding DEFINE_WAIT fails to compile in linux module

I added a wait queue entry by calling DEFINE_WAIT and the code is failing to compile.
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
MODULE_LICENSE("GPL");
DEFINE_WAIT(mywait);
static int __init test_hello_init(void)
{
return 0;
}
static void __exit test_hello_exit(void)
{
}
module_init(test_hello_init);
module_exit(test_hello_exit);
It fails with the below error, saying initializer element is not constant.
In file included from ./include/linux/thread_info.h:21:0,
from ./arch/x86/include/asm/preempt.h:7,
from ./include/linux/preempt.h:78,
from ./include/linux/spinlock.h:51,
from ./include/linux/seqlock.h:36,
from ./include/linux/time.h:6,
from ./include/linux/stat.h:19,
from ./include/linux/module.h:10,
from /home/linuxtrainer/Linux_Device_Drivers/day20/3_waitqueue/2/hello.c:2:
./arch/x86/include/asm/current.h:18:17: error: initializer element is not constant
#define current get_current()
Adding DEFINE_WAIT inside test_hello_init works, what is the difference?
DEFINE_WAIT() uses an initialiser that is not a compile-time constant - it needs to be executed in process context - so it can't be used at file scope.
It should be used directly in the function that is going to do the wait.

Runtime check failure in CString destructor caused by setting Class ptr to NULL?

so somewhere along the lines of putting this app together I've started to get a runtime check failure stack corruption when the destructor for a cstring class member is called.
I've gotten to the point of trying to debug this by throwing bricks at the issue but still havent root caused it. At the current moment the class that the cstring resides in does nothing but initialize its private string members and set a pointer to another class to NULL.
Interestingly if I do not set the class pointer to NULL and comment out that line the corruption goes away. I think this is somewhat of a red herring, and that something is changing in the way the compiler is putting the code together when it pulls in the .h file that contains theCLog definitions and that would be used since I'm declaring a pointer to that object.
int _tmain(int argc, _TCHAR* argv[])
{
DWORD a = 0xBABA; //just to help catch the corrupter
DWORD b = 0xDFDF;
CStringW startat = L"\\\\anetworkshare\\fre";
CStringW lookfor = L".inf";
DirEnum myEnum(startat,lookfor);
ULONG en = a + b;
en = a - b;
return 0;
}
DirEnum.cpp
DirEnum::DirEnum(CString startingdir,CString fileFilter)
{
m_plogfile = NULL; //If you comment out this line corruption goes away
m_startingdir = L"";
m_extfilter = L"";
if(startingdir.GetLength() > 0)
m_startingdir = startingdir;
if(fileFilter.GetLength() > 0)
m_extfilter = fileFilter;
//following commented out to tshoot
//CLogBase& ref = ref.GetInstance();
//logBase = &ref;
//m_plogfile = new CLog(L"DirEnumerator",L"logfile.txt",logINFO);
}
Now I suspect that something in the log.h file is causing a change to occuur in the ATL or CString libraries but I dont know what. Heres the log.h file
#pragma once
//#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
using namespace std;
#ifndef TYPEDEF_H
#define TYPEDEF_H
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <tchar.h>
#include <time.h>
//simple defines to allow the TCHAR library to be used
typedef std::basic_string<TCHAR> tstring;
typedef std::basic_ostream<TCHAR> tostream;
typedef std::basic_istream<TCHAR> tistream;
typedef std::basic_ostringstream<TCHAR> tostringstream;
typedef std::basic_istringstream<TCHAR> tistringstream;
typedef std::basic_ofstream<TCHAR> tofstream;
#if defined(UNICODE) || defined(_UNICODE)
#define tcout std::wcout
#define tcin std::wcin
#else
#define tcout std::cout
#define tcin std::cin;
#endif
#endif
#if defined DEBUG || defined (_DEBUG)
#define TOCONSOLE
#endif
typedef enum LOGLVL{logERROR =0,logWARN,logINFO,logDEBUG};
//CLogBase os a singleton log class. Intent is that you can establish a reference from anywhere in the project and write to the same file
// without having locking issues or threading issues
class CLogBase
{
public:
static CLogBase& GetInstance(CString logname = L"log.txt",LOGLVL lvl = logWARN);
~CLogBase(void);
tostringstream& GetLog(LOGLVL level);
tostringstream& GetStream(LOGLVL);
void Forceflush();
private:
CLogBase(CString file,LOGLVL lvl);
//our outstream
tostringstream m_os;
tostringstream m_dummy;
tofstream m_filestream;
CString m_filename;
LOGLVL m_reportlvl;
//Private declarations to prevent copy constructors from being invoked; these are do nothig implimentations
CLogBase(CLogBase const&);
void operator=(CLogBase const&);
};
class CLog
{
public:
CLog(CString component);
CLog(CString component,CString logname,LOGLVL lvl);
~CLog();
void Log(LOGLVL,CString message);
void CLog::Flush();
tostringstream& CLog::GetStream(LOGLVL lvl);
private:
CString m_componentname;
CLogBase* m_logBase;
};
I thought I would answer this as I found the issue. this was not a coding problem per se but a visual studio issue.
What happened was that I was storing the direnum.h file and .cpp file in a different directory than the one used for the main project. referencing the header with #include "..\somedir\direnum.h"
at one point in time visual studio reported the file as locked and did I want to overwrite \ cancel etc. I choose overwrite but what seemed to happen was that this caused VS to COPY the files to the current project. All my troubleshooting attempts were being edited in the local somename.h file whtne opened in the editor but the compiler was doing the correct thing and pulling down the .h file from the location above.
removing switching to the now local copy of direnum.h and recompiling fixed this as it was compiling part of the code as ANSI and the other part as WCHAR

googletest integrated in CLR/CLI Compilation error LNK2028

Im totally new to google testing in CLI. Just managed to set up and integrate google test with visual studio 2012. However, when I try to include the header file of my project with my tester.h file (because I want to test the functions in that headerfile), I encountered the following error:
Error 2 error LNK2028: unresolved token (0A0006FB) "public: __thiscall ExpenseTracker::ExpenseTracker(void)" (??0ExpenseTracker##$$FQAE#XZ) referenced in function "private: virtual void __thiscall enter_settings_user_login_Test::TestBody(void)" (?TestBody#enter_settings_user_login_Test##$$FEAEXXZ) C:\Users\Jacky\Desktop\EzXpns3\test_project\main.obj test_project
Error 3 error LNK2019: unresolved external symbol "public: __thiscall ExpenseTracker::ExpenseTracker(void)" (??0ExpenseTracker##$$FQAE#XZ) referenced in function "private: virtual void __thiscall enter_settings_user_login_Test::TestBody(void)" (?TestBody#enter_settings_user_login_Test##$$FEAEXXZ) C:\Users\Jia Wei\Desktop\EzXpns3\test_project\main.obj test_project
I have tried including dependencies gtestd.lib, kernel32.lib, user32.lib, advapi32.lib, Ws2_32.lib
Problem slightly resolved. Instead of having expenseTracker.h and ExpenseTracker.cpp, I placed all my implementation of ExpenseTracker.h in the header file itself and everything compiled nicely and test run. However, my entire project has been built on both header file and cpp file and its not very wise to redo everything for testing purposes right? Could anyone help with this?
Attached below are my header files.
//tester.h
#include "gtest/gtest.h" //include to use Google Unit test's stuff
#include "C:\Users\Jacky\Desktop\EzXpns3\Source - testing\EzXpns2\ExpenseTracker.h"
using namespace std;
class ExpenseTracker;
/************************************************************************/
/* We write test cases here */
/************************************************************************/
TEST(basic_test, add_simple_route)
{
ASSERT_EQ(1, 1);
}
TEST(enter_settings, user_login)
{
ExpenseTracker :: ExpenseTracker();
//loadUserInfo();
string username = "XXX_XXX";
string password = "12345";
//myTracker -> loadUserInfo();
//bool result = myTracker -> login(username, password);
//ASSERT_EQ(true, result);
}
void runTest(int argument_count, char** argument_vars)
{
testing::InitGoogleTest(&argument_count, argument_vars); //initialize GTest
RUN_ALL_TESTS();
std::getchar(); //pause the program after all the testing
}
//main.cpp
#include "tester.h"
#include <cstdio>
using namespace System;
using namespace testing;
int main(int argument_count, char** argument_vars)
{
//int argc;
//char** argv;
//runTest(argc, argv);
//InitGoogleTest(argc, argv);
testing::InitGoogleTest(&argument_count, argument_vars); //initialize GTest
RUN_ALL_TESTS();
std::getchar();
return 0;
}
//ExpenseTracker.h, the file which I want to include
#ifndef _EXPENSETRACKER_H
#define _EXPENSETRACKER_H
#include <string>
#include <vector>
#include "user2.h"
using namespace std;
class ExpenseTracker
{
private:
vector<User*> allMyUsers;
public:
ExpenseTracker(); //empty constructor
void addUser(User*);
int findUser(string);
bool login(string, string);
void loadUserInfo();
User* getUser(string);
int getUserSize();
};
#endif;
You need to provide an implementation for the constructor. In other words, you should change:
ExpenseTracker(); //empty constructor
into:
ExpenseTracker() {} // empty constructor
However, since the constructor is empty you might as well remove it entirely!

Getting args from kprobe not finding regs->rdi x86_64

I'm writing a kernel module under Scientific Linux 6.3 x86_64 and I'm looking to use kprobes. In this module, I need access to the first argument of a function on return, so jprobes are out.
I found this very helpful post: Getting function arguments using kprobes
However, when I try accessing regs->rdi inside my probe, the compiler complains with
error: ‘struct pt_regs’ has no member named ‘rdi’
During my module initialization, I run this check with no problems:
#ifndef CONFIG_X86_64
printk(KERN_ALERT "Error: this module only supports x86_64!\n");
return -EINVAL;
#endif
Is there anything else I should be looking at?
uname -r returns 2.6.32-279.14.1.el6.x86_64.debug
Here is a MWE:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kprobes.h>
#include <linux/blkdev.h>
static int kprobe_test(struct kprobe *p, struct pt_regs *regs) {
printk(KERN_INFO "rdi: %p\n", regs->rdi);
return 0;
}
static struct kprobe myprobe = {
.pre_handler = NULL,
.post_handler = kprobe_test,
.fault_handler = NULL,
.addr = (kprobe_opcode_t *) generic_make_request,
};
int init_module(void) {
register_kprobe(&myprobe);
return 0;
}
void cleanup_module(void) {
unregister_kprobe(&myprobe);
}
Which results in:
...
/home/user/kmod/kprobe_64_mwe/kprobe_mwe.c:7: error: ‘struct pt_regs’ has no member named ‘rdi’
...
The definition of pt_reg changes when __KERNEL__ is defined. Try using di instead.

error C2064: term does not evaluate to a function taking 0 arguments thread.hpp(60)

I'm creating c++ game server. The server creates many objects monster, and every monster should have its thread with specific function.
I get error :
error C2064: term does not evaluate to a function taking 0 arguments
thread.hpp(60) : while compiling class template member function 'void
boost::detail::thread_data<F>::run(void)'
monster.cpp:
#include "monster.h"
monster::monster(string temp_mob_name)
{
//New login monster
mob_name = temp_mob_name;
x=rand() % 1000;
y=rand() % 1000;
boost::thread make_thread(&monster::mob_engine);
}
monster::~monster()
{
//Destructor
}
void monster::mob_engine()
{
while(true)
{
Sleep(100);
cout<< "Monster name"<<mob_name<<endl;
}
}
monster.h:
#ifndef _H_MONSTER_
#define _H_MONSTER_
//Additional include dependancies
#include <iostream>
#include <string>
#include "boost/thread.hpp"
using namespace std;
class monster
{
public:
//Functions
monster(string temp_mob_name);
~monster();
//Custom defined functions
void mob_engine();
int x;
int y;
};
//Include protection
#endif
mob_engine is a non-static member function, so it has an implicit this argument.
Try this:
boost::thread make_thread(boost::bind(&monster::mob_engine, this));
According to this similar question boost:thread - compiler error you can even avoid using bind by simply writing:
boost::thread make_thread(&monster::mob_engine, this);
Also, you will probably want to declare a boost::thread member variable to keep a reference to the thread.

Resources