324_Book

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: rectness. Programs must explicitly synchronize accesses to shared variables. Step 1 2 3 4 5 6 7 8 9 10 Thread 1 1 2 2 2 2 1 1 1 2 Instr À½ Ľ À¾ ľ ; ˾ ͽ ˽ ̽ ̾ %eax½ – 0 – – – – 1 1 1 1 %eax¾ – – – 0 1 1 – – – – ØÖ 0 0 0 0 0 1 1 1 1 1 Variable cnt has a final incorrect value of ½. Problem 11.5 Solution: [Pg. 599] If we free the block immediately after the call to pthread create in line 15, then we will introduce a new race, this time between the call to free in the main thread, and the assignment statement in line 25 of the thread routine. Problem 11.6 Solution: [Pg. 599] 736 APPENDIX B. SOLUTIONS TO PRACTICE PROBLEMS A. Another approach is to pass the integer i directly, rather than passing a pointer to i: for (i = 0; i < N; i++) Pthread_create(&tid[i], NULL, thread, (void *)i); In the thread routine, we cast the argument back to an int and assign it to myid: int myid = (int) vargp; B. The advantage is that it reduces overhead by eliminating the calls to malloc and free. A significan...
View Full Document

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online