TAILIEUCHUNG - Apress - Accelerated C#_6

Tham khảo tài liệu 'apress - accelerated c#_6', công nghệ thông tin, cơ sở dữ liệu phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | CHAPTER 12 THREADING IN C private static StreamWriter fsLog new StreamWriter private static void RndThreadFunc using new MySpinLockManager logLock Thread Starting int time 10 200 time using new MySpinLockManager logLock Thread Exiting static void Main Start the threads that wait random time. Thread rndthreads new Thread 50 for uint i 0 i 50 i rndthreads i new Thread new ThreadStart rndthreads i .Start This example is similar to the previous one. It creates 50 threads that wait a random amount of time. However instead of managing a thread count it outputs a line to a log file. This writing is happening from multiple threads and instance methods of StreamWriter are not thread-safe therefore you must do the writing in a safe manner within the context of a lock. That is where the MySpinLock class comes in. Internally it manages a lock variable in the form of an integer and it uses to gate access to the lock. The call to in is saying 1. If the lock value is equal to 0 replace the value with 1 to indicate that the lock is taken otherwise do nothing. 2. If the value of the slot already contains 1 it s taken and you must sleep and spin. Both of those items occur in an atomic fashion via the Interlocked class so there is no possible way that more than one thread at a time can acquire the lock. When the method is called all it needs to do is reset the lock. However that must be done atomically as well hence the call to . 384 CHAPTER 12 THREADING IN C Note Because the internal lock is represented by an int which is an Int32 one could simply set the value to zero in . However as mentioned in the previous sidebar you must be careful if the lock were a 64-bit value and you are running on a .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.