C # code standard. net2.0 (4) multi-threaded coding guidelines

Source: Internet
Author: User

1. Use synchronization domains. Avoid manual synchronization, because that often leads to deadlocks and race conditions.

2. Never call outside your synchronization domain.

3. Manage asynchronous call completion on a callback method. Do not wait, poll, or block for completion.

4. Always name your threads:

Thread currentthread = thread. currentthread;
String threadname = "Main UI thread ";
Currentthread. Name = threadname;

The name is traced in the debugger threads window, making debug sessions more productive.

5. Do not call suspend () or resume () on a thread.

6. Do not call thread. Sleep (), wait t in the following conditions:

A. thread. Sleep (0) is an acceptable optimization technique to force a context switch.

B. thread. Sleep () is acceptable in testing or simulation code.

7. Do not call thread. spinwait ().

8. Do not call thread. Abort () to terminate threads. Use a synchronization object instead to signal the thread to terminate.

9. Avoid explicitly setting the thread priority to control execution. You can set the thread priority based on task semantics (such as threadpriority. belownormal for a screensaver ).

10. Do not read the value of the threadstate property. Use thread. isalive () to determine whether the thread is dead or alive.

11. Do not rely on setting the thread type to background thread for application shutdown. Use a watchdog or other monitoring entity to deterministically kill threads.

12. Do not use the Thread Local Storage unless thread affinity is guaranteed.

13. Do not call thread. memorybarrier ().

14. Never call thread. Join () without checking that you are not joining your own thread:

Void waitforthreadtodie (thread)
{
Debug. Assert (thread. currentthread. managedthreadid! = Thread. managedthreadid );
Thread. Join ();
}

 

15. Always use the lock () statement rather than explicit monitor manipulation.

16. Always encapsulate the lock () statement inside the object it protects:

Public class myclass
{
Public void dosomething ()
{
Lock (this)
{...}
}
}

 

17. You can use synchronized methods instead of writing the lock () statement yourself.

18. Avoid fragmented locking.

19. Avoid using a monitor to wait or pulse objects. Use manual or auto-reset events instead.

20. do not use volatile variables. lock your object or fields instead to guarantee deterministic and thread-safe access. do not use thread. volatileread (), thread. volatilewrite (), or the volatile modifier.

21. avoid increasing the maximum number of threads in the thread pool.

22. Never stack lock () statements, because that does not provide atomic locking:

Myclass obj1 = new myclass ();
Myclass obj2 = new myclass ();
Myclass obj3 = new myclass ();

// Do not stack lock statements
Lock (obj1)
Lock (obj2)
Lock (obj3)
{
Obj1.dosomething ();
Obj2.dosomething ();
Obj3.dosomething ();
}

 

Use waithandle. waitall () instead.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.