Starting Multitasking with out threads in C

Discussion in 'C Programming' started by raghu, Jan 19, 2007.

  1. raghu

    raghu Guest

    Hello Everyone

    I am working on C platform for a project in that project
    I need to maintain 'n' number of states where n is a dynamic value
    simultaneously. But I should not use any kind of threads since number
    of threads allowed, in the project, already reached and I can't predict
    how many threads to be maintained in this case.
    Can you please pull me out of this problem.

    Thanks in adv
    Bye
    with smile
    Raghu
     
    raghu, Jan 19, 2007
    #1
    1. Advertising

  2. In article <>,
    raghu <> wrote:

    > I am working on C platform for a project in that project
    >I need to maintain 'n' number of states where n is a dynamic value
    >simultaneously. But I should not use any kind of threads since number
    >of threads allowed, in the project, already reached and I can't predict
    >how many threads to be maintained in this case.
    > Can you please pull me out of this problem.


    That sounds like a question that goes beyond the scope of what the
    C Standard offers. We could tell you "malloc() a state struct
    when needed", but if your threads need to access that state, you
    will run into locking issues -- and locking and threads are not
    built into C.

    Also, you said that you "should not use any kind of threads", and
    there are those who would say that maintaining a state structure
    is -a- kind of threading, so your problem description would appear
    to rule out any possibility of a solution.
    --
    "law -- it's a commodity"
    -- Andrew Ryan (The Globe and Mail, 2005/11/26)
     
    Walter Roberson, Jan 19, 2007
    #2
    1. Advertising

  3. raghu

    jacob navia Guest

    raghu wrote:
    > Hello Everyone
    >
    > I am working on C platform for a project in that project
    > I need to maintain 'n' number of states where n is a dynamic value
    > simultaneously. But I should not use any kind of threads since number
    > of threads allowed, in the project, already reached and I can't predict
    > how many threads to be maintained in this case.
    > Can you please pull me out of this problem.
    >
    > Thanks in adv
    > Bye
    > with smile
    > Raghu
    >


    You can switch from context to context within one thread.

    Suppose your program stores its state in a structure.
    Switching states would mean just switching from one structure
    to the next one, in a round robin fashion...

    You make a circular list of contexts, and you loop around
    handling each of them.
     
    jacob navia, Jan 19, 2007
    #3
  4. On Fri, 19 Jan 2007 21:18:50 +0100, jacob navia wrote:
    >You can switch from context to context within one thread.
    >Suppose your program stores its state in a structure.
    >Switching states would mean just switching from one structure
    >to the next one, in a round robin fashion...
    >You make a circular list of contexts, and you loop around
    >handling each of them.


    This may contain some ideas:
    http://www.stanford.edu/class/cs240/readings/threads-bad-usenix96.pdf
     
    Roland Pibinger, Jan 19, 2007
    #4
  5. raghu

    jacob navia Guest

    Roland Pibinger wrote:
    > On Fri, 19 Jan 2007 21:18:50 +0100, jacob navia wrote:
    >
    >>You can switch from context to context within one thread.
    >>Suppose your program stores its state in a structure.
    >>Switching states would mean just switching from one structure
    >>to the next one, in a round robin fashion...
    >>You make a circular list of contexts, and you loop around
    >>handling each of them.

    >
    >
    > This may contain some ideas:
    > http://www.stanford.edu/class/cs240/readings/threads-bad-usenix96.pdf


    This is a very good article. Thanks for this pointer.
     
    jacob navia, Jan 19, 2007
    #5
  6. raghu

    santosh Guest

    raghu wrote:
    > Hello Everyone
    >
    > I am working on C platform for a project in that project
    > I need to maintain 'n' number of states where n is a dynamic value
    > simultaneously. But I should not use any kind of threads since number
    > of threads allowed, in the project, already reached and I can't predict
    > how many threads to be maintained in this case.
    > Can you please pull me out of this problem.


    Look up thunks and coroutines.
     
    santosh, Jan 20, 2007
    #6
  7. jacob navia wrote:
    > raghu wrote:
    > > Hello Everyone
    > >
    > > I am working on C platform for a project in that project
    > > I need to maintain 'n' number of states where n is a dynamic value
    > > simultaneously. But I should not use any kind of threads since number
    > > of threads allowed, in the project, already reached and I can't predict
    > > how many threads to be maintained in this case.
    > > Can you please pull me out of this problem.
    > >
    > > Thanks in adv
    > > Bye
    > > with smile
    > > Raghu
    > >

    >
    > You can switch from context to context within one thread.
    >
    > Suppose your program stores its state in a structure.
    > Switching states would mean just switching from one structure
    > to the next one, in a round robin fashion...
    >
    > You make a circular list of contexts, and you loop around
    > handling each of them.


    hello jacob navia,
    I am really thankful for ur suggestion. But will it be successful
    by using context switching? I have never worked with that. Would you
    mind gimme a brief idea about that? A simple C program example may
    work. So please help me on this with an example. Thanks a lot for your
    support.

    Ajit
     
    Ajit-the Gick, Jan 20, 2007
    #7
  8. raghu

    jacob navia Guest

    Ajit-the Gick a écrit :
    > jacob navia wrote:
    >
    >>raghu wrote:
    >>
    >>>Hello Everyone
    >>>
    >>> I am working on C platform for a project in that project
    >>>I need to maintain 'n' number of states where n is a dynamic value
    >>>simultaneously. But I should not use any kind of threads since number
    >>>of threads allowed, in the project, already reached and I can't predict
    >>>how many threads to be maintained in this case.
    >>> Can you please pull me out of this problem.
    >>>
    >>>Thanks in adv
    >>>Bye
    >>>with smile
    >>>Raghu
    >>>

    >>
    >>You can switch from context to context within one thread.
    >>
    >>Suppose your program stores its state in a structure.
    >>Switching states would mean just switching from one structure
    >>to the next one, in a round robin fashion...
    >>
    >>You make a circular list of contexts, and you loop around
    >>handling each of them.

    >
    >
    > hello jacob navia,
    > I am really thankful for ur suggestion. But will it be successful
    > by using context switching? I have never worked with that. Would you
    > mind gimme a brief idea about that? A simple C program example may
    > work. So please help me on this with an example. Thanks a lot for your
    > support.
    >
    > Ajit
    >


    You do yourself the context switch. Suppose you put all your
    context (global variables/data/etc). You make *some* processing
    of each context structure, and you stop at a certain point. You
    have to decide when to change. For instance you would have a counter
    each time you pass through an event loop or something similar. When this
    counter arrives at some N, you save the context in the list of contexts
    and start processing the next one.

    Or you could just check the time often, and stop after a certain delta
    (1 second for instance)

    jacob
     
    jacob navia, Jan 20, 2007
    #8
  9. raghu

    Randy Howard Guest

    On Fri, 19 Jan 2007 17:33:45 -0600, jacob navia wrote
    (in article <45b15559$0$25906$>):

    > Roland Pibinger wrote:
    >> On Fri, 19 Jan 2007 21:18:50 +0100, jacob navia wrote:
    >>
    >>> You can switch from context to context within one thread.
    >>> Suppose your program stores its state in a structure.
    >>> Switching states would mean just switching from one structure
    >>> to the next one, in a round robin fashion...
    >>> You make a circular list of contexts, and you loop around
    >>> handling each of them.

    >>
    >>
    >> This may contain some ideas:
    >> http://www.stanford.edu/class/cs240/readings/threads-bad-usenix96.pdf

    >
    > This is a very good article.


    It's over 10 years old, and not particularly accurate now. For one
    thing, it was written at a time when almost nobody had access to SMP
    hardware, when threading was wildly different across platforms (you can
    code to Posix pthreads almost everywhere now, even on Windows), and
    nobody had been taught anything about threaded programming.
    Programmers almost universally thought that it 2 threads were good, 200
    were even better. The so called "thundering herd problem."

    The current hardware direction is adding CPUs, not clock speed because
    they haven't figured out how to ignore the laws of physics yet. As
    such, threaded programming is becoming more and more important.

    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
     
    Randy Howard, Jan 20, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kak3012

    multitasking

    kak3012, Feb 28, 2005, in forum: C++
    Replies:
    5
    Views:
    504
    Thomas Matthews
    Mar 2, 2005
  2. Dan Henry

    Re: Firmware Multitasking for dsPIC30F4011

    Dan Henry, Jul 26, 2005, in forum: C Programming
    Replies:
    0
    Views:
    406
    Dan Henry
    Jul 26, 2005
  3. Replies:
    10
    Views:
    241
    Michael Granger
    Jun 22, 2005
  4. Greg Willits

    Multitasking and collecting results

    Greg Willits, Oct 7, 2007, in forum: Ruby
    Replies:
    3
    Views:
    130
    Clifford Heath
    Oct 7, 2007
  5. Replies:
    2
    Views:
    143
Loading...

Share This Page