NPAPI threading model: should access to global variables be protected by a lock?
From https://developer.mozilla.org/En/Gecko_Plugin_API_Reference:Scripting_plugins :
This API is not designed to be thread safe. The threading model for this API is such that all calls through this API are synchronous and calls from a plugin to methods in this API must come from the thread on which the plugin was initiated, and likewise all calls to methods in this API by the browser are guaranteed to come from the same thread. Future revisions to this API might provide a mechanism for proxying calls from one thread to another to aid in using this API from other threads.
If I want to access a global variable in my plugin (shared between all instances, even on different pages), do I need to lock it or does the browser use only one thread to communicate with the plugin for all开发者_StackOverflow社区 instances?
The browser always uses exactly one thread to communicate with the plugin for all instances; you should do the same in return and never call any NPN_ functions from other than the main thread.
Keep in mind that if you're doing anything that may block the main thread at all you'll want to create your own threads and in that case you may need locking; however, just for the browser? no, you don't need them.
精彩评论