Languages
[Edit]
EN

C#/.NET - thread yield method example

16 points
Created by:
Marcino
720

In this article, we would like to explain how Thread.Yield() method works on C# / .NET example.

When we write programs that use 100% of processor load it is necessary to yield processor execution between threads. The problem of load dispatching is solved by Thread.Yield() method.

 

Practical example

Edit

In this section, we have created many threads with infinity loops to get high processor load, dispatching load between threads by Thread.Yield() method calls.

Output:

 

Incorrect program example

Edit

This section uses a similar program to the above, but without Thread.Yield() method calls. That approach should never let dispatch load between different threads.

Output:

Summary:

Avoided Thread.Yield() method in last thread causes the thread probably will never get processor time to print the message.

 

References

Edit
  1. Thread.Yeld Method - Microsoft Docs
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

C# - Thread Class

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join