简体   繁体   English

如何创建从线程查询的全局可访问对象的持久列表?

[英]How can I create a persistent list of globally accesible objects queried from threads?

I've been trying to get a persistent object from a thread for hours.几个小时以来,我一直试图从线程中获取持久的 object。 I want to write a shared library in C++ that starts a persistent loop in a function.我想在 C++ 中编写一个共享库,它在 function 中启动一个持久循环。 In the following code snippets there is a class called Process.在以下代码片段中,有一个名为 Process 的 class。 Process initializes a TCP/IP interface to read and write data from a Simulink model.进程初始化 TCP/IP 接口以从 Simulink model 读取和写入数据。

This is only for declaration and should not be important for this problem, but now you know what I talk about when mentioning the processes.这仅用于声明,对于这个问题应该不重要,但是现在您知道我在提到流程时所说的内容。

main.cpp主文件

I know, it looks kinda ugly/unprofessional, but I'm fairly new to C++..我知道,它看起来有点丑/不专业,但我对 C++ 还很陌生。

// frustrated attempt to make everything persistent
static vector<std::thread> processThreads;
static ProcessHandle processHandle;
static vector<std::promise<Process>> promiseProcess;
static vector<std::future<Process>> futureProcess;

EXPORT int initializeProcessLoop(const char *host, int port)
{
    std::promise<Process> promiseObj;
    futureProcess.push_back(std::future<Process>(promiseObj.get_future()));
    processThreads.push_back(std::thread(&ProcessHandle::addProcess, processHandle, host, port, &promiseProcess[0]));

    Process val = futureProcess[0].get();

    processHandle.handleList.push_back(val);

    return (processHandle.handleList.size() - 1);
}

ProcessHandle.cpp进程句柄.cpp

The addProcess function from ProcessHandle creates the Process that should be persistent, adds it to a static vector member of ProcessHandle and passes the promise to the execution loop.来自 ProcessHandle 的 addProcess function 创建应该持久的进程,将其添加到 ProcessHandle 的 static 向量成员,并将 promise 传递给循环。

int ProcessHandle::addProcess(const char *address, int port, std::promise<Process> * promiseObj) {
    Process process(address, port);

    handleList.push_back(process);
    handleList[handleList.size() - 1].exec(promiseObj);
    return handleList.size() - 1;
}

To the main problem now...现在主要问题...

If I change "initializeProcessLoop" to include:如果我将“initializeProcessLoop”更改为包括:

    if(processHandle.handleList[0].isConnected())
    {
        processHandle.handleList[0].poll("/Compare To Constant/const");
    }

after i've pushed "val" to the processHandle.handleList everything works fine and I can poll the data as it should be.在我将“val”推送到 processHandle.handleList 之后,一切正常,我可以按应有的方式轮询数据。

If I instead poll it from - for examle - the main function, the loop crashes inside of the "initializeProcessLoop" because "Process val" is reassigned (?) with futureProcess[0].get().如果我改为从主 function 轮询它,则循环在“initializeProcessLoop”内部崩溃,因为“Process val”被重新分配(?)futureProcess[0].get()。

How can I get the Process variable and the threaded loop to be consistent after the function returns?在 function 返回后,如何使 Process 变量和线程循环保持一致?

If there are any questions to the code (and I bet there will be), feel free to ask.如果对代码有任何问题(我打赌会有),请随时提问。 Thanks in advance!提前致谢!

PS: Obligatory "English is not my native language, please excuse any spelling errors or gibberish"... PS:强制性“英语不是我的母语,请原谅任何拼写错误或乱码”...

Okay, first I have to declare, that the coding style above and following are by any means not best practice.好的,首先我必须声明,上面和下面的编码风格无论如何都不是最佳实践。

While Sam Varshavchik is still right with how to learn C++ the right way, just changing虽然 Sam Varshavchik 在如何以正确的方式学习 C++ 方面仍然是正确的,只是改变

Process val = futureProcess[0].get();

to

static Process val = futureProcess[0].get();

did the job.做了这项工作。

To be clear: don't do this.要明确:不要这样做。 It's a quick fix but it will backfire in the future.这是一个快速修复,但它会在未来适得其反。 But I hope that it'll help anyone with a similar problem.但我希望它能帮助任何有类似问题的人。

If anyone has a better solution (it can't get any worse, can it?), feel free to add your answer to this question.如果有人有更好的解决方案(它不会变得更糟,可以吗?),请随时添加您对这个问题的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM