不断检查C 中的变量
constantly checking a variable in c++
我检查了很多帖子,但是不确定答案是否与我想做的事情有关。我只能理解的是使用线程。
我想拥有一个服务器对象,该对象不断检查客户端对象中的布尔值,如果它是真正的服务器,则应做某事并将标志转换为false。
我希望该检查过程平行于我的主体。BTW服务器和客户端也必须做其他事情,我不想将其与检查客户端标志的函数相同的线程
编辑:我需要拥有服务器和客户端的多个对象
所以这是我在C 代码中的意思:main.cpp
#include <iostream>
using namespace std;
class Client
{
public:
bool flag;
void setFlag(bool flagStat)
{
flag = flagStat;
}
bool getFlag()
{
return flag;
}
};
class Server
{
public:
void checkClientFlag(Client *client)
{
while (true)
{
if (client->getFlag())
{
alarmServer();
client->setFlag(false);
}
}
}
void alarmServer()
{
cout << "Client Flag UP!!!" << endl;
}
};
int main()
{
Server *newSrv = new Server;
Client *newCnt = new Client;
newSrv->checkClientFlag(newCnt);
return 0;
}
因此,请求帮助。预先感谢您。
您可以使用将为您处理此检查的条件变量。
这个想法是,您有一个设置为true/flase的标志,具体取决于是否完成了工作(设置为false,并在完成工作方法时将设置为true)。
等待方法意味着程序将等待直到填充某个条件。您使用通知将此条件设置为true
server.h
#include <condition_variable>
#include <mutex>
class CServer
{
std::condition_variable & cv;
std::mutex & mut;
bool & flag;
public:
~CServer(void);
CServer::CServer(std::condition_variable & cv,
std::mutex & mut,
bool & flag);
CServer::CServer(CServer &);
int Notify();
int DisplayNotification();
std::condition_variable & getCondVar(){return cv;}
std::mutex & getMutex(){return mut;}
bool & getFlag(){return flag;}
};
server.cpp
#include "Server.h"
#include <iostream>
using namespace std;
CServer::~CServer(void)
{
}
CServer::CServer(std::condition_variable & cv_,
std::mutex & mut_,
bool & flag_):
cv(cv_),
mut(mut_),
flag(flag_)
{
}
CServer::CServer(CServer &toCopy):
cv(toCopy.getCondVar()),
mut(toCopy.getMutex()),
flag(toCopy.getFlag())
{
flag=false;
cout<<"Copy constructor"<<std::endl;
}
int CServer::Notify()
{
{
std::lock_guard<std::mutex> lk(mut);
flag=true;
std::cout << "ready for notfication"<<endl;
}
cv.notify_one();
return 0;
}
int CServer::DisplayNotification()
{
// wait for the worker
{
std::unique_lock<std::mutex> lk(mut);
cv.wait(lk, [this]{return this->getFlag();});
}
cout<<"Notification displayed"<<endl;
return 0;
}
客户端
#include <chrono>
#include "Server.h"
class CClient
{
CServer & serv;
std::chrono::seconds sleepTime;
bool finishedWork;
public:
CClient(CServer & serv,
std::chrono::seconds sleepTime);
~CClient(void);
int work();
};
client.cpp
#include "Client.h"
#include <thread>
using namespace std;
CClient::CClient(CServer & serv_,
std::chrono::seconds sleepTime_):
serv(serv_),
sleepTime(sleepTime_),
finishedWork(false)
{
}
CClient::~CClient(void)
{
}
int CClient::work()
{
this_thread::sleep_for(sleepTime);
finishedWork=true;
serv.Notify();
return 0;
}
主要程序是
#include "Client.h"
#include <thread>
using namespace std;
int main()
{
std::chrono::seconds sleepTime=std::chrono::seconds(10);
//create client and server
condition_variable cv;
mutex mut;
bool flag=false;
CServer serv(cv,mut, flag);
CClient cli(serv,sleepTime);
//thread with the server
thread thServ(&CServer::DisplayNotification,serv);
////thread with the client
thread thCli (&CClient::work,cli);
////join threads
thServ.join();
thCli.join();
return 0;
}
希望有帮助,问我是否有任何疑问
相关文章:
- 试图让变量检查数组中的某些内容
- 如何检查变量是否是C++中的地图?
- 运行时检查失败 #2 变量"A"周围的堆栈已损坏
- 成员变量与函数概念检查
- 为什么我的 c++ 程序检查不是初始化的变量?
- 如何更好地检查两个 char 变量是否在一组值中?
- 如何检查模板类中的变量是否为字符串类型?
- 如何检查 int 变量是否包含合法(非陷阱表示)值?
- 运行时检查失败 #2 - 变量"e"周围的堆栈已损坏。发生
- 使用 gtest 框架在单元测试代码中检查目标对象的私有变量的最佳实践是什么?
- 是否有一个库可以检查C++中的变量类型?
- 有没有办法检查发送到变量的值是否是正确的类型,而它已经在该变量下?
- 检查私有成员变量是否在一定范围内,如果没有调整
- 如何使用 ifstream 检查文件中变量的类型?
- C++ 如何检查 char 变量是否未定义(未初始化)
- 是否可以检查变量是否位于 L1/L2/L3 缓存中
- 运行时检查失败 #2 - 变量周围的堆栈'...'已损坏
- if() 跳过我的变量检查
- 使用整型变量检查用户输入是整型还是字符串
- Makefile.am /配置.ac条件环境变量检查