mikeash.com pyblog/friday-qa-2009-01-09.html commentshttp://www.mikeash.com/?page=pyblog/friday-qa-2009-01-09.html#commentsmikeash.com Recent CommentsThu, 28 Mar 2024 11:46:47 GMTPyRSS2Gen-1.0.0http://blogs.law.harvard.edu/tech/rssGeorge Warner - 2009-03-11 06:53:28http://www.mikeash.com/?page=pyblog/friday-qa-2009-01-09.html#commentsJoshua Bloch's excellent book, Effective Java Programming Language Guide defines Degrees of Thread Safety as: <br />&nbsp;&nbsp;&nbsp;&nbsp;Thread-safe <br />&nbsp;&nbsp;&nbsp;&nbsp;Immutable <br />&nbsp;&nbsp;&nbsp;&nbsp;Thread-aware <br />&nbsp;&nbsp;&nbsp;&nbsp;Conditionally thread-safe <br />&nbsp;&nbsp;&nbsp;&nbsp;Thread-compatible (friendly) <br />&nbsp;&nbsp;&nbsp;&nbsp;Thread-hostile (non-reentrant) <br />2cbb8a4641dfa013774673ee177f225cWed, 11 Mar 2009 06:53:28 GMTGeorge Warner - 2009-03-11 06:51:16http://www.mikeash.com/?page=pyblog/friday-qa-2009-01-09.html#commentsIt's important to realize that what's meant by "thread-safe" is really talking about how the routine accesses data. <br /> <br />Thread-safe data includes: <br /> <br />&nbsp;&nbsp;&nbsp;&nbsp;Immutable variables (any scope) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Read-only data should be declared const) <br />&nbsp;&nbsp;&nbsp;&nbsp;Non-shared data <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local variables <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;method parameters <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return values <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;locally allocated memory <br /> <br />Thread-safe code... <br />...must have exclusive access to any data it modifies <br />...may modify non-shared data <br />...may simultaneously access distinct data <br />...may require synchronization <br />...has no race conditions <br />...does not deadlock <br />...has no priority failures <br />...has no starvation failures <br />ef923e8c8e5b9665d13929d76e5da7a3Wed, 11 Mar 2009 06:51:16 GMTGeorge Warner - 2009-03-11 06:45:01http://www.mikeash.com/?page=pyblog/friday-qa-2009-01-09.html#commentsI would call the OpenGL API's calls single-thread-safe meaning that all OpenGL calls are thread-safe from the thread that that OpenGL context was created on. Plus there are (safe) ways to migrate OpenGL contexts between threads. But once they're there they are again single-thread-safe. <br /> <br />a0f93b0c0f199616ae587fe1473a446fWed, 11 Mar 2009 06:45:01 GMTAndre Pang - 2009-02-07 06:01:02http://www.mikeash.com/?page=pyblog/friday-qa-2009-01-09.html#commentsNote to graphics folks: OpenGL API calls (at least in all the incarnations that I've ever used it) can be used on either the main thread or the background thread, but once you set up an OpenGL context on that thread, you must call all future OpenGL functions which apply to that context from that thread. Under Mike's definition above, this is "not thread safe" (which the computer science guy in me certainly agrees with). <br /> <br />Google for "OpenGL threading" for more info. <br /> <br />4295f6204f6dccba2213ed04f211d7e9Sat, 07 Feb 2009 06:01:02 GMT