Google Answers Logo
View Question
 
Q: Programming. Threads and internal communication in Visual C++ ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Programming. Threads and internal communication in Visual C++
Category: Computers > Programming
Asked by: goodwasabi-ga
List Price: $10.00
Posted: 01 Apr 2004 01:32 PST
Expires: 01 May 2004 02:32 PDT
Question ID: 323424
I am developing a software interface between two systems using Visual
C++ (microsoft). According to the specifications I will be writing a
series of independent processes (a total of 12, each responsible for a
certain functionality) that will interact among each other.

It was suggested to build each processes as threads. Each process
should be able to "talk" to any other process at any given time so
there will be the need for:


-- basic comunication functions (send message to other process,
receive message from other process)
-- an incoming message queue for concurrency
-- a way to identify each thread

These functions will be common to all processes since they will all be
structured in the same way, they will only differ for the
functionality they implement.

My question is:

I am looking for a library/resource that gives me these "containers"
that already have the inner comunication functionality, so I can just
replicate them and add the extra functions to meet my needs.

Request for Question Clarification by maniac-ga on 01 Apr 2004 05:42 PST
Hello Goodwasabi,

Just a few comments on what you describe and then I'll ask for the clarifcation.

You appear to use processes and threads as equivalent terms. However,
a process (or application) has a memory space with one or more threads
of execution. So two processes will not share memory (unless you take
some specific action) but two threads will.

I will use the phrase "task" below to describe a chunk of work /
software that needs to be done (by a thread or process).

Do you want the two tasks to share memory or just the data being exchanged?
Do you want an error in one task to cause the entire application (all
tasks) to abort or just one?
Are you developing an application that may be migrated to Unix / Linux systems?

Answers to the first two will help determine if you should use threads
(or not). If the third answer is yes, it limits the solutions to
methods that work on both Windows and Linux. There are certainly a
number of good solutions to this type of problem - but please clarify
your situation so we get you the "right choice". For example, sockets
work good with communicating processes and is portable to both types
of systems, but has some overhead you might not be willing to absorb.
Thread safe queues can be used in threads but almost never work
between processes. [I can explain why if needed]

  --Maniac

Clarification of Question by goodwasabi-ga on 01 Apr 2004 09:21 PST
Sorry for the misunderstanding. 

I see no need to share memory. 

All tasks should all be quite independent, I can't afford to make the
whole system sequential.

If one task encounters an error it will decide what action to take (if
to stop the other tasks or go on and ignore it).

I should be able to define what to do if a single task crashes, if the
crash will affect the whole system, the entire system should be
restarted but if I know that the task is not essential I should be
able to just restart it.

I see no need to port the program to Unix/Linux at this time, esp. if
it requires extra overhead. This program will have to be processor
efficient. It will be running on a computer that is already stressed
by another application that does PLC scanning, so I can't take up too
much processor time.

The idea of internal messaging+queuing was intended so that I would
have no concurrency issues, but maybe the same can be obtained if each
task exposes functions instead of accepting messages. When one task
wants to communicate with another they would just call the appropriate
function. Does this make sense?

Request for Question Clarification by maniac-ga on 01 Apr 2004 10:38 PST
Hello Goodwasabi,

>All tasks should all be quite independent, I can't afford to make the
>whole system sequential.
That is not a problem with either threads or processes.

>If one task encounters an error it will decide what action to take (if
>to stop the other tasks or go on and ignore it).
>
>I should be able to define what to do if a single task crashes, if the
>crash will affect the whole system, the entire system should be
>restarted but if I know that the task is not essential I should be
>able to just restart it.
If this is important - I would rule out threads. A thread crash (e.g.,
segmentation violation) would basically take out the whole process. If
you use separate processes, you have the opportunity to recover.

>I see no need to port the program to Unix/Linux at this time, esp. if
>it requires extra overhead. This program will have to be processor
>efficient. It will be running on a computer that is already stressed
>by another application that does PLC scanning, so I can't take up too
>much processor time.
I will keep the CPU load in mind.
Do you have any idea how much data will be passed around between the
tasks and the rates of transfer?

>The idea of internal messaging+queuing was intended so that I would
>have no concurrency issues, but maybe the same can be obtained if each
>task exposes functions instead of accepting messages. When one task
>wants to communicate with another they would just call the appropriate
>function. Does this make sense?
Yes it does - but if I was designing your system, I would encapsulate
the exchange of data or somehow manage the execution of tasks to limit
the scope of concurrency issues anyway. To explain the latter part
more fully - if you have a set of cyclic tasks, a simple architecture
is to gather the data [input phase], process the data [processing
phase], and then move the data to output buffers [output phase]. You
do not allow any task processing to start until all tasks have
completed their inputs, helping to prevent corruption of data.

I will be looking into some alternatives and get back later today.
  --Maniac
Answer  
Subject: Re: Programming. Threads and internal communication in Visual C++
Answered By: maniac-ga on 01 Apr 2004 16:50 PST
Rated:5 out of 5 stars
 
Hello Goodwasabi,

There are a number of good interprocess communication packages
available on Windows with code callable from C or C++. A few examples
follow:

These examples supports a "client / server" model where you have a
single server and multiple clients. Includes some description as well
as sample Visual C projects.
  http://www.codeproject.com/threads/sharedmemipc.asp
  http://www.codeproject.com/threads/ipc_tute.asp
According to the documention, runs on both Windows 95/98 as well as NT
based systems. Go up a level to
  http://www.codeproject.com/threads/
to get a LONG list of related articles and sample code. As another example:
  http://www.codeproject.com/threads/xylock.asp
talks about C++ thread safe code and the efficiency of the methods he uses.

Sample source code using several different methods
  http://www.sabanciuniv.edu/kcenter/En/preview/IT532a-ipc-5-src.pdf

A general package called MPI (Message Passing Interface), commonly
used in large numerical analysis systems. Implementations for several
operating systems exist - it also allows you to split your job into
several pieces across several machines.
  http://www-unix.mcs.anl.gov/mpi/mpich/
  http://www-unix.mcs.anl.gov/mpi/mpich/mpich-nt/
Note that MPICH is not thread safe and must be used between multiple
processes (or systems). It does come with extensive documentation and
is very efficient.

Another package for IPC in C++; shareware - $49.
  http://www.programmersheaven.com/zone3/cat626/28342.htm

A 25 page article titled "C++ Class Libraries for Interprocess
Communication", includes source code examples:
  http://www.cs.wustl.edu/~schmidt/PDF/netcomp.pdf
or go up to
  http://www.cs.wustl.edu/~schmidt/
for the authors main page which includes a number of references to C++ guides.

On line documentation at Microsoft on the MFC thread related functions
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_application_and_thread_support_classes.asp

A presentation summarizing a few methods, specific to Windows systems:
  http://www.sabanciuniv.edu/kcenter/En/preview/IT532a-ipc-5.pdf
It also talks about how child processes can be created / some of the
implications (e.g., sharing semaphores).

A paper describing a few methods for both Windows NT an Unix systems is at
  http://www.cs.brown.edu/people/scl/files/IPCWinNTUNIX.pdf
which describes Pipes, Sockets, Shared Memory, and Mailslots. All but
the last are implemented on both Windows and Unix / Linux. The paper
also has some recommendations near the end on which one you should use
for what kind of application.

To find more examples, try searches such as
  windows interprocess communication
  windows interprocess communication source code
  windows interprocess communication C++ source code
  windows [IPC method here] source code
and so on.

If this information is not sufficient for your needs or is somehow
unclear, please use a clarification request so I can fix the answer.

  --Maniac
goodwasabi-ga rated this answer:5 out of 5 stars
excellent information, just what I needed, thank you. M

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy