translation thread safety

This commit is contained in:
Akash Mozumdar 2019-01-23 13:41:50 -05:00
parent af8a069243
commit ac6294f30e
2 changed files with 5 additions and 3 deletions

View File

@ -85,7 +85,7 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
// This function detects language and puts it in translateFrom if it's empty // This function detects language and puts it in translateFrom if it's empty
std::wstring Translate(std::wstring text, std::wstring& translateFrom, std::wstring translateTo) std::wstring Translate(std::wstring text, std::wstring& translateFrom, std::wstring translateTo)
{ {
static HINTERNET internet = NULL; static std::atomic<HINTERNET> internet = NULL;
if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0); if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
char utf8[10000] = {}; char utf8[10000] = {};
@ -156,6 +156,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
translation[i] = 0x200b; translation[i] = 0x200b;
if (translation[i + 1] == L'r') translation[i + 1] = L'\r'; if (translation[i + 1] == L'r') translation[i + 1] = L'\r';
if (translation[i + 1] == L'n') translation[i + 1] = L'\n'; if (translation[i + 1] == L'n') translation[i + 1] = L'\n';
if (translation[i + 1] == L't') translation[i + 1] = L'\t';
} }
} }

View File

@ -149,9 +149,9 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
} }
} }
static HINTERNET internet = NULL; static std::atomic<HINTERNET> internet = NULL;
if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0); if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
static unsigned TKK = 0; static std::atomic<unsigned> TKK = 0;
std::wstring translation; std::wstring translation;
if (internet) if (internet)
{ {
@ -202,6 +202,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
translation[i] = 0x200b; translation[i] = 0x200b;
if (translation[i + 1] == L'r') translation[i + 1] = L'\r'; if (translation[i + 1] == L'r') translation[i + 1] = L'\r';
if (translation[i + 1] == L'n') translation[i + 1] = L'\n'; if (translation[i + 1] == L'n') translation[i + 1] = L'\n';
if (translation[i + 1] == L't') translation[i + 1] = L'\t';
} }
} }
} }