Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi every one
I have understood the benefits of using threads and multithreading programming but I want to know, can we write a program without thread?
Posted
Comments
Sergey Alexandrovich Kryukov 21-Aug-13 0:17am    
Not a correct question: it depends on what you are writing. And why? Do you think having no threads would make you life easier?

It also depends on what do you mean "without threads". You could say "single-threaded". There cannot be zero threads. :-)

—SA

Please see my comments to the question and Solution 2.

Now, the question about "pros and cons" is itself incorrect. First, you always have at least one threads. Adding more threads in many cases is not your choice, it is indirectly defined by the requirements.

For example, imagine that you got some requirements for numerical simulation: you need to calculate trajectories of some moving bodies (more exactly, worlds lines). If the problem is solvable at all, you always can do it one thread, and it's likely the best option. Now, imagine you need to output some intermediate locations of bodies as motion progresses and give the user the power to stop motion or change some parameters on the fly. Then you will certatainly need at least one more thread. And apparently you would need at least two threads if you have to visualize the motion: one for the UI, second one for the scenario of the motion.

Are you getting the idea? It depends on scenario and requirements. Requirements should not directly dictate you to use threads, but it can be either unavoidable or highly practical (or, rather, having one thread would be extremely impractical).

And some phenomena of real life to be modeled by your application can be multithreaded by their nature. Imaging you are modeling behavior of two or more animals, each one with its own goals and behavioral program. Such independence in behavior will likely logically require a separate thread per player (animal, in this case).

No, this is not about "pros and cons". This is about what and how. :-)

—SA
 
Share this answer
 
v3
Technically no, everything runs on threads. The .NET framework takes care of threading, even just consuming events or using delegates creates uses threads. (Edit: To expand on that properly, delegates and events are typesafe function pointers in C# that operate on other threads. Events are a special type of delegate, and are sometimes invoked on threads other than the thread the owner of the control or object was created on.)

Can you program without explicitly creating threads (without using the Thread class, or BackgroundWorker, or ThreadPool)? Yeah, sure, but there are still threads running behind the scenes. (Edit: Threads meaning applications and other work associated with the processor, of which the task scheduler is based on threads. Your application runs on one (or more) of these threads that the processor is cycling through, so you have at least one thread in your program at all times.)
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 21-Aug-13 0:20am    
I voted 4. You see, consuming events and using delegates itself does not create threads. I think you should understand that, so don't confuse OP.

By the way, such thing as file dialog always creates a thread, by apparent reasons. But in most other cases, if you are not creating a thread or taking one from a thread pool, the program will be strictly single-threaded. You should rather point out that "without thread" is incorrect expression. Please see my comment to the question.

—SA
Sergey Alexandrovich Kryukov 21-Aug-13 0:34am    
Probably I found more essential path to explain things. Please see my answer.
—SA
Ron Beyer 21-Aug-13 0:36am    
Apologies, late evening and refreshing beverages make me use the wrong words in some places, updated!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900