Using threads and finite state machines to control script synchronization
Posted on 2007-10-09
I have a question (or need for advice) on thread programming that isn't covered by the
I'm a newbie to thread programming. I only worked on one lab project involving Windows
threads in C++ back in college. I like using C# as well but haven't gotten to thread
programming in that language.
It probably behaves like a finite state machine as well, which I've never programmed in C++
or C#. I've only used Verilog for that.
At my workplace, we have an in house tool for synchronizing the execution of certain
commands (in custom script files) using threads. The tool is based on the Intel Dialogic API
and is for phone/PBX testing. It is closely tied to the Dialogic hardware. So I am planning
on writing a subset port of the tool that supports the synchronization (the current tool has
a lot more features than that).
However, I hit some stumbling blocks:
1. The developer of the tool left years back. I managed to find the C++ source code but it
is sparsely commented, somewhat cryptic, and there is no formal documentation. It's well
object oriented but too much such that I can't find the code of interest. I'm still
attempting to analyze it when I have time.
2. The tutorials on the web for threads in C++ or C# don't seem to cover what I'm looking
So I'm hoping someone here can advise me on how I should approach this and where I can find
the info necessary to build such a tool. If possible, I'd like to hear of possible
approaches in both C++ and C#. A cross-platform type of solution would be nice as well, but
I just need it for Windows for now.
Here's what I'm trying to accomplish:
We start with a script file like the following
begin script file
201; EXECUTE; "command here"
201; RELEASE; 202
202; EXECUTE; "command here"
202; RELEASE; 201
end script file
In this script, 201 & 202 execute in parallel or simultaneously. Because 201 is set to wait
state, only 202 starts initially when the script runs. Later, 202 releases 201, so it can
run. Next sequence for 202 is a wait state as well, so 201 & 202 wait on each other before
execution because the command they execute may depend on the result of the other. However,
this isn't always the case, there may be cases where 202 releases 201 and both then continue
to run as 201 initially depends on 202. And vice versa.
The script file example above is read in by the tool and parsed (into a finite state
machine?) as some data structure. Threads are created for each ID (201, 202, etc.) and the
appropriate data structure is assigned to each thread.
Throughout the lifecycle of each thread, there are 3 things it can do: wait until another
thread releases it, release another thread (by some ID), or execute a command. The command
is simply a system call to the command shell to execute an external program or script.
I can handle the script parsing, data structure setup, and command execution, but I have no
idea where or how to start on the thread synchronization. It doesn't look like a simple
mutex or semaphore will do to control the synchronization per the scripts behavior.