473,378 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

C++ Message Handling without using state machines

Hello,

I'm writing code and I have a scenario where I'm waiting to receive a
msg and a timeout message at the same time. For example, I've set a
timer so that
if I don't receive a message in a certain time, then I will receive a
timeout
message. Based on which message I receive first I will perform certain
tasks. Is
there a good way to handle this in C++ without using statemachines. If
I receive the
timeout message first then what do I do when the other message comes in
or vice-versa.
Need to determine a way to find out, once I receive a message, if the
other
message has been received yet. Any advice would be greatly appreciated.

thanks,
Libster

Jun 30 '06 #1
4 2072
"libster" <oh********@aol.com> wrote:
I'm writing code and I have a scenario where I'm waiting to receive
a msg and a timeout message at the same time.
That's a little superfluous, isn't it? I mean, a "timeout message"
already IS a "message". So what you really meant is, "I'm waiting
[callback function?] to receive messages, which may be timeout or
other".
For example, I've set a timer so that if I don't receive a message
in a certain time, then I will receive a timeout message. Based on
which message I receive first I will perform certain tasks. Is
there a good way to handle this in C++ without using state machines?
I'd say a function which processes incoming messages and alters the
state of a system based on what messages it receives already IS a
"state machine". Why are you afraid of that term?
If I receive the timeout message first then what do I do when the
other message comes in or vice-versa? Need to determine a way to
find out, once I receive a message, if the other message has been
received yet. Any advice would be greatly appreciated.


Use static bool variables to keep track of whether certain messages
have been received yet by a function. Sort of like:

int
__stdcall
MyCallbackFunction
(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{
static bool GotTimeout = false;
static bool GotSleep = false;
static bool GotWakeup = false;
static bool GotExplode = false;
switch (message)
{
case TIMEOUT:
GotTimeout = true;
// do stuff
return 1;
case SLEEP:
GotSleep = true;
// do stuff
return 1;
case WAKEUP:
GotWakeup = true;
// do stuff
return 1;
case EXPLODE:
bomb.explode();
return 666;
}
return 0;
}

You'll have to decide under what conditions you want to reset
your "got-the-message" booleans so that they can be triggered
by the next message of that type. But I think the above scheme
would work.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net (put "[ciao]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/

Jun 30 '06 #2
On Fri, 30 Jun 2006 09:37:42 GMT, Robbie Hatley <bo***********@no.spamwrote:
"libster" <oh********@aol.comwrote:
....
>For example, I've set a timer so that if I don't receive a message
in a certain time, then I will receive a timeout message. Based on
which message I receive first I will perform certain tasks. Is
there a good way to handle this in C++ without using state machines?

I'd say a function which processes incoming messages and alters the
state of a system based on what messages it receives already IS a
"state machine".
Yes.
Why are you afraid of that term?
I think he's afraid of the usual, brittle and hard-to-read implementation
with callbacks, switch statements, flags and so on, which you examplify below
(snipped).

I don't know, because I haven't had to do complex state machine stuff ...
but I expect that you can do better than that (more explicitly, more type
safety and other compile-time checks, and so on) with C++.

- Is there something at boost.org, maybe?
- Is there a design involving clever template abuse^Wmetaprogramming?

It wouldn't surprise me if you can make state machines much less tedious and
difficult in C++ without resorting to code generators and stuff.

Not an answer, but I've clarified what the OP was asking for. I hope.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org R'lyeh wgah'nagl fhtagn!
Jul 2 '06 #3
"Jorgen Grahn" wrote:
I think he's afraid of the usual, brittle and hard-to-read
implementation with callbacks, switch statements, flags and
so on, which you examplify below.
That's all just part of processing messages. Goes with the
territory. Try writing Microsoft Windows window procs.
(Or worse, communications-parsing functions.) (Or worst of
all, a fully-standard-complient C++ compiler! Not that I've
ever done that.) You'll get your fill of state machines in
any of those cases.
I don't know, because I haven't had to do complex state machine
stuff
This is one area where a flow chart helps enormously.
but I expect that you can do better than that (more explicitly,
more type safety and other compile-time checks, and so on) with
C++.

Is there something at boost.org, maybe?
Depends on what the OP wants to do. Sounds like something
very application-specific, rather than something that's going
to be in a library. I mean, come on, what library is going
to have a class called:

GeneralElectricModel4856RadioReceptionFilterStateM achineClass

Not.

Face it, sometimes you just have to roll your own.
Is there a design involving clever template abuse [or]
metaprogramming?
I don't see how metaprogramming is going to help, as that's
purely compile-time, whereas state machines and message
processers are always very real-time by nature.
It wouldn't surprise me if you can make state machines much
less tedious and difficult in C++ without resorting to code
generators and stuff.
The only code generator I've ever used to create state machines
and message processors is my brain. :-)

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 3 '06 #4
In message <tX********************@newssvr13.news.prodigy.com >, Robbie
Hatley <bo***********@no.spamwrites
>"Jorgen Grahn" wrote:
[...]
>>
Is there something at boost.org, maybe?

Depends on what the OP wants to do. Sounds like something
very application-specific, rather than something that's going
to be in a library. I mean, come on, what library is going
to have a class called:

GeneralElectricModel4856RadioReceptionFilterState MachineClass

Not.
You're a victim of premature specialisation ;-)

There might well be a library with a StateMachine _framework_ which
defines suitable types for states, messages, transitions etc.
>
Face it, sometimes you just have to roll your own.
>Is there a design involving clever template abuse [or]
metaprogramming?

I don't see how metaprogramming is going to help, as that's
purely compile-time, whereas state machines and message
processers are always very real-time by nature.
But the set of states, the set of possible messages, the table of
transitions etc. are all known at compile time. There might well be
metaprogramming tricks to simplify generating those things.
>
>It wouldn't surprise me if you can make state machines much
less tedious and difficult in C++ without resorting to code
generators and stuff.

The only code generator I've ever used to create state machines
and message processors is my brain. :-)
Indeed.

--
Richard Herring
Jul 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Leonard J. Reder | last post by:
Hello list, I have been searching on the web for a while now for a specific Python implementation of an FSM. More specifically what I am looking for is a Python implementation of the so called...
12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
2
by: genc_ymeri | last post by:
Hi, We are a team of 4 using win2003 server (sp1) in our dev machines and VS2003 for our .Net web app. We did make a copy of our project (located to one "hardwarely old" PC ) to our machines. All...
11
by: Deiter | last post by:
State Machines and Coroutines The other thread on goto: Lead me to want to ask... In the spirit of state machines and coroutines, This n00b to C would like to know if setjmp/longjmp are the only...
16
by: NewToCPP | last post by:
I have seen at several places that C++ programmers writing for RealTime Embedded applications dont use Exception Handling. They dont like Throw/catch concept. WHY? Thanks.
10
by: subramanian | last post by:
Consider the following code: segment violation is deliberately generated to catch the SIGSEGV signal. The handler for this signal, namely SIGSEGV_handler, is called. However after the handler is...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
67
by: Rui Maciel | last post by:
I've been delving into finite state machines and at this time it seems that the best way to implement them is through an intense use of the goto statement. Yet, everyone plus their granmother is...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.