《并行程序设计导论》 第四课.pptVIP

  • 3
  • 0
  • 约2.1万字
  • 约 105页
  • 2018-08-30 发布于湖北
  • 举报
What happened? Thus, thread 0’s call to strtok with the third line of the input has apparently overwritten the contents of thread 1’s call with the second line. So the strtok function is not thread-safe. If multiple threads call it simultaneously, the output may not be correct. Copyright ? 2010, Elsevier Inc. All rights Reserved Other unsafe C library functions Regrettably, it’s not uncommon for C library functions to fail to be thread-safe. The random number generator random in stdlib.h. The time conversion function localtime in time.h. Copyright ? 2010, Elsevier Inc. All rights Reserved “re-entrant” (thread safe) functions In some cases, the C standard specifies an alternate, thread-safe, version of a function. Copyright ? 2010, Elsevier Inc. All rights Reserved Concluding Remarks (1) A thread in shared-memory programming is analogous to a process in distributed memory programming. However, a thread is often lighter-weight than a full-fledged process. In Pthreads programs, all the threads have access to global variables, while local variables usually are private to the thread running the function. Copyright ? 2010, Elsevier Inc. All rights Reserved Concluding Remarks (2) When indeterminacy results from multiple threads attempting to access a shared resource such as a shared variable or a shared file, at least one of the accesses is an update, and the accesses can result in an error, we have a race condition. Copyright ? 2010, Elsevier Inc. All rights Reserved Concluding Remarks (3) A critical section is a block of code that updates a shared resource that can only be updated by one thread at a time. So the execution of code in a critical section should, effectively, be executed as serial code. Copyright ? 2010, Elsevier Inc. All rights Reserved Concluding Remarks (4) Busy-waiting can be used to avoid conflicting access to critical sections with a flag variable and a while-loop with an empty body. It can be very wasteful of CPU cycles. It can also be unreliabl

文档评论(0)

1亿VIP精品文档

相关文档