Starting Multitasking with out threads in C

R

raghu

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
 
W

Walter Roberson

raghu said:
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.
 
J

jacob navia

raghu said:
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.
 
S

santosh

raghu said:
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.
 
A

Ajit-the Gick

jacob said:
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
 
J

jacob navia

Ajit-the Gick a écrit :
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
 
R

Randy Howard

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top