C++ design question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: C++ design question

  1. #1
    Ken F Guest

    C++ design question


    Hi,

    When designing an application that is multiple threaded and programming with
    C++, I was wondering is there an optimal approach? I have an object Time
    that time stamps each record that passes through my server. Is it, better
    to make the Time object a singleton pattern and put locks around the critical
    sections? Or, create a Time object in each method that is going to do the
    time stamps. Creating objects in each of the methods wastes memory and itís
    hard to keep the time synced up. But with the singleton pattern the locking
    might take long periods of time before itís done with the critical section.
    Thus slowing down access to the system time in for each method calling it.
    What is a programmer to do. ;)

    Thanks in advance.
    Ken

  2. #2
    ralph Guest

    Re: C++ design question


    "Ken F" <terryken@erols.com> wrote:
    >
    >Hi,
    >
    >When designing an application that is multiple threaded and programming

    with
    >C++, I was wondering is there an optimal approach? I have an object Time
    >that time stamps each record that passes through my server. Is it, better
    >to make the Time object a singleton pattern and put locks around the critical
    >sections? Or, create a Time object in each method that is going to do the
    >time stamps. Creating objects in each of the methods wastes memory and itís
    >hard to keep the time synced up. But with the singleton pattern the locking
    >might take long periods of time before itís done with the critical section.
    > Thus slowing down access to the system time in for each method calling

    it.
    > What is a programmer to do. ;)
    >
    >Thanks in advance.
    >Ken


    You have described the two basic methods and appear to have a good understanding
    of the pro's and con's of each method. This is a classic design issue. Timestamping
    stuff in a multi-threaded/multi-processor environment is a real pain as you
    have discovered.

    So do you really need timestamps?

    If these objects are going to end up in a database why not let the database
    take care of it? No matter how many critters are dumping objects in a repository
    - there is always one moment when one record alone is actually added. That
    is the moment to stamp it. Even with field sampling - depending on latency
    - the actual recording stamp is usually sufficient.

    Or perhaps you only need sequencing. In that case take a tip from packet
    frames - they often have a simple date, some form of 'context' flag, and
    just a sequence number. Sequence numbers are often all you need to keep things
    in order while processing, because eventually these items become packaged
    into something different and the reason for 'unique' tagging has gone away.

    If you are using timestamps as an attempt to "coordinate" or control program
    flow - then you are headed for trouble. <g> "Time" in a computer system can
    get very cloudy especially when the intervals get small. It alway seems like
    a good idea. You know, having someone beat precise beats on a drum while
    eveyone stays in rhythmic flow - unfortunately this only works with lots
    of chains, abuse, and control. More often than not it turns into a lone teacher
    with a whistle surrounded by a hundred screaming kids at recess. The best
    you can do is stop fights, try to get everyone to take turns, and march in
    and out in a single file.

    -ralph

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center