views:

519

answers:

5

I'm relatively new to coding; most of my "work" has been just simple GUI apps that only function for one thing, so I haven't had to thread much.

Anyway, one thing I'm wondering about threading is if you want to keep a thread alive forever to do whatever job it's doing (processing, waiting for input, whatever), is it normal to format it like so:

while (true) {
    // do stuff
    Thread.Sleep(1000);
}

(or something along those lines)...? Or is this not safe and should it be avoided if possible?

+14  A: 

Yep, that's what you do.

But typically it's like:

bool keepRunning = true;

...

while(keepRunning){
}

Because sometimes you may like to have someone/something else to have the ability to stop you.

Noon Silk
So it is common practice? Okay. What are the problems with say, Thread.Abort() if you wanted to close that? Just curious.
Corey
Thread.Abort is fine but it'll cause an exception, and it may interrupt something that you care about finishing. With the 'keepRunning' variable it lets you finish whatever you were doing gracefully (though it won't result in an *immediate* finish).
Noon Silk
Abort has its problems since it can cause data structures to remain unstable, and since the thread is releasing them other threads could become unstable too. (supposed they were locked while being handled, therefore unstable when released). That's why the method was made deprecated in Java, at least.
Aviad Ben Dov
keepRunning would usually be declared as volatile so a different thread can set it to false to stop the current thread.
Filmund
Thread.Abort is _not_ fine. Don't ever do that.
Henk Holterman
@Henk in what language? java? Yes, agreed but in c#: I see nothing wrong with it.
Noon Silk
Several discussions on SO, here is just one http://stackoverflow.com/questions/710070/timeout-pattern-how-bad-is-thread-abort-really
Henk Holterman
Also don't forgot to make keepRunning volatile! (volatile bool keepRunning = true;)
Cris McLaughlin
+1  A: 

Additionally You can use System.Threading.Timer. In this case, we don't have to use the Sleep method. Simple example:

public sealed class TimerTask
{
    private Timer _timer;
    private int _period;

    public TimerTask(int period)
    {
        _period = period;
        _timer = new Timer(new TimerCallback(Run), "Hello ....", Timeout.Infinite, period);
    }

    public void Start()
    {
        _timer.Change(0, _period);
    }

    public void Stop()
    {
        _timer.Change(Timeout.Infinite, Timeout.Infinite);
    }

    private void Run(Object param)
    {
        Console.WriteLine(param.ToString());
    }
}

Use:

public static class Program
{
    [STAThread]
    static void Main(String[] args)
    {
        TimerTask task = new TimerTask(1000);
        Console.WriteLine("Timer start.");
        task.Start();
        Console.ReadLine();
        Console.WriteLine("Timer stop.");
        task.Stop();
        Console.ReadLine();
        Console.WriteLine("Timer start.");
        task.Start();
        Console.ReadLine();
        Console.WriteLine("Timer stop.");
        task.Stop();
        Console.ReadLine();
    }
}

Console output:

Timer start.
Hello ....
Hello ....
Hello ....

Timer stop.

Timer start.
Hello ....
Hello ....

Timer stop.
mykhaylo
A: 

Just as some additional info, typical none ending loops use

for(;;)
{
 ...
}

as there is no compare done in the loop. When doing threads it is best to check a flag if the loop to end or not though.

shimpossible
Works, but it is an awful C-ism that is unnecessarily cryptic.
Henk Holterman
A: 

Ideally you want the thread to be "runnable" when it has work to do, and "sleeping" when there is nothing to do.

This is best done with objects like mutual exclusions (mutexes), semaphores and condition variables, which provide mechanisms for threads to wake other threads up when there may be something for them to do.

Just doing a timed sleep is inefficient, because a short sleep means the thread wastes time waking up to check if there's work to do, while a long sleep means the thread might be asleep while there's work to be done. Usually this is not a big deal but if the code deals with large volumes of requests or data things don't go so well.

A basic model works like this: Thread A puts objects in a queue. Thread B removes an object from the queue, performs an action, and repeats. If there are no objects in the queue, thread B will remain asleep until an object arrives.

You must also be careful that threads which access shared stuff avoid race conditions.

I can't give any C#-specific insight, but I know that C# gives you some tools to help you out.

Artelius
A: 

To elaborate a bit more, if a thread is sleeping, when the OS comes along to activate the thread, it will just check to see if it's still sleeping and if so, then just yield its timeslice.

If you leave out the Sleep and do something like

while (true)
{
    if (workAvailable)
        {
        doWork();       
        }
}

then even if workAvailable is false it will keep spinning until the OS stops it, taking up its entire slice doing nothing. Obviously that's a little more inefficient.

You can get even more complex as needed with mutexes, semaphores and whatnot, as mentioned above, but things get complex quickly with those, so you might want to use them to solve a particular problem.

Jason