Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 361647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:54:12+00:00 2026-05-12T12:54:12+00:00

I have a statemachine in a real-time system with very few (3) states. typedef

  • 0

I have a statemachine in a real-time system with very few (3) states.

typedef enum {
    STATE1,
    STATE2,
    STATE3
} state_t;

However, the transitions between those states need considerable time and have their own subdivisions. So I have two choices, either I extend the main statemachine such that all the intermediate states are represented:

typedef enum {
    STATE1,
    STATE1_PREPARE_TRANSITION_TO_STATE2,
    STATE1_DO_TRANSITION_TO_STATE2,
    STATE1_PREPARE_TRANSITION_TO_STATE3,
    STATE1_DO_TRANSITION_TO_STATE3,
    STATE2,
    ...
} state_t;

or I create a nested statemachine for the relevant main states:

typedef enum {
    STATE1_NOT_ACTIVE,
    STATE1_NORMAL,
    STATE1_PREPARE_TRANSITION_TO_STATE2,
    STATE1_DO_TRANSITION_TO_STATE2,
    STATE1_PREPARE_TRANSITION_TO_STATE3,
    STATE1_DO_TRANSITION_TO_STATE3
} sub_state1_t;
...

Both possibilities have their advantages and drawbacks. The big statemachine gets messy and complicated very easily. However, having all the states consistent in the second case isn’t trivial either and many functions would need information about both the global state and the substates.

I’d like to avoid complicated code which has to handle several parallel states, like:

if ((global_state == STATE1) &&
    (sub_state_1 == STATE1_DO_TRANSITION_TO_STATE2))
{
    ...
    if (transition_xy_done(...))
    {
        global_state = STATE2;
        sub_state_1 = STATE1_NOT_ACTIVE;
        sub_state_2 = STATE2_NORMAL;
    }
}

What is the general best approach for such a problem: many small and nested statemachines (with many invalid combinations), one big statemachine or anything else?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:54:12+00:00Added an answer on May 12, 2026 at 12:54 pm

    First, I want to commend you for recognizing what’s happening and making these states explicit (since they are in fact additional states in your model, not really transitions with an action). Far too often I see state machines that end up like your last example (that you want to avoid). When you have tests for ‘additional’ state variables inside your event handlers, it’s a sign that your state machine has more states that you’ve really put into the design – those show be reflected in the design, not jammed into the existing state’s event handlers with a bunch of spaghetti coded checks for additional ‘state’ encoded in global variables.

    There are several frameworks for C++ that model hierarchical state machines – HSMs – (which is what your nested state machine idea sounds like), but the only one I’m aware of that supports straight C is Quantum Framework, and I think that buying into that would probably mean a decent level of commitment (i.e., it’s probably not a simple change). However, if you want to look into this possibility, Samek has written a lot of articles (and a book) about how to support HSMs in C.

    However, if you don’t need some of the more sophisticated parts of the HSM models (such as events that aren’t handled by the ‘innermost’ state get bubbled up to be possibly handled by parent states, full enter and exit support for the entire state hierarchy), then it’s pretty easy to support nested state machines just as completely independent state machines that happen to start and stop when a parent state is entered/exited.

    The big state machine model is probably a bit easier to implement (it’s just several more states in your existing framework). I’d suggest that if adding the states to your current state machine mode doesn’t make the model too complex, then just go with that.

    In other words, let what works best for your model drive how you implement the state machine in software.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a StateMachine table having all the states in a key, value pair
I'd like to have any Form managed through the StateMachine generated from LWUIT's 1.5
have written this little class, which generates a UUID every time an object of
Have deployed numerous report parts which reference the same view however one of them
I have a state machine with many states A--B--C--D--E . I have many transitions
I am creating a custom statemachine and in order to be determinist, I have
I have a state machine workflow of 2 states. State 1 has 2 transitions
I have been using State machine based design tools for some time, and have
I have some trouble with using enum types in signals. Basicly I have two
I have implemented a simple state machine for an embedded system using C's switch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.