首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不断检查c++中的变量

不断检查c++中的变量
EN

Stack Overflow用户
提问于 2014-01-24 07:23:51
回答 1查看 1.2K关注 0票数 1

我查看了相当多的帖子,但不能确定答案是否与我想做的事情有关。我所能理解的就是使用线程。

我希望有一个服务器对象,它不断地检查客户机对象中的布尔值,如果是真的,服务器应该做一些事情,并将标志设置为false。

我希望这个检查过程与我的main并行完成。顺便说一句,服务器和客户机还必须做其他事情,我不想把它们放在检查客户机标志的函数所在的线程中

编辑:我需要服务器和客户端的多个对象

这就是我在C++代码中的意思: main.cpp

代码语言:javascript
运行
复制
#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;
}

因此,我们非常感谢您的帮助。提前谢谢你。

EN

回答 1

Stack Overflow用户

发布于 2014-01-27 08:00:34

您可以使用将为您处理此检查的条件变量。

这个想法是,您有一个标志,它根据工作是否已经完成而设置为true/flase (设置为false,当工作方法完成时将设置为true )。

wait方法意味着程序将等待,直到某个条件被满足。您可以使用notify将此条件设置为true

Server.h

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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;
}

Client.h

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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;
}

主程序是

代码语言:javascript
运行
复制
#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;
}

希望这对你有帮助,如果你有任何问题,请问我

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21321430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档