尝试使用线程时出现错误
errors when trying to work with threads
大家好,希望一切都好。我正在尝试在c++中使用线程。我编译了代码,得到了以下错误消息。
C:UsersPeretzDocumentskeyloggerKeyConstants.h|183|warning: unknown escape sequence: '|' [enabled by default]|
C:UsersPeretzDocumentskeyloggerTimer.h|10|error: 'thread' in namespace 'std' does not name a type|
C:UsersPeretzDocumentskeyloggerTimer.h||In member function 'void Timer::SleepAndRun()':|
C:UsersPeretzDocumentskeyloggerTimer.h|25|error: 'std::this_thread' has not been declared|
C:UsersPeretzDocumentskeyloggerTimer.h||In member function 'void Timer::Start(bool)':|
C:UsersPeretzDocumentskeyloggerTimer.h|71|error: 'Thread' was not declared in this scope|
C:UsersPeretzDocumentskeyloggerTimer.h|71|error: 'thread' is not a member of 'std'|
C:UsersPeretzDocumentskeyloggerTimer.h||In member function 'void Timer::Stop()':|
C:UsersPeretzDocumentskeyloggerTimer.h|82|error: 'Thread' was not declared in this scope|
||=== Build failed: 5 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
我在Youtube上寻找c++和c++11中的线程,但找不到答案。请解释为什么会有这些错误。
#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
error: 'thread' in namespace 'std' does not name a type
无论您使用哪种编译器(看起来像Visual c++)都不支持std::thread
(这是c++ 11的一部分)。
相关文章:
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++使用params创建线程函数会导致转换错误
- 是什么导致了Unity 3D中的"错误线程异常"?
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 尝试从头开始实现Leetcode的FizzBuzz多线程问题。收到"libc++abi.dylib: terminating"错误
- 线程时访问静态映射时出现隔离错误
- 多线程减慢程序速度:无错误共享,无互斥锁,无缓存未命中,无小工作量
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- C++多线程程序:变量定义为类成员的隔离错误
- 当 2 个线程共享同一物理内核时,具有错误共享的易失性增量在发布中的运行速度比在调试中慢
- 线程 std::调用未知类型,无法专门化函数错误
- 停止 winapi 线程时出现错误行为
- C++线程错误
- C++线程错误:没有名为"类型"MINGW 的类型
- 可中断线程类 C++11 - 遇到错误?
- 分离的线程:在繁忙时销毁互斥锁 错误C++
- 为同一向量C++运行多个线程时出现分段错误
- 查找最低值.功能.错误:线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x7fff5fc89000)
- MINGW错误:“线程”不是“ STD”的成员