Skip to content

Commit

Permalink
Merge pull request #1075 from ma8ma/use-std-thread
Browse files Browse the repository at this point in the history
Use std::thread instead of JDLIB::Thread
  • Loading branch information
ma8ma committed Dec 3, 2022
2 parents 3d43e63 + 9de4712 commit 1cc18c0
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 210 deletions.
17 changes: 10 additions & 7 deletions src/article/embeddedimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
#include "message/messageadmin.h"

#include <mutex>
#include <system_error>


//
// スレッドのランチャ
//
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;

Expand All @@ -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();

// 再描画
Expand All @@ -49,8 +50,6 @@ void* eimg_launcher( void* dat )
#ifdef _DEBUG
std::cout << "end" << std::endl;
#endif

return nullptr;
}


Expand Down Expand Up @@ -78,6 +77,7 @@ EmbeddedImage::~EmbeddedImage()
set_dispatchable( false );

stop();
// m_thread.joinable() == true のときスレッドを破棄すると強制終了するため待機処理を入れる
wait();
}

Expand All @@ -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();
}


Expand All @@ -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;
Expand All @@ -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" );
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/article/embeddedimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

#include "skeleton/dispatchable.h"

#include "jdlib/jdthread.h"
#include "jdlib/imgloader.h"

#include <thread>


namespace DBIMG
{
class Img;
Expand All @@ -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;

Expand Down
21 changes: 8 additions & 13 deletions src/dbimg/delimgcachediag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <glib/gi18n.h>

#include <ctime>
#include <system_error>


using namespace DBIMG;
Expand Down Expand Up @@ -47,6 +48,7 @@ DelImgCacheDiag::~DelImgCacheDiag()
std::cout << "DelImgCacheDiag::~DelImgCacheDiag\n";
#endif

// m_thread.joinable() == true のときスレッドを破棄すると強制終了するため待機処理を入れる
slot_cancel_clicked();
}

Expand All @@ -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()
{
Expand Down Expand Up @@ -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();
}


Expand Down
8 changes: 3 additions & 5 deletions src/dbimg/delimgcachediag.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
#ifndef _DELIMGCACHEDIAG_H
#define _DELIMGCACHEDIAG_H

#include "jdlib/jdthread.h"

#include "skeleton/dispatchable.h"

#include <gtkmm.h>
#include <ctime>
#include <thread>


namespace DBIMG
{
Expand All @@ -21,7 +21,7 @@ namespace DBIMG
Gtk::Label m_label;

bool m_stop; // = true にするとスレッド停止
JDLIB::Thread m_thread;
std::thread m_thread;

public:

Expand All @@ -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;
Expand Down
23 changes: 15 additions & 8 deletions src/image/imageareabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,27 @@
#include "config/globalconf.h"

#include <mutex>
#include <system_error>


//
// スレッドのランチャ
//
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 );

#ifdef _DEBUG
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;
}


Expand Down Expand Up @@ -78,6 +77,7 @@ ImageAreaBase::~ImageAreaBase()
set_dispatchable( false );

stop();
// m_thread.joinable() == true のときスレッドを破棄すると強制終了するため待機処理を入れる
wait();
}

Expand All @@ -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;
Expand Down Expand Up @@ -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" );
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/image/imageareabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

#include "skeleton/dispatchable.h"

#include "jdlib/jdthread.h"
#include "jdlib/imgloader.h"

#include <thread>


namespace DBIMG
{
class Img;
Expand All @@ -36,7 +38,7 @@ namespace IMAGE
int m_height{};

// スレッド用変数
JDLIB::Thread m_thread;
std::thread m_thread;

protected:
Glib::RefPtr< JDLIB::ImgLoader > m_imgloader;
Expand All @@ -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; }
Expand Down
2 changes: 0 additions & 2 deletions src/jdlib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ libjdlib_a_SOURCES = \
misctrip.cpp \
miscx.cpp \
miscgtk.cpp \
jdthread.cpp \
misccharcode.cpp \
heap.cpp \
jdiconv.cpp \
Expand All @@ -29,7 +28,6 @@ noinst_HEADERS = \
misctrip.h \
miscx.h \
miscgtk.h \
jdthread.h \
misccharcode.h \
refptr_lock.h \
heap.h \
Expand Down
62 changes: 0 additions & 62 deletions src/jdlib/jdthread.cpp

This file was deleted.

0 comments on commit 1cc18c0

Please sign in to comment.