diff --git a/src/article/embeddedimage.cpp b/src/article/embeddedimage.cpp index 5184fc904..99b47be54 100644 --- a/src/article/embeddedimage.cpp +++ b/src/article/embeddedimage.cpp @@ -17,6 +17,8 @@ #include "message/messageadmin.h" #include +#include + // // スレッドのランチャ @@ -24,7 +26,7 @@ static std::mutex eimg_launcher_mutex; int redraw_counter = 0; // 0 になったとき再描画する -void* eimg_launcher( void* dat ) +static void eimg_launcher( ARTICLE::EmbeddedImage* eimg ) { ++redraw_counter; @@ -36,7 +38,6 @@ void* eimg_launcher( void* dat ) std::cout << "start eimg_launcher" << std::endl; #endif - ARTICLE::EmbeddedImage* eimg = (ARTICLE::EmbeddedImage* )( dat ); eimg->resize_thread(); // 再描画 @@ -49,8 +50,6 @@ void* eimg_launcher( void* dat ) #ifdef _DEBUG std::cout << "end" << std::endl; #endif - - return nullptr; } @@ -78,6 +77,7 @@ EmbeddedImage::~EmbeddedImage() set_dispatchable( false ); stop(); + // m_thread.joinable() == true のときスレッドを破棄すると強制終了するため待機処理を入れる wait(); } @@ -98,7 +98,7 @@ void EmbeddedImage::wait() #ifdef _DEBUG std::cout << "EmbeddedImage::wait" << std::endl; #endif - m_thread.join(); + if( m_thread.joinable() ) m_thread.join(); } @@ -108,7 +108,7 @@ void EmbeddedImage::show() std::cout << "EmbeddedImage::show url = " << m_url << std::endl; #endif - if( m_thread.is_running() ) return; + if( m_thread.joinable() ) return; // 画像読み込み if( ! m_img->is_cached() ) return; @@ -119,7 +119,10 @@ void EmbeddedImage::show() if( ! width || ! height ) return; // スレッド起動して縮小 - if( ! m_thread.create( eimg_launcher, ( void* )this, JDLIB::NODETACH ) ){ + try { + m_thread = std::thread( eimg_launcher, this ); + } + catch( std::system_error& ) { MISC::ERRMSG( "EmbeddedImage::show : could not start thread" ); } } diff --git a/src/article/embeddedimage.h b/src/article/embeddedimage.h index b0b3664bc..dfa13e7d9 100644 --- a/src/article/embeddedimage.h +++ b/src/article/embeddedimage.h @@ -10,9 +10,11 @@ #include "skeleton/dispatchable.h" -#include "jdlib/jdthread.h" #include "jdlib/imgloader.h" +#include + + namespace DBIMG { class Img; @@ -25,7 +27,7 @@ namespace ARTICLE std::string m_url; Glib::RefPtr< Gdk::Pixbuf > m_pixbuf; DBIMG::Img* m_img; - JDLIB::Thread m_thread; + std::thread m_thread; Glib::RefPtr< JDLIB::ImgLoader > m_imgloader; diff --git a/src/dbimg/delimgcachediag.cpp b/src/dbimg/delimgcachediag.cpp index a4328f0ce..291c8ef27 100644 --- a/src/dbimg/delimgcachediag.cpp +++ b/src/dbimg/delimgcachediag.cpp @@ -19,6 +19,7 @@ #include #include +#include using namespace DBIMG; @@ -47,6 +48,7 @@ DelImgCacheDiag::~DelImgCacheDiag() std::cout << "DelImgCacheDiag::~DelImgCacheDiag\n"; #endif + // m_thread.joinable() == true のときスレッドを破棄すると強制終了するため待機処理を入れる slot_cancel_clicked(); } @@ -65,25 +67,18 @@ bool DelImgCacheDiag::on_draw( const Cairo::RefPtr< Cairo::Context >& cr ) void DelImgCacheDiag::launch_thread() { // スレッドを起動してキャッシュ削除 - if( !m_thread.is_running() ) { + if( ! m_thread.joinable() ) { m_stop = false; - if( !m_thread.create( static_cast< STARTFUNC >( launcher ), - static_cast< void* >( this ), - JDLIB::NODETACH ) ) { + try { + m_thread = std::thread( &DelImgCacheDiag::main_thread, this ); + } + catch( std::system_error& ) { MISC::ERRMSG( "DelImgCacheDiag::launch_thread : could not start thread" ); } } } -void* DelImgCacheDiag::launcher( void* dat ) -{ - DelImgCacheDiag* tt = ( DelImgCacheDiag * ) dat; - tt->main_thread(); - return nullptr; -} - - // 画像キャッシュ削除スレッド void DelImgCacheDiag::main_thread() { @@ -194,7 +189,7 @@ void DelImgCacheDiag::wait() #ifdef _DEBUG std::cout << "DelImgCacheDiag::wait\n"; #endif - m_thread.join(); + if( m_thread.joinable() ) m_thread.join(); } diff --git a/src/dbimg/delimgcachediag.h b/src/dbimg/delimgcachediag.h index 8724cc521..072e97e8f 100644 --- a/src/dbimg/delimgcachediag.h +++ b/src/dbimg/delimgcachediag.h @@ -7,12 +7,12 @@ #ifndef _DELIMGCACHEDIAG_H #define _DELIMGCACHEDIAG_H -#include "jdlib/jdthread.h" - #include "skeleton/dispatchable.h" #include #include +#include + namespace DBIMG { @@ -21,7 +21,7 @@ namespace DBIMG Gtk::Label m_label; bool m_stop; // = true にするとスレッド停止 - JDLIB::Thread m_thread; + std::thread m_thread; public: @@ -31,8 +31,6 @@ namespace DBIMG // 画像キャッシュ削除スレッド void main_thread(); - static void* launcher( void* dat ); - protected: bool on_draw( const Cairo::RefPtr< Cairo::Context >& cr ) override; diff --git a/src/image/imageareabase.cpp b/src/image/imageareabase.cpp index 830680d4c..497b31dc4 100644 --- a/src/image/imageareabase.cpp +++ b/src/image/imageareabase.cpp @@ -13,13 +13,15 @@ #include "config/globalconf.h" #include +#include + // // スレッドのランチャ // static std::mutex imgarea_launcher_mutex; -void* imgarea_launcher( void* dat ) +static void imgarea_launcher( IMAGE::ImageAreaBase* area ) { std::lock_guard< std::mutex > lock( imgarea_launcher_mutex ); @@ -27,14 +29,11 @@ void* imgarea_launcher( void* dat ) std::cout << "start imgarea_launcher" << std::endl; #endif - IMAGE::ImageAreaBase* area = ( IMAGE::ImageAreaBase* )( dat ); area->load_image_thread(); #ifdef _DEBUG std::cout << "end" << std::endl; #endif - - return nullptr; } @@ -78,6 +77,7 @@ ImageAreaBase::~ImageAreaBase() set_dispatchable( false ); stop(); + // m_thread.joinable() == true のときスレッドを破棄すると強制終了するため待機処理を入れる wait(); } @@ -98,7 +98,7 @@ void ImageAreaBase::wait() std::cout << "ImageAreaBase::wait" << std::endl; #endif - m_thread.join(); + if( m_thread.joinable() ) m_thread.join(); #ifdef _DEBUG std::cout << "ImageAreaBase::wait ok" << std::endl; @@ -131,10 +131,17 @@ void ImageAreaBase::load_image() #ifdef _DEBUG std::cout << "ImageAreaBase::load_image" << std::endl; #endif + if( m_thread.joinable() ) { + MISC::ERRMSG( "ImageAreaBase::load_image : thread is already running" ); + return; + } - // 大きい画像を表示しようとするとJDが固まるときがあるのでスレッドを使用する - // ランチャ経由で load_image_thread() を動かす - if( ! m_thread.create( imgarea_launcher, ( void* ) this, JDLIB::NODETACH ) ) { + try { + // 大きい画像を表示しようとするとJDが固まるときがあるのでスレッドを使用する + // ランチャ経由で load_image_thread() を動かす + m_thread = std::thread( imgarea_launcher, this ); + } + catch( std::system_error& ) { MISC::ERRMSG( "ImageAreaBase::load_image : could not start thread" ); } } diff --git a/src/image/imageareabase.h b/src/image/imageareabase.h index 3326af138..4168df381 100644 --- a/src/image/imageareabase.h +++ b/src/image/imageareabase.h @@ -11,9 +11,11 @@ #include "skeleton/dispatchable.h" -#include "jdlib/jdthread.h" #include "jdlib/imgloader.h" +#include + + namespace DBIMG { class Img; @@ -36,7 +38,7 @@ namespace IMAGE int m_height{}; // スレッド用変数 - JDLIB::Thread m_thread; + std::thread m_thread; protected: Glib::RefPtr< JDLIB::ImgLoader > m_imgloader; @@ -57,7 +59,7 @@ namespace IMAGE const std::string& get_errmsg() const{ return m_errmsg;} bool is_ready() const { return m_ready; } - bool is_loading() const { return m_thread.is_running(); } + bool is_loading() const noexcept { return m_thread.joinable(); } int get_width() const { return m_width; } int get_height() const { return m_height; } diff --git a/src/jdlib/Makefile.am b/src/jdlib/Makefile.am index 658f484fc..09d3d6a94 100644 --- a/src/jdlib/Makefile.am +++ b/src/jdlib/Makefile.am @@ -7,7 +7,6 @@ libjdlib_a_SOURCES = \ misctrip.cpp \ miscx.cpp \ miscgtk.cpp \ - jdthread.cpp \ misccharcode.cpp \ heap.cpp \ jdiconv.cpp \ @@ -29,7 +28,6 @@ noinst_HEADERS = \ misctrip.h \ miscx.h \ miscgtk.h \ - jdthread.h \ misccharcode.h \ refptr_lock.h \ heap.h \ diff --git a/src/jdlib/jdthread.cpp b/src/jdlib/jdthread.cpp deleted file mode 100644 index 7d09976e4..000000000 --- a/src/jdlib/jdthread.cpp +++ /dev/null @@ -1,62 +0,0 @@ -// ライセンス: GPL2 - -//#define _DEBUG -#include "jddebug.h" - -#include "jdthread.h" -#include "miscmsg.h" - -#include - -using namespace JDLIB; - - -Thread::~Thread() -{ - join(); -} - - -// スレッド作成 -bool Thread::create( STARTFUNC func , void* arg, const bool detach, const int ) -{ - if( m_thread.joinable() ){ - MISC::ERRMSG( "Thread::create : thread is already running" ); - return false; - } - - try { - m_thread = std::thread( func, arg ); - } - catch( std::system_error& err ) { - MISC::ERRMSG( err.what() ); - return false; - } - - if( detach ) { -#ifdef _DEBUG - std::cout << "Thread::create detach" << std::endl; -#endif - m_thread.detach(); - assert( ! m_thread.joinable() ); - } - -#ifdef _DEBUG - std::cout << "Thread::create thread = " << m_thread.get_id() << std::endl; -#endif - - return true; -} - - -bool Thread::join() -{ -#ifdef _DEBUG - std::cout << "Thread::join thread = " << m_thread.get_id() << std::endl; -#endif - - if( m_thread.joinable() ) { - m_thread.join(); - } - return true; -} diff --git a/src/jdlib/jdthread.h b/src/jdlib/jdthread.h deleted file mode 100644 index d4461996e..000000000 --- a/src/jdlib/jdthread.h +++ /dev/null @@ -1,49 +0,0 @@ -// ライセンス: GPL2 - -// スレッドクラス - -#ifndef _JDTHREAD_H -#define _JDTHREAD_H - -#include - - -typedef void* ( *STARTFUNC )( void * ); - -enum -{ - DEFAULT_STACKSIZE = 64 -}; - -namespace JDLIB -{ - enum - { - DETACH = true, - NODETACH = false - }; - - class Thread - { - std::thread m_thread; - - public: - - Thread() noexcept = default; - Thread( Thread&& ) noexcept = delete; - Thread( const Thread& ) = delete; - virtual ~Thread(); - - Thread& operator=( Thread&& ) noexcept = delete; - Thread& operator=( const Thread& ) = delete; - - bool is_running() const noexcept { return m_thread.joinable(); } - - // スレッド作成 - bool create( STARTFUNC func , void * arg, const bool detach, const int stack_kbyte = DEFAULT_STACKSIZE ); - - bool join(); - }; -} - -#endif diff --git a/src/jdlib/loader.cpp b/src/jdlib/loader.cpp index fcbc9da97..08b6a97d8 100644 --- a/src/jdlib/loader.cpp +++ b/src/jdlib/loader.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -496,6 +497,7 @@ Loader::~Loader() std::cout << "Loader::~Loader : url = " << m_data.url << std::endl; #endif + // m_thread.joinable() == true のときスレッドを破棄すると強制終了するため待機処理を入れる clear(); // assert( ! m_loading ); @@ -524,7 +526,7 @@ void Loader::clear() void Loader::wait() { - m_thread.join(); + if( m_thread.joinable() ) m_thread.join(); } @@ -735,29 +737,23 @@ void Loader::create_thread() #ifdef _DEBUG std::cout << "Loader::create_thread : url = " << m_data.url << std::endl; #endif + if( m_thread.joinable() ) { + MISC::ERRMSG( "Loader::create_thread : thread is already running" ); + return; + } - if( ! m_thread.create( ( STARTFUNC ) launcher, ( void * ) this, JDLIB::NODETACH ) ){ - + try { + m_thread = std::thread( &Loader::run_main, this ); + } + catch( std::system_error& ) { m_data.code = HTTP_ERR; m_data.str_code = "Loader::run : could not start thread"; MISC::ERRMSG( m_data.str_code ); finish_loading(); - return; } } -// -// スレッドのランチャ (static) -// -void* Loader::launcher( void* dat ) -{ - Loader* tt = ( Loader * ) dat; - tt->run_main(); - return nullptr; -} - - bool Loader::send_connect( const int soc, std::string& errmsg ) { std::string authority; diff --git a/src/jdlib/loader.h b/src/jdlib/loader.h index 19fec8d31..6570c2bf3 100644 --- a/src/jdlib/loader.h +++ b/src/jdlib/loader.h @@ -10,7 +10,6 @@ #define _LOADER_H #include "loaderdata.h" -#include "jdthread.h" #include #include @@ -20,6 +19,7 @@ #include #include #include +#include #include @@ -116,7 +116,7 @@ namespace JDLIB bool m_stop{}; // = true にするとスレッド停止 bool m_loading{}; - JDLIB::Thread m_thread; + std::thread m_thread; SKELETON::Loadable* m_loadable; // スレッド起動待ち状態になった時に、起動順のプライオリティを下げる @@ -152,8 +152,6 @@ namespace JDLIB private: - static void* launcher( void* ); - void clear(); void run_main(); struct addrinfo* get_addrinfo( const std::string& hostname, const int port ); diff --git a/src/jdlib/meson.build b/src/jdlib/meson.build index c87f5f9d1..2bda2c57b 100644 --- a/src/jdlib/meson.build +++ b/src/jdlib/meson.build @@ -6,7 +6,6 @@ sources = [ 'jdiconv.cpp', 'jdmigemo.cpp', 'jdregex.cpp', - 'jdthread.cpp', 'loader.cpp', 'loaderdata.cpp', 'misccharcode.cpp', diff --git a/src/searchmanager.cpp b/src/searchmanager.cpp index 7bf5faf5b..6eb1640af 100644 --- a/src/searchmanager.cpp +++ b/src/searchmanager.cpp @@ -15,6 +15,9 @@ #include "config/globalconf.h" +#include + + CORE::Search_Manager* instance_search_manager = nullptr; CORE::Search_Manager* CORE::get_search_manager() @@ -45,6 +48,7 @@ Search_Manager::~Search_Manager() set_dispatchable( false ); stop( std::string() ); + // m_thread.joinable() == true のときスレッドを破棄すると強制終了するため待機処理を入れる wait(); } @@ -70,7 +74,7 @@ bool Search_Manager::search( const std::string& id, const int searchmode, const #endif if( m_searching ) return false; - if( m_thread.is_running() ) return false; + if( m_thread.joinable() ) return false; m_searchmode = searchmode; @@ -88,9 +92,12 @@ bool Search_Manager::search( const std::string& id, const int searchmode, const // 読み込んでおかないと大量の warning が出る if( m_searchmode == SEARCHMODE_ALLLOG ) DBTREE::read_boardinfo_all(); - if( ! m_thread.create( ( STARTFUNC ) launcher, ( void * ) this, JDLIB::NODETACH ) ){ + try { + m_thread = std::thread( &Search_Manager::thread_search, this ); + } + catch( std::system_error& ) { MISC::ERRMSG( "Search_Manager::search : could not start thread" ); - return FALSE; + return false; } m_searching = true; @@ -98,17 +105,6 @@ bool Search_Manager::search( const std::string& id, const int searchmode, const } -// -// スレッドのランチャ (static) -// -void* Search_Manager::launcher( void* dat ) -{ - Search_Manager* sm = ( Search_Manager * ) dat; - sm->thread_search(); - return nullptr; -} - - // // ログ検索実行スレッド // @@ -195,7 +191,7 @@ void Search_Manager::stop( const std::string& id ) void Search_Manager::wait() { - m_thread.join(); + if( m_thread.joinable() ) m_thread.join(); } diff --git a/src/searchmanager.h b/src/searchmanager.h index 6a4cc628d..789755bfb 100644 --- a/src/searchmanager.h +++ b/src/searchmanager.h @@ -9,13 +9,12 @@ #include "skeleton/dispatchable.h" -#include "jdlib/jdthread.h" - #include #include #include #include +#include namespace DBTREE @@ -52,7 +51,7 @@ namespace CORE SIG_SEARCH_FIN m_sig_search_fin; - JDLIB::Thread m_thread; + std::thread m_thread; int m_searchmode{}; std::string m_id; @@ -109,7 +108,6 @@ namespace CORE private: - static void* launcher( void* ); void wait(); void thread_search(); void callback_dispatch() override; diff --git a/src/sound/playsound.cpp b/src/sound/playsound.cpp index 923044f9b..203d384d9 100644 --- a/src/sound/playsound.cpp +++ b/src/sound/playsound.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include @@ -56,6 +57,7 @@ Play_Sound::~Play_Sound() set_dispatchable( false ); stop(); + // m_thread.joinable() == true のときスレッドを破棄すると強制終了するため待機処理を入れる wait(); } @@ -75,7 +77,7 @@ void Play_Sound::stop() void Play_Sound::wait() { - m_thread.join(); + if( m_thread.joinable() ) m_thread.join(); } @@ -85,7 +87,7 @@ void Play_Sound::wait() void Play_Sound::play( const std::string& wavfile ) { if( wavfile.empty() ) return; - if( m_thread.is_running() ){ + if( m_thread.joinable() ){ MISC::ERRMSG( "Play_Sound::play : thread has been running" ); return; } @@ -93,23 +95,13 @@ void Play_Sound::play( const std::string& wavfile ) m_wavfile = wavfile; m_stop = false; - if( ! m_thread.create( ( STARTFUNC ) launcher, ( void * ) this, JDLIB::NODETACH ) ){ - MISC::ERRMSG( "Play_Sound::play : could not start thread" ); - } - else{ + try { + m_thread = std::thread( &Play_Sound::play_wavfile, this ); m_playing = true; } -} - - -// -// スレッドのランチャ (static) -// -void* Play_Sound::launcher( void* dat ) -{ - Play_Sound* ps = ( Play_Sound * ) dat; - ps->play_wavfile(); - return nullptr; + catch( std::system_error& ) { + MISC::ERRMSG( "Play_Sound::play : could not start thread" ); + } } diff --git a/src/sound/playsound.h b/src/sound/playsound.h index bc5fdab3a..403e6a473 100644 --- a/src/sound/playsound.h +++ b/src/sound/playsound.h @@ -12,9 +12,9 @@ #ifdef USE_ALSA #include "skeleton/dispatchable.h" -#include "jdlib/jdthread.h" #include +#include namespace SOUND @@ -49,7 +49,7 @@ namespace SOUND class Play_Sound : public SKELETON::Dispatchable { - JDLIB::Thread m_thread; + std::thread m_thread; std::string m_wavfile; bool m_stop{}; bool m_playing{}; @@ -67,7 +67,6 @@ namespace SOUND private: void wait(); - static void* launcher( void* ); void play_wavfile(); void callback_dispatch() override;