100% cpu on thread suspend! bug?

  • Thread starter Thread starter Jesper Rasmussen
  • Start date Start date
J

Jesper Rasmussen

Hi,
Im sure this must be a FAQ, or a known issue, but i
haven't been able to find any information on it.

When I have one thread suspended and another one starts
running, the CPU jumps to 100% untill the thread is
resumed! this can't be appropriate behaviour?

See example code and output below!

Thanks in advance for any input
Jesper Rasmussen
(please cc email if possible)
------------------------------------------
Example:
------------------------------------------
class Class1
{
public static void thread1()
{
while(true)
{
Console.WriteLine("T1");
Thread.Sleep(1000);
}
}
public static void thread2()
{
while(true)
{
Console.WriteLine("
T2");
Thread.Sleep(1000);
}

}
private static Thread t1;
private static Thread t2;

static void Main(string[] args)
{

t1 = new Thread(new ThreadStart
(thread1));
t1.Start();
Thread.Sleep(3000);
Console.WriteLine("Suspending
T1");
t1.Suspend();

Thread.Sleep(2000);

t2 = new Thread(new ThreadStart
(thread2));
t2.Start();

Thread.Sleep(3000);
Console.WriteLine("Suspending
T2");
t2.Suspend();

t1.Resume();
Thread.Sleep(3000);
Console.WriteLine("Test Done");
t2.Resume();
}

}
-------------------------------------------
Output:
-------------------------------------------
T1 <5%cpu
T1 <5%cpu
T1 <5%cpu
Suspending T1 <5%cpu (for the 2 secs until T2 starts)
T2 100%cpu
T2 100%cpu
T2 100%cpu
T2 100%cpu
Suspending T2
100%cpu (still 100% when both is suspended)
T1 <5%cpu (T1 resumed)
T1 <5%cpu
T1 <5%cpu
Test Done
 
Yes I guess this is a known issue.
Instead of using threads, try using Timers.

HTH
Sunil TG
 
Yes I guess this is a known issue.
Instead of using threads, try using Timers.

HTH
Sunil TG
 
Thank you for you quick reply.

In the application i need to suspend a thread until a
user interaction occures, so I can't use a timer.

Could there possibly be other possibilities?

Thanks
Jesper Rasmussen
-----Original Message-----
Yes I guess this is a known issue.
Instead of using threads, try using Timers.

HTH
Sunil TG
-----Original Message-----
Hi,
Im sure this must be a FAQ, or a known issue, but i
haven't been able to find any information on it.

When I have one thread suspended and another one starts
running, the CPU jumps to 100% untill the thread is
resumed! this can't be appropriate behaviour?

See example code and output below!

Thanks in advance for any input
Jesper Rasmussen
(please cc email if possible)
------------------------------------------
Example:
------------------------------------------
class Class1
{
public static void thread1()
{
while(true)
{
Console.WriteLine("T1");
Thread.Sleep(1000);
}
}
public static void thread2()
{
while(true)
{
Console.WriteLine("
T2");
Thread.Sleep(1000);
}

}
private static Thread t1;
private static Thread t2;

static void Main(string[] args)
{

t1 = new Thread(new ThreadStart
(thread1));
t1.Start();
Thread.Sleep(3000);
Console.WriteLine("Suspending
T1");
t1.Suspend();

Thread.Sleep(2000);

t2 = new Thread(new ThreadStart
(thread2));
t2.Start();

Thread.Sleep(3000);
Console.WriteLine("Suspending
T2");
t2.Suspend();

t1.Resume();
Thread.Sleep(3000);
Console.WriteLine("Test Done");
t2.Resume();
}

}
-------------------------------------------
Output:
-------------------------------------------
T1 <5%cpu
T1 <5%cpu
T1 <5%cpu
Suspending T1 <5%cpu (for the 2 secs until T2 starts)
T2 100%cpu
T2 100%cpu
T2 100%cpu
T2 100%cpu
Suspending T2
100%cpu (still 100% when both is suspended)
T1 <5%cpu (T1 resumed)
T1 <5%cpu
T1 <5%cpu
Test Done

.
.
 
I tried copying the code, and run the project in the debugger - and it did
as you describe.
But when I run the program without debugging, the behavior of the program is
exactly as you would expect.

Just for the sake of discussion - I can't find any method that corresponds
to the old "WaitForSingleObject" API call. Have MS decided that that was
unneccesary?

Regards,
Peter Strøiman
 
You really shouldn't be using Suspend/resume for basic synchronization.
Block on an event or mutex. When the thread is blocked it consumes 0 cpu
cycles but is still in a state that it is runnable.

Jesper Rasmussen said:
Thank you for you quick reply.

In the application i need to suspend a thread until a
user interaction occures, so I can't use a timer.

Could there possibly be other possibilities?

Thanks
Jesper Rasmussen
-----Original Message-----
Yes I guess this is a known issue.
Instead of using threads, try using Timers.

HTH
Sunil TG
-----Original Message-----
Hi,
Im sure this must be a FAQ, or a known issue, but i
haven't been able to find any information on it.

When I have one thread suspended and another one starts
running, the CPU jumps to 100% untill the thread is
resumed! this can't be appropriate behaviour?

See example code and output below!

Thanks in advance for any input
Jesper Rasmussen
(please cc email if possible)
------------------------------------------
Example:
------------------------------------------
class Class1
{
public static void thread1()
{
while(true)
{
Console.WriteLine("T1");
Thread.Sleep(1000);
}
}
public static void thread2()
{
while(true)
{
Console.WriteLine("
T2");
Thread.Sleep(1000);
}

}
private static Thread t1;
private static Thread t2;

static void Main(string[] args)
{

t1 = new Thread(new ThreadStart
(thread1));
t1.Start();
Thread.Sleep(3000);
Console.WriteLine("Suspending
T1");
t1.Suspend();

Thread.Sleep(2000);

t2 = new Thread(new ThreadStart
(thread2));
t2.Start();

Thread.Sleep(3000);
Console.WriteLine("Suspending
T2");
t2.Suspend();

t1.Resume();
Thread.Sleep(3000);
Console.WriteLine("Test Done");
t2.Resume();
}

}
-------------------------------------------
Output:
-------------------------------------------
T1 <5%cpu
T1 <5%cpu
T1 <5%cpu
Suspending T1 <5%cpu (for the 2 secs until T2 starts)
T2 100%cpu
T2 100%cpu
T2 100%cpu
T2 100%cpu
Suspending T2
100%cpu (still 100% when both is suspended)
T1 <5%cpu (T1 resumed)
T1 <5%cpu
T1 <5%cpu
Test Done

.
.
 
Back
Top