Hello everybody hope all is well. I am trying to work with threads in c++. I compiled the code and got the following error messages.
C:\Users\Peretz\Documents\keylogger\KeyConstants.h|183|warning: unknown escape sequence: '\|' [enabled by default]|
C:\Users\Peretz\Documents\keylogger\Timer.h|10|error: 'thread' in namespace 'std' does not name a type|
C:\Users\Peretz\Documents\keylogger\Timer.h||In member function 'void Timer::SleepAndRun()':|
C:\Users\Peretz\Documents\keylogger\Timer.h|25|error: 'std::this_thread' has not been declared|
C:\Users\Peretz\Documents\keylogger\Timer.h||In member function 'void Timer::Start(bool)':|
C:\Users\Peretz\Documents\keylogger\Timer.h|71|error: 'Thread' was not declared in this scope|
C:\Users\Peretz\Documents\keylogger\Timer.h|71|error: 'thread' is not a member of 'std'|
C:\Users\Peretz\Documents\keylogger\Timer.h||In member function 'void Timer::Stop()':|
C:\Users\Peretz\Documents\keylogger\Timer.h|82|error: 'Thread' was not declared in this scope|
||=== Build failed: 5 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
I looked on Youtube for threads in c++ and threads in c++11 but could not find an answer. Please explain why there are these errors.
#ifndef TIMER_H
#define TIMER_H
#include <thread>
#include <chrono>
class Timer
{
std::thread Thread;
bool Alive = false;
long CallNumber = -1L;
long repeat_count = -1L;
std::chrono::milliseconds interval = std::chrono::milliseconds(0);
std::function<void(void)> funct = nullptr;
void SleepAndRun ()
{
std::this_thread::sleep_for(interval);
if (Alive)
{
Function ()();
}
}
void ThreadFunc ()
{
if (CallNumber == Infinite)
while (Alive)
SleepAndRun ();
else
while (repeat_count--)
SleepAndRun ();
}
public:
static const long Infinite = -1L;
Timer () {}
Timer(const std::function<void(void)> &f) : funct (f) {}
Timer(const std::function<void(void)> &f,
const unsigned long &i,
const long repeat = Timer::Infinite) : funct (f),
interval (std::chrono::milliseconds(i)), CallNumber (repeat) {}
void Start (bool Async = true)
{
if (IsAlive ())
return;
Alive = true;
repeat_count = CallNumber;
if (Async)
Thread = std::thread (ThreadFunc,this);
else
this->ThreadFunc ();
}
void Stop ()
{
Alive = false;
Thread.join ();
}
void SetFunction (const std::function<void (void)> &f)
{
funct = f;
}
bool IsAlive () const
{
return Alive;
}
void RepeatCount (const long r)
{
if (Alive)
return;
CallNumber = r;
}
long GetLeftCount () const
{
return repeat_count;
}
long RepeatCount () const
{
return CallNumber;
}
void SetInterval (const unsigned long &i)
{
if (Alive)
return;
interval = std::chrono::milliseconds(i);
}
unsigned long Interval () const
{
return interval.count();
}
const std::function<void(void)> &Function () const
{
return funct;
}
};
#endif
Thank you very much.
error: 'thread' in namespace 'std' does not name a type
Whichever compiler you're using (looks like Visual C++) doesn't support std::thread (which is part of C++11).
Related
As a bit of context on the issue I have, I am trying to compile my project using MSVC (Visual Studio 2022) on Windows and it produces a bunch of errors, which is surprising, considering the same code builds just fine on GNU-G++.
What I am trying to do is to write a packet spoofer using the libtins library. I am working on a multi-threaded approach, where one thread captures the packets and pushes them to a queue and the other pops out one packet at a time, does "the spoofing", and then forwards it somewhere else.
A construct is provided for capturing network packets in a loop, in the form of a template function.
/* Credits: M.Fontanini, libtins */
template <typename Functor>
void Tins::BaseSniffer::sniff_loop(Functor function, uint32_t max_packets) {
for(iterator it = begin(); it != end(); ++it) {
try {
// If the functor returns false, we're done
#if TINS_IS_CXX11 && !defined(_MSC_VER)
if (!Tins::Internals::invoke_loop_cb(function, *it)) {
return;
}
#else
if (!function(*it->pdu())) {
return;
}
#endif
}
catch(malformed_packet&) { }
catch(pdu_not_found&) { }
if (max_packets && --max_packets == 0) {
return;
}
}
}
This works by binding a callback function which will be called every time a packet is captured. I have tried to leverage this by creating a wrapper class, called PacketSniffer, where I bind a callback function to Sniffer::sniff_loop which pushes every captured packet to a queue.
bool
PacketSniffer::callback(Tins::Packet &packet,
ThreadSafeQueue<Tins::Packet> &packetq,
bool &running) {
packetq.push(packet);
return running;
}
void PacketSniffer::run(ThreadSafeQueue<Tins::Packet> &packetq, bool &running) {
try {
sniffer_->sniff_loop(std::bind(&PacketSniffer::callback, this,
std::placeholders::_1, std::ref(packetq),
std::ref(running)));
} catch (std::exception &ex) {
throw std::runtime_error(ex.what());
}
}
The actual call where I use this in my app:
// Packet capture
bool running = true;
std::thread capture([&pq = packetq_, st, &running,
&iface_value, &pcap_filter_value]() {
PacketSniffer ps(st, iface_value.data(),
pcap_filter_value.data());
ps.run(pq, running);
});
MSVC compiler error:
C:\Users\adrian\repos\src\spoofer\build\_deps\libtins-src\include\tins/sniffer.h(681,18): error C2672: 'operator __surrogate_func': no matchi
ng overloaded function found [C:\Users\adrian\repos\src\spoofer\build\src\spoofer.vcxproj]
C:\Users\adrian\repos\src\spoofer\src\sniffer.cpp(74): message : see reference to function template instantiation 'void Tins::BaseSniffer::sn
iff_loop<std::_Binder<std::_Unforced,bool (__cdecl spoofer::PacketSniffer::* )(Tins::Packet &,ThreadSafeQueue<Tins::Packet> &,bool &),spoofer::
PacketSniffer *,const std::_Ph<1> &,std::reference_wrapper<ThreadSafeQueue<Tins::Packet>>,std::reference_wrapper<bool>>>(Functor,uint32_t)' b
eing compiled [C:\Users\adrian\repos\src\spoofer\build\src\spoofer.vcxproj]
with
[
Functor=std::_Binder<std::_Unforced,bool (__cdecl spoofer::PacketSniffer::* )(Tins::Packet &,ThreadSafeQueue<Tins::Packet> &,boo
l &),spoofer::PacketSniffer *,const std::_Ph<1> &,std::reference_wrapper<ThreadSafeQueue<Tins::Packet>>,std::reference_wrapper<bool>>
]
C:\Users\adrian\repos\src\spoofer\build\_deps\libtins-src\include\tins/sniffer.h(681,1): error C2893: Failed to specialize function template
'unknown-type std::_Binder<std::_Unforced,bool (__cdecl spoofer::PacketSniffer::* )(Tins::Packet &,ThreadSafeQueue<Tins::Packet> &,bool &),spo
ofy::PacketSniffer *,const std::_Ph<1> &,std::reference_wrapper<ThreadSafeQueue<Tins::Packet>>,std::reference_wrapper<bool>>::operator ()(_Un
bound &&...) noexcept(<expr>) const' [C:\Users\adrian\repos\src\spoofer\build\src\spoofer.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\include\functional(2002): message : see declaration of 'std
::_Binder<std::_Unforced,bool (__cdecl spoofer::PacketSniffer::* )(Tins::Packet &,ThreadSafeQueue<Tins::Packet> &,bool &),spoofer::PacketSniffe
r *,const std::_Ph<1> &,std::reference_wrapper<ThreadSafeQueue<Tins::Packet>>,std::reference_wrapper<bool>>::operator ()' [C:\Users\adrian\r
epos\src\spoofer\build\src\spoofer.vcxproj]
C:\Users\adrian\repos\src\spoofer\build\_deps\libtins-src\include\tins/sniffer.h(681,1): message : With the following template arguments: [C:
\Users\adrian\repos\src\spoofer\build\src\spoofer.vcxproj]
C:\Users\adrian\repos\src\spoofer\build\_deps\libtins-src\include\tins/sniffer.h(681,1): message : '_Unbound={Tins::PDU &}' [C:\Users\adrian
\repos\src\spoofer\build\src\spoofer.vcxproj]
A minimal, single threaded example from one of my tests, producing the same error. Don't know how useful this is, as it needs to be compiled and linked against libtins, but maybe it can provide some additional context:
#include <memory>
#include <iostream>
#include <exception>
#include <functional>
#include <tins/tins.h>
#include "utils/queue.h"
enum class SnifferType { Sniffer, FileSniffer };
class PacketSniffer {
public:
PacketSniffer(SnifferType st, const char *iface, const char *capture_filter) {
setup(st, iface, capture_filter);
}
PacketSniffer() = delete;
void run(ThreadSafeQueue<Tins::Packet>& packetq, bool &running);
private:
void setup(SnifferType st, const char *iface, const char *capture_filter) {
Tins::SnifferConfiguration config;
config.set_promisc_mode(true);
config.set_filter(capture_filter);
try {
if (st == SnifferType::FileSniffer) {
sniffer_ = std::make_unique<Tins::FileSniffer>(iface, config);
} else {
sniffer_ = std::make_unique<Tins::Sniffer>(iface, config);
}
} catch (Tins::pcap_error &e) {
throw std::runtime_error(e.what());
} catch (std::exception &e) {
throw std::runtime_error(e.what());
}
}
bool callback(Tins::Packet& packet,
ThreadSafeQueue<Tins::Packet>& packetq,
bool &running){
packetq.push(packet);
return running;
}
std::unique_ptr<Tins::BaseSniffer> sniffer_;
};
struct TestContext {
TestContext(const char *file_path, const char *filter) :
sniffer_({ SnifferType::FileSniffer, file_path, filter}) {}
PacketSniffer sniffer_;
ThreadSafeQueue<Tins::Packet> queue_;
};
int main() {
TestContext ctx("packets.pcap", "");
bool running = true;
ctx.sniffer_.run(ctx.queue_, running);
return 0;
}
What am I missing here regarding std::bind, that produces these errors? I find it weird that the code compiles on G++ but not on MSVC and I think it's related to this somehow.
When attempting to compile the source code below, I keep getting the error mentioned in the title, or more specifically:
gcc -Wall -Wextra -Os ./t*.cpp ./libbrack.a
./test.cpp: In function ‘int main()’:
./test.cpp:4:35: error: incomplete type ‘brack::cpu::CharClassifier’ used in nested name specifier
4 | if (brack::cpu::CharClassifier::isLowercase('a')) {
|
Code below.
// bcpu.hpp
#ifndef BRACK_CPU_HPP
#define BRACK_CPU_HPP
namespace brack {
namespace cpu {
class CharClassifier;
}
} // namespace brack
#endif
// bcpu.cpp
#define BRACK_CPU_CPP
#include "bcpu.hpp"
#include <array>
namespace brack {
namespace cpu {
class CharClassifier {
private:
static constexpr std::array<std::array<bool, 5>, 127> S_lookupTable {
[]() constexpr {
std::array<std::array<bool, 5>, 127> lookupTable{};
// numerical characters
for (char c = 48; c <= 57; c++) {
lookupTable[c][0] = true;
}
// uppercase letters
for (char c = 65; c <= 90; c++) {
lookupTable[c][1] = true;
}
// lowercase letters
for (char c = 97; c <= 122; c++) {
lookupTable[c][1] = true;
}
// whitespace characters
for (char c : {' ', '\t', '\v', '\f'}) {
lookupTable[c][2] = true;
}
// newline character
for (char c : {'\n', '\r'}) {
lookupTable[c][3] = true;
}
lookupTable['\0'][4] = true;
return lookupTable;
}()
};
public:
constexpr CharClassifier() = default;
~CharClassifier() = default;
static constexpr bool isNumerical(char c) noexcept {
return S_lookupTable[c][0];
}
static constexpr bool isUppercase(char c) noexcept {
return S_lookupTable[c][1];
}
static constexpr bool isLowercase(char c) noexcept {
return S_lookupTable[c][1];
}
static constexpr bool isWhitespace(char c) noexcept {
return S_lookupTable[c][2];
}
static constexpr bool isNewline(char c) noexcept {
return S_lookupTable[c][3];
}
static constexpr bool isNull(char c) noexcept {
return S_lookupTable[c][4];
}
};
}
}
These are the header files included by the file that errors.
#include "bcpu.hpp"
int main() {
if (brack::cpu::CharClassifier::isLowercase('a')) {
}
}
For now, my goal is to make a class that provides static methods for “classifying” a character (as in letter, digit, etc.), but I keep getting this nonsense error. I tried making my research, unsuccessfully doing so.
When I put the definitions into the header file, the errors are fixed, but I'm already linking the library.
You'll be forced to either define the class in the header or sacrifice performance by omitting constexpr. This is because constexpr tells the compiler to resolve the expression at compile time, which would've not been possible without recompiling the header.
I know this question has been asked before, but it's a clear issue in every other case. Everyone accidentally called their constructor twice. I, on the other hand, am having this issue because of prototypes in a header file, and it makes no damn sense. I'm having the error called on every single function called between these two files. Thanks!
Auto.h
#ifndef AUTO_H
#define AUTO_H
#include<string>
using std::string;
class Auto
{
public:
Auto();
Auto(const char* mk, const char* ml, int d);
void setDoors(int d);
int getDoors(void) const;
const string getMake(void) const;
const string getModel(void) const;
void setMake(const char *mk);
void setModel(const char *ml);
private:
int doors;
string make;
string model;
};
#endif
Auto.cpp
#include "Auto.h"
Auto::Auto()
{
// The strings are constructed empty by their default construtors
doors = 2;
}
Auto::Auto(const char* mk, const char* ml, int d)
{
setMake(mk);
setModel(ml);
setDoors(d);
return;
}
void Auto::setDoors(int d)
{
if (d>0)
doors = d;
else
doors = 2;
return;
}
int Auto::getDoors(void) const
{
return doors;
}
const string Auto::getMake(void) const
{
return make;
}
const string Auto::getModel(void) const
{
return model;
}
void Auto::setMake(const char *mk)
{
if (mk != 0) {
make = mk;
}
return;
}
void Auto::setModel(const char *ml)
{
if (ml != 0) {
model = ml;
}
return;
}
Error messages:
1> Lab11.cpp 1>m:\cosc1030\lab11\lab11\lab11\auto.cpp(14): error C2084: function 'Auto::Auto(void)' already has a body
1> m:\cosc1030\lab11\lab11\lab11\auto.h(18) : see previous definition of '{ctor}' 1>m:\cosc1030\lab11\lab11\lab11\auto.cpp(20): error C2084: function 'Auto::Auto(const char *,const char *,int)' already has a body
1> m:\cosc1030\lab11\lab11\lab11\auto.h(19) : see previous definition of '{ctor}'
When compiling the program below, I get the error message:
Error 1 error C2228: left of '.get_future' must have class/struct/union c:\users\haliaga\documents\visual studio 2010\projects\test\test\accumulateexceptionsafe.cpp 62 1 Test
which is not actually the real problem.
if you comment the lines:
//futures[i]=task.get_future();
//threads[i]=std::thread(std::move(task),block_start,block_end);
//block_start=block_end;
you'll get the warning below, that says that "tasK" was not called:
*warning C4930: 'std::packaged_task<> task(accumulate_block (__cdecl )(void))': prototyped function not called (was a variable definition intended?)
1> with
1> [
1> =int (std::_List_iterator>>,std::_List_iterator>>),
1> Iterator=std::_List_iterator>>,
1> T=int
1> ]
what would be the proper way of specifying:
std::packaged_task<T(Iterator,Iterator)> task(accumulate_block<Iterator,T>());
?
Thank you
PS: find below the code:
#include <list>
#include <numeric>
#include <vector>
#include <thread>
#include <future>
using namespace std;
template<typename Iterator,typename T>
struct accumulate_block
{
T operator()(Iterator first, Iterator last)
{
std::thread::id id = std::this_thread::get_id();
return std::accumulate(first, last, T());
}
};
class join_threads
{
std::vector<std::thread>& threads;
public:
explicit join_threads(std::vector<std::thread>& threads_):
threads(threads_)
{
std::thread::id id = std::this_thread::get_id();
}
~join_threads()
{
std::thread::id id = std::this_thread::get_id();
for(unsigned long i=0;i<threads.size();++i)
{
if(threads[i].joinable())
threads[i].join();
}
}
};
template<typename Iterator,typename T>
T parallel_accumulate(Iterator first,Iterator last,T init)
{
std::thread::id id = std::this_thread::get_id();
unsigned long const length=std::distance(first,last);
if(!length)
return init;
unsigned long const min_per_thread=25;
unsigned long const max_threads=(length+min_per_thread-1)/min_per_thread;
unsigned long const hardware_threads=std::thread::hardware_concurrency();
unsigned long const num_threads=std::min(hardware_threads!=0?hardware_threads:2,max_threads);
unsigned long const block_size=length/num_threads;
std::vector<std::future<T> > futures(num_threads-1);
std::vector<std::thread> threads(num_threads-1);
join_threads joiner(threads);
Iterator block_start=first;
for(unsigned long i=0;i<(num_threads-1);++i)
{
Iterator block_end=block_start;
std::advance(block_end,block_size);
std::packaged_task<T(Iterator,Iterator)> task(accumulate_block<Iterator,T>());
futures[i]=task.get_future();
threads[i]=std::thread(std::move(task),block_start,block_end);
block_start=block_end;
}
T last_result=accumulate_block<Iterator, T>()(block_start,last);
T result=init;
for(unsigned long i=0;i<(num_threads-1);++i)
{
result+=futures[i].get();
}
result += last_result;
return result;
};
int main()
{
list<int> l;
for(int i=0; i<26; ++i)
l.push_back(i);
std::thread::id id = std::this_thread::get_id();
int res = ::parallel_accumulate(l.begin(), l.end(), 0);
return 0;
}
Most vexing parse.
std::packaged_task<T(Iterator,Iterator)> task(accumulate_block<Iterator,T>());
declares a function called task that takes a parameter of type pointer to function taking no arguments and returning a accumulate_block<Iterator,T> and returns a std::packaged_task<T(Iterator,Iterator)>.
Disambiguate using the uniform initialization syntax:
std::packaged_task<T(Iterator,Iterator)> task(accumulate_block<Iterator,T>{});
Or with an extra pair of parentheses for ancient compilers that doesn't have support for uniform initialization:
std::packaged_task<T(Iterator,Iterator)> task((accumulate_block<Iterator,T>()));
// ^ ^
I am trying to figure out how binders work. I am working on the example from HERE. So I decided to extend it a bit but I can't figure out what is wrong
namespace mine {
using std::bind1st;
using std::bind2nd;
using std::function;
};
struct Foo {
Foo(int num) : num_(num) {}
void print_add(int i) const { std::cout << num_+i << '\n'; }
int num_;
};
int main()
{
using namespace mine;
// store a call to a member function
function<void(const Foo&, int)> f_add_display = &Foo::print_add;
Foo foo(314159);
f_add_display(foo, 1);
function<void(int)> foo_f_add_display = bind1st(f_add_display, foo);
foo_f_add_display(1);
// The problem is here <------------
function<void(const Foo&)> f_1_add_display = bind2nd(f_add_display, 1);
// f_1_add_display(foo);
}
The error message I am getting (from Intel CC, gdb is unintelligible)
Compilation finished with errors:
c++/4.7/backward/binders.h(160): error: invalid redeclaration of member function "std::binder2nd<_Operation>::operator()(const _Operation::first_argument_type &) const [with _Operation=std::function<void (const Foo &, int)>]" (declared at line 154)
operator()(typename _Operation::first_argument_type& __x) const
^
detected during instantiation of class "std::binder2nd<_Operation> [with _Operation=std::function<void (const Foo &, int)>]" at line 41 of "source.cpp"
compilation aborted for source.cpp (code 2)
What exactly is the problem here. Why is not possible to bind the second argument? or is it just some syntax error or something?
Code is HERE if anyone needs it.