不断检查C 中的变量

constantly checking a variable in c++

本文关键字:变量 检查      更新时间:2023-10-16

我检查了很多帖子,但是不确定答案是否与我想做的事情有关。我只能理解的是使用线程。

我想拥有一个服务器对象,该对象不断检查客户端对象中的布尔值,如果它是真正的服务器,则应做某事并将标志转换为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;
}

希望有帮助,问我是否有任何疑问