【问题标题】:How to implement two thread one of which runs an infinite state Machine Loop如何实现两个线程,其中一个运行无限状态机循环
【发布时间】:2022-12-19 15:34:44
【问题描述】:

我有一个具有 7 个状态的状态机的设备,即:待机、连接、操作等。它必须在一个名为“Device Thraed”的线程中实现,我有一个类 SerialPortIO,它读取/写入串行端口,必须在另一个线程中实现。 我制作了一些线程并将类移动到线程中。

我尝试从串行端口向设备发送信号并更改状态。但是没有发送信号!!!!

如何使用 QThread 实现我的软件?

我将它们实现为一个类,状态机处于无限 While 循环中。

while (true)
{
  switch (mode){
   case StandBy:
   break;
   case Connected:
   break;
}
}

【问题讨论】:

标签: c++ multithreading qt infinite-loop


【解决方案1】:

下面是一个如何实现两个线程的示例,一个用于状态机循环,另一个用于 SerialPortIO 类:

#include <iostream>
#include <thread>
#include <atomic>

using namespace std;

// Define the different states of the state machine
enum States {
    Standby,
    Connected,
    Operation,
    // Add other states here
};

// The state machine class
class StateMachine {
public:
    // The state machine loop
    void run() {
        while (true) {
            // Lock the mutex to ensure that the state is not modified by another thread
            lock_guard<mutex> lock(stateMutex);

            // Switch on the current state
            switch (state) {
                case Standby:
                    // Implement the Standby state here
                    break;
                case Connected:
                    // Implement the Connected state here
                    break;
                case Operation:
                    // Implement the Operation state here
                    break;
                // Add other states here
            }
        }
    }

    // Function to set the state of the state machine
    void setState(States newState) {
        // Lock the mutex to ensure that the state is not modified by another thread
        lock_guard<mutex> lock(stateMutex);

        // Set the state
        state = newState;
    }

private:
    // The current state of the state machine
    States state = Standby;

    // Mutex to protect the state variable
    mutex stateMutex;
};

// The SerialPortIO class
class SerialPortIO {
public:
    // Function to read from the serial port
    void read() {
        // Implement the read function here
    }

    // Function to write to the serial port
    void write() {
        // Implement the write function here
    }
};

int main() {
    // Create the state machine object
    StateMachine stateMachine;

    // Create the SerialPortIO object
    SerialPortIO serialPortIO;

    // Create a thread to run the state machine loop
    thread stateMachineThread(&StateMachine::run, &stateMachine);

    // Create a thread to read from the serial port
    thread serialPortIOReadThread(&SerialPortIO::read, &serialPortIO);

    // Create a thread to write to the serial port
    thread serialPortIOWriteThread(&SerialPortIO::write, &serialPortIO);

    // Wait for the state machine thread to finish
    stateMachineThread.join();

    // Wait for the serial port IO read thread to finish
    serialPortIOReadThread.join();

    // Wait for the serial port IO write thread to finish
    serialPortIOWriteThread.join();

    return 0;
}

为了进一步说明,在上面提供的代码示例中,StateMachine 类有一个包含无限循环的运行函数。在循环内部,使用 switch 语句检查状态机的当前状态,并根据当前状态采取适当的操作。

通过创建线程对象并将 StateMachine::run 函数作为参数传递,运行函数在单独的线程中运行。这允许状态机循环与程序中的其他线程同时运行。

SerialPortIO 类具有读取和写入串口的函数。这些函数也使用线程对象在单独的线程中运行。

在主函数中,创建了 stateMachine 和 serialPortIO 对象,并创建了线程来运行 StateMachine::run、SerialPortIO::read 和 SerialPortIO::write 函数。这允许状态机循环、串行端口读取和串行端口写入操作同时运行。

【讨论】:

  • 非常感谢。你知道如何用 QThread 和 Signal_Slot 概念实现吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2019-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多