Toshihiro Shimizu 890ddd
#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
/*
Toshihiro Shimizu 890ddd
	state が
Toshihiro Shimizu 890ddd
	PTHREAD_CREATE_JOINABLE なら、pthread_join()を呼ぶこと。
Toshihiro Shimizu 890ddd
	PTHREAD_CREATE_DETACHED なら、なにも呼ぶ必要がないが、
Toshihiro Shimizu 890ddd
		thread終了を知るには自前で仕掛けが必要。
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
pthread_t igs::resource::thread_run(
Toshihiro Shimizu 890ddd
	void *(*function)(void *), void *func_arg, const int state // PTHREAD_CREATE_JOINABLE/PTHREAD_CREATE_DETACHED
Toshihiro Shimizu 890ddd
	)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	pthread_attr_t attr;
Toshihiro Shimizu 890ddd
	if (::pthread_attr_init(&attr)) {
Toshihiro Shimizu 890ddd
		throw std::domain_error("pthread_attr_init(-)");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (::pthread_attr_setdetachstate(&attr, state)) {
Toshihiro Shimizu 890ddd
		throw std::domain_error("pthread_attr_setdetachstate(-)");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pthread_t thread_id = 0;
Toshihiro Shimizu 890ddd
	const int erno = ::pthread_create(
Toshihiro Shimizu 890ddd
		&(thread_id), &attr, function, func_arg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (0 != erno) {
Toshihiro Shimizu 890ddd
		throw std::domain_error(igs_resource_msg_from_err(
Toshihiro Shimizu 890ddd
			"pthread_create(-)", erno));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	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
*/
Toshihiro Shimizu 890ddd
void igs::resource::thread_join(const pthread_t thread_id)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	const int erno = ::pthread_join(thread_id, NULL);
Toshihiro Shimizu 890ddd
	if (0 != erno) {
Toshihiro Shimizu 890ddd
		throw std::domain_error(igs_resource_msg_from_err(
Toshihiro Shimizu 890ddd
			"pthread_join(-)", erno));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}