Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

C# threading problem.

Started by Holy Fuzz Jun 26, 2004 at 9:16 AM 4 replies 2.7k views
Original Post
Holy Fuzz
Holy Fuzz
I'm having a problem with getting multithreading to work in C#. Specifically, my calls to Thread.Sleep() don't yield their time to the other threads. This is true even when I copy code verbatim out of help. Here's a piece of code copied directly from the help docs:

using System;
using System.Threading;

// Simple threading scenario:  Start a static method running
// on a second thread.
public class ThreadExample {
    // The ThreadProc method is called when the thread starts.
    // It loops ten times, writing to the console and yielding 
    // the rest of its time slice each time, and then ends.
    public static void ThreadProc() {
        for (int i = 0; i < 10; i++) {
            Console.WriteLine("ThreadProc: {0}", i);
            // Yield the rest of the time slice.
            Thread.Sleep(0);
        }
    }

    public static void Main() {
        Console.WriteLine("Main thread: Start a second thread.");
        // The constructor for the Thread class requires a ThreadStart 
        // delegate that represents the method to be executed on the 
        // thread.  C# simplifies the creation of this delegate.
        Thread t = new Thread(new ThreadStart(ThreadProc));
        // Start ThreadProc.  On a uniprocessor, the thread does not get 
        // any processor time until the main thread yields.  Uncomment 
        // the Thread.Sleep that follows t.Start() to see the difference.
        t.Start();
        //Thread.Sleep(0);

        for (int i = 0; i < 4; i++) {
            Console.WriteLine("Main thread: Do some work.");
            Thread.Sleep(0);
        }

        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
        t.Join();
        Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
        Console.ReadLine();
    }
}
Here's what the output is SUPPOSED to look like, according to the help docs:
Quote:
Main thread: Start a second thread. Main thread: Do some work. ThreadProc: 0 Main thread: Do some work. ThreadProc: 1 Main thread: Do some work. ThreadProc: 2 Main thread: Do some work. ThreadProc: 3 Main thread: Call Join(), to wait until ThreadProc ends. ThreadProc: 4 ThreadProc: 5 ThreadProc: 6 ThreadProc: 7 ThreadProc: 8 ThreadProc: 9 Main thread: ThreadProc.Join has returned. Press Enter to end program.
Here's the output I actually get when I run my program:
Quote:
Main thread: Start a second thread. Main thread: Do some work. Main thread: Do some work. Main thread: Do some work. Main thread: Do some work. Main thread: Call Join(), to wait until ThreadProc ends. ThreadProc: 0 ThreadProc: 1 ThreadProc: 2 ThreadProc: 3 ThreadProc: 4 ThreadProc: 5 ThreadProc: 6 ThreadProc: 7 ThreadProc: 8 ThreadProc: 9 Main thread: ThreadProc.Join has returned. Press Enter to end program.
As you can see, any calls to Thread.Sleep() do NOT yield control to the other thread. But it's supposed to. Anyone have any ideas why this is happening? Thanks.
Epolevne
Epolevne
That's very odd, because it works as the help docs say it should when I copy that code into a new console app.

I'm using:
Visual Studio 2003
.NET Framework 1.1
Windows XP 32-bit
Athlon64

I can't think of a simple reason off the top of my head why your app wouldn't work.



Epolevne
TangentZ
TangentZ
To the OP, are you running on a multi-CPU machine or you have
HyperThreading enabled? Just a thought.
神はサイコロを振らない!
Raghar
Raghar
Thread.sleep(0); ?
Is this C# equivalent of Java's Thread.yield() ?
Thread.sleep(30) is MUCH better.
C-Junkie
C-Junkie
Synchronization of threads can only be done with locks (mutexs, semaphores, etc)
Epolevne
Epolevne
I don't think he's trying to sync the threads, just trying to get them to interupt one another.

Holy Fuzz-
What happens if you use Sleep(30) instead of Sleep(0)?

Of course in a real-world situation Sleep(30) would limit your loop to 33 iterations/second...

Epolevne

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.