better solution for this problem. - Unix

This is a discussion on better solution for this problem. - Unix ; I have two variables x and y; x is set to some constant value. and y will be incremented every time based on some event.Once the value of y equals x some operation will be done. we can implement the ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: better solution for this problem.

  1. better solution for this problem.

    I have two variables x and y; x is set to some constant value.
    and y will be incremented every time based on some event.Once the
    value of y equals
    x some operation will be done. we can implement the above scenario in
    two different ways.

    1. every time y is incremented check if it equals x and do_some_stuff
    ();

    2. start a timer and wait for some reasonable time suppose 1 sec and
    check if y equals x
    and do_some_stuff (); the idea here is y will be incemented while
    the timer is running and you can avoid
    checking if y equals x for each and every time y is incremented.

    Of the above two possible solutions, which one is the best and why?
    i will implement the logic in C under Linux, in case if these things
    matter.

    thanks
    sinbad


  2. Re: better solution for this problem.

    In <58c95632-e6f5-4bc5-b163-e2f3226e69b5@a3g2000prm.googlegroups.com> sinbad writes:

    > Of the above two possible solutions, which one is the best and why?


    It depends entirely on factors that you haven't told us. For example:

    Is the application intended to be real-time?

    Is it acceptable for Y to be incremented several times before your program
    notices? Or must your program know each and every time Y is incremented?

    What is the "event" that increments Y? Is it an external real-world event,
    like counting raindrops that fall on a sensor?

    --
    John Gordon A is for Amy, who fell down the stairs
    gordon@panix.com B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"


  3. Re: better solution for this problem.

    On Aug 6, 11:39*pm, John Gordon wrote:
    > In <58c95632-e6f5-4bc5-b163-e2f3226e6...@a3g2000prm.googlegroups.com> sinbad writes:
    >
    > > Of the above two possible solutions, which one is the best and why?

    >
    > It depends entirely on factors that you haven't told us. *For example:
    >
    > Is the application intended to be real-time?
    >
    > Is it acceptable for Y to be incremented several times before your program
    > notices? *Or must your program know each and every time Y is incremented?
    >
    > What is the "event" that increments Y? *Is it an external real-world event,
    > like counting raindrops that fall on a sensor?
    >
    > --
    > John Gordon * * * * * * * * * A is for Amy, who fell down the stairs
    > gor...@panix.com * * * * * * *B is for Basil, assaulted by bears
    > * * * * * * * * * * * * * * * * -- EdwardGorey, "The Gashlycrumb Tinies"


    The application is infact real time.
    It is acceptable for Y to be incremented serveral times before my
    program notices;
    My program need not to know every time Y is incremented. It needs to
    know only when Y equals X.
    and Y will be incremented basing on some external world event like
    other process writing some message on to the socket.


  4. Re: better solution for this problem.

    In sinbad writes:

    > The application is infact real time.
    > It is acceptable for Y to be incremented serveral times before my
    > program notices;
    > My program need not to know every time Y is incremented. It needs to
    > know only when Y equals X.


    But what happens if Y becomes equal to X, and then gets incremented *again*
    before your timer fires?

    Sounds liker you might not be able to use a timer.

    --
    John Gordon A is for Amy, who fell down the stairs
    gordon@panix.com B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"


  5. Re: better solution for this problem.

    On Aug 7, 9:51*am, John Gordon wrote:
    > In sinbad writes:
    >
    > > The application is infact real time.
    > > It is acceptable for Y to be incremented serveral times before my
    > > program notices;
    > > My program need not to know every time Y is incremented. It needs to
    > > know only when Y equals X.

    >
    > But what happens if Y becomes equal to X, and then gets incremented *again*
    > before your timer fires?
    >
    > Sounds liker you might not be able to use a timer.
    >
    > --
    > John Gordon * * * * * * * * * A is for Amy, who fell down the stairs
    > gor...@panix.com * * * * * * *B is for Basil, assaulted by bears
    > * * * * * * * * * * * * * * * * -- EdwardGorey, "The Gashlycrumb Tinies"



    Y Can never go beyond X, so once the timer fires and if Y has actually
    been incremented till X, do_some_stuff() will be called

  6. Re: better solution for this problem.

    Simbad, the first approach for your implementation will work fine but
    for not very complex operations. You can imagine y being an async
    (overlapped) operation. For a more complex design you could assign y
    an event and a value and put it in an event queue. Implement a
    function that waits for any event in the queue and have that value
    associated. This will be the job of one thread. The others can work
    and according to events do custom stuff too.

+ Reply to Thread