Comments to clarify default shared ImFontAtlas and current context pointer thread-safety (#586, #591)

This commit is contained in:
ocornut 2016-05-08 17:12:54 +02:00
parent 8a0d3b9628
commit aa11934efa
2 changed files with 7 additions and 6 deletions

View File

@ -696,15 +696,15 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y);
// Context // Context
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// We access everything through this pointer (always assumed to be != NULL) // Default context, default font atlas.
// You can swap the pointer to a different context by calling ImGui::SetCurrentContext() // New contexts always point by default to this font atlas. It can be changed by reassigning the GetIO().Fonts variable.
static ImGuiContext GImDefaultContext; static ImGuiContext GImDefaultContext;
ImGuiContext* GImGui = &GImDefaultContext;
// Statically allocated default font atlas. This is merely a maneuver to keep ImFontAtlas definition at the bottom of the .h file (otherwise it'd be inside ImGuiIO)
// Also we wouldn't be able to new() one at this point, before users have a chance to setup their allocator.
static ImFontAtlas GImDefaultFontAtlas; static ImFontAtlas GImDefaultFontAtlas;
// Current context pointer. Implicitely used by all ImGui functions. Always assumed to be != NULL. Change to a different context by calling ImGui::SetCurrentContext()
// ImGui is currently not thread-safe because of this variable. If you want thread-safety to allow N threads to access N different contexts, you might work around it by (A) having two instances of the ImGui code under different namespaces or (B) change this variable to be TLS. Further development aim to make this context pointer explicit to all calls. Also read https://github.com/ocornut/imgui/issues/586
ImGuiContext* GImGui = &GImDefaultContext;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// User facing structures // User facing structures
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -444,6 +444,7 @@ namespace ImGui
IMGUI_API void SetClipboardText(const char* text); IMGUI_API void SetClipboardText(const char* text);
// Internal context access - if you want to use multiple context, share context between modules (e.g. DLL). There is a default context created and active by default. // Internal context access - if you want to use multiple context, share context between modules (e.g. DLL). There is a default context created and active by default.
// All contexts share a same ImFontAtlas by default. If you want different font atlas, you can new() them and overwrite the GetIO().Fonts variable of an ImGui context.
IMGUI_API const char* GetVersion(); IMGUI_API const char* GetVersion();
IMGUI_API ImGuiContext* CreateContext(void* (*malloc_fn)(size_t) = NULL, void (*free_fn)(void*) = NULL); IMGUI_API ImGuiContext* CreateContext(void* (*malloc_fn)(size_t) = NULL, void (*free_fn)(void*) = NULL);
IMGUI_API void DestroyContext(ImGuiContext* ctx); IMGUI_API void DestroyContext(ImGuiContext* ctx);