Shinya Kitaoka 120a6e
#include <stdexcept>  // std::domain_error()</stdexcept>
Toshihiro Shimizu 890ddd
#include "igs_resource_msg_from_err.h"
Toshihiro Shimizu 890ddd
#include "igs_resource_thread.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
// pthread_t = unsigned long int(rhel4)
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 120a6e
        state が
Shinya Kitaoka 120a6e
        PTHREAD_CREATE_JOINABLE なら、pthread_join()を呼ぶこと。
Shinya Kitaoka 120a6e
        PTHREAD_CREATE_DETACHED なら、なにも呼ぶ必要がないが、
Shinya Kitaoka 120a6e
                thread終了を知るには自前で仕掛けが必要。
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
pthread_t igs::resource::thread_run(
Shinya Kitaoka 120a6e
    void *(*function)(void *), void *func_arg,
Shinya Kitaoka 120a6e
    const int state  // PTHREAD_CREATE_JOINABLE/PTHREAD_CREATE_DETACHED
Shinya Kitaoka 120a6e
    ) {
Shinya Kitaoka 120a6e
  pthread_attr_t attr;
Shinya Kitaoka 120a6e
  if (::pthread_attr_init(&attr)) {
Shinya Kitaoka 120a6e
    throw std::domain_error("pthread_attr_init(-)");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (::pthread_attr_setdetachstate(&attr, state)) {
Shinya Kitaoka 120a6e
    throw std::domain_error("pthread_attr_setdetachstate(-)");
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  pthread_t thread_id = 0;
Shinya Kitaoka 120a6e
  const int erno = ::pthread_create(&(thread_id), &attr, function, func_arg);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (0 != erno) {
Shinya Kitaoka 120a6e
    throw std::domain_error(
Shinya Kitaoka 120a6e
        igs_resource_msg_from_err("pthread_create(-)", erno));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return thread_id;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
const bool igs::resource::thread_was_done(const pthread_t thread_id) {
Toshihiro Shimizu 890ddd
??????????????????????????????????????????????????????????????????
Toshihiro Shimizu 890ddd
??????????????????????????????????????????????????????????????????
Toshihiro Shimizu 890ddd
??????????????????????????????????????????????????????????????????
Toshihiro Shimizu 890ddd
threadの終了方法を見る関数は見つからない。
Toshihiro Shimizu 890ddd
実行関数の引数で、終了フラグを立てて、外から感知する方法か。
Toshihiro Shimizu 890ddd
関数が終了するまでの間のタイムラグがあるが、問題はあるのか???
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
void igs::resource::thread_join(const pthread_t thread_id) {
Shinya Kitaoka 120a6e
  const int erno = ::pthread_join(thread_id, NULL);
Shinya Kitaoka 120a6e
  if (0 != erno) {
Shinya Kitaoka 120a6e
    throw std::domain_error(igs_resource_msg_from_err("pthread_join(-)", erno));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}