Switch to unique_ptr for thread pointers in TriviaGame

This commit is contained in:
Jack Bond-Preston 2016-07-12 20:47:18 +01:00
parent 4bcedace38
commit 273f732d8c
4 changed files with 15 additions and 51 deletions

View File

@ -108,14 +108,14 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;CURL_STATICLIB;WIN32_WINNT=0x0600;URDL_NO_LIB=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>C:\boost_1_61_0;C:\OpenSSL-Win64\include;C:\Users\Jack\Documents\GitHubVisualStudio\TriviaDiscord\lib\cpr\include;C:\Users\Jack\Documents\GitHubVisualStudio\TriviaDiscord\lib\websocketpp;C:\Users\Jack\Documents\GitHubVisualStudio\TriviaDiscord\lib\sqlite3;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>C:\Users\Jack\Documents\GitHubVisualStudio\TriviaDiscord\lib\libcurl\include;C:\Users\Jack\Documents\GitHubVisualStudio\TriviaDiscord\lib\boost_1_61_0;C:\Users\Jack\Documents\GitHubVisualStudio\TriviaDiscord\lib\openssl\include;C:\Users\Jack\Documents\GitHubVisualStudio\TriviaDiscord\lib\websocketpp;C:\Users\Jack\Documents\GitHubVisualStudio\TriviaDiscord\lib\sqlite3;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>-D_SCL_SECURE_NO_WARNINGS -DUSE_SYSTEM_CURL=OFF -D_WIN32_WINNT=0x0A00 %(AdditionalOptions)</AdditionalOptions>
<ObjectFileName>$(IntDir)/%(RelativeDir)/</ObjectFileName>
</ClCompile>
<Link>
<AdditionalDependencies>libcurl.lib;sqlite3.lib;libeay32.lib;ssleay32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>libcurl.lib;libeay32.lib;ssleay32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalOptions>/SUBSYSTEM:CONSOLE %(AdditionalOptions)</AdditionalOptions>
<AdditionalLibraryDirectories>C:\buildcurl\third-party\libcurl\lib\dll-release-x64;C:\OpenSSL-Win64\lib;C:\boost_1_61_0\lib64-msvc-14.0;C:\Users\Jack\Documents\GitHubVisualStudio\TriviaDiscord\lib\sqlite3\x64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>C:\Users\Jack\Documents\GitHubVisualStudio\TriviaDiscord\lib\libcurl\lib\dll-release-x64;C:\Users\Jack\Documents\GitHubVisualStudio\TriviaDiscord\lib\openssl\lib;C:\Users\Jack\Documents\GitHubVisualStudio\TriviaDiscord\lib\boost_1_61_0\lib64-msvc-14.0;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -157,19 +157,10 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\lib\cpr\cpr\auth.cpp" />
<ClCompile Include="..\lib\cpr\cpr\cookies.cpp" />
<ClCompile Include="..\lib\cpr\cpr\cprtypes.cpp" />
<ClCompile Include="..\lib\cpr\cpr\digest.cpp" />
<ClCompile Include="..\lib\cpr\cpr\error.cpp" />
<ClCompile Include="..\lib\cpr\cpr\multipart.cpp" />
<ClCompile Include="..\lib\cpr\cpr\parameters.cpp" />
<ClCompile Include="..\lib\cpr\cpr\payload.cpp" />
<ClCompile Include="..\lib\cpr\cpr\proxies.cpp" />
<ClCompile Include="..\lib\cpr\cpr\session.cpp" />
<ClCompile Include="..\lib\cpr\cpr\util.cpp" />
<ClCompile Include="..\..\..\GitHubVisualStudio\TriviaDiscord\lib\sqlite3\sqlite3.c" />
<ClCompile Include="bot\APIHelper.cpp" />
<ClCompile Include="bot\ClientConnection.cpp" />
<ClInclude Include="..\..\..\GitHubVisualStudio\TriviaDiscord\lib\sqlite3\sqlite3.h" />
<ClInclude Include="bot\ClientConnection.hpp" />
<ClCompile Include="bot\GatewayHandler.cpp" />
<ClCompile Include="bot\http\HTTPHelper.cpp" />

View File

@ -42,44 +42,14 @@
<ClInclude Include="bot\data_structures\Guild.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\GitHubVisualStudio\TriviaDiscord\lib\sqlite3\sqlite3.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="bot\ClientConnection.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\cpr\cpr\auth.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\cpr\cpr\cookies.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\cpr\cpr\cprtypes.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\cpr\cpr\digest.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\cpr\cpr\error.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\cpr\cpr\multipart.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\cpr\cpr\parameters.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\cpr\cpr\payload.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\cpr\cpr\proxies.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\cpr\cpr\session.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\cpr\cpr\util.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="bot\GatewayHandler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@ -98,6 +68,9 @@
<ClCompile Include="bot\TriviaBot.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\GitHubVisualStudio\TriviaDiscord\lib\sqlite3\sqlite3.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="bot\data_structures\Text.txt" />

View File

@ -212,7 +212,7 @@ void TriviaGame::question() {
ah->send_message(channel_id, ":question: **(" + std::to_string(questions_asked) + "/" + std::to_string(total_questions) + ")** " + current_question);
question_start = boost::posix_time::microsec_clock::universal_time();
current_thread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&TriviaGame::give_hint, this, 0, "")));
current_thread = std::make_unique<boost::thread>(boost::bind(&TriviaGame::give_hint, this, 0, ""));
}
void TriviaGame::give_hint(int hints_given, std::string hint) {
@ -277,9 +277,9 @@ void TriviaGame::give_hint(int hints_given, std::string hint) {
}
if (hints_given < 4) {
current_thread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&TriviaGame::give_hint, this, hints_given, hint)));
current_thread = std::make_unique<boost::thread>(boost::bind(&TriviaGame::give_hint, this, hints_given, hint));
} else {
current_thread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&TriviaGame::question_failed, this)));
current_thread = std::make_unique<boost::thread>(boost::bind(&TriviaGame::question_failed, this));
}
}

View File

@ -51,7 +51,7 @@ private:
// <user_id, average_time>
std::map<std::string, int> average_times;
boost::shared_ptr<boost::thread> current_thread;
std::unique_ptr<boost::thread> current_thread;
boost::posix_time::ptime question_start;
};