TAILIEUCHUNG - thinking in c 2nd ed volume 2 rev 20 - phần 10

Một lần trong một thời gian tuyệt vời bạn có thể cần phải khởi hành từ sự khôn ngoan thông thường của thiết kế objectoriented âm thanh bằng cách kiểm tra kiểu thời gian chạy của một đối tượng để xử lý đặc biệt. | 469z516 class Blocked public Runnable public void run try Thread sleep 1000 cout Waiting for get in run catch Interrupted_Exception cout Caught Interrupted_Exception endl Exit the task int main int argc char argv try Thread t new Blocked if argc 1 Thread sleep 1100 catch Synchronization_Exception e cerr endl You can see that run contains two points where blocking can occur the call to Thread sleep 1000 and the call to . By giving the program any command-line argument you tell main to sleep long enough that the task will finish its sleep and move into the . If you don t give the program an argument the sleep in main is skipped. In this case the call to interrupt will occur while the task is sleeping and you ll see that this will cause Interrupted_Exception to be thrown. If you give the program a command-line argument you ll discover that a task cannot be interrupted if it is blocked on IO. That is you can interrupt out of any blocking operation except IO. This is a little disconcerting if you re creating a thread that performs IO because it means that I O has the potential of locking your multithreaded program. The problem is that again C was not designed with threading in mind quite the opposite it effectively pretends that threading doesn t exist. Thus the iostream library is not thread-friendly. If the new C standard decides to add thread support the iostream library may need to be reconsidered in the process. Blocked by a mutex In the previous list of ways to become blocked the last one happens when you re trying to call a function whose mutex has already been acquired. In this situation the calling task will be suspended until the mutex becomes available. The following example tests whether this kind of blocking is interruptible C11 Interrupting a thread blocked with a synchronization guard. L ZThread include zthread include zthread include zthread include iostream using .

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.