strict cstyle pthread
This commit is contained in:
parent
279758a92e
commit
2eccdda3c5
@ -123,16 +123,33 @@ class Semaphore {
|
|||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// helper for c thread
|
||||||
|
// used to strictly call c++ function from pthread
|
||||||
|
struct ThreadContext {
|
||||||
|
void *(*entry)(void*);
|
||||||
|
void *param;
|
||||||
|
};
|
||||||
|
extern "C" {
|
||||||
|
inline void *RunThreadContext(void *ctx_) {
|
||||||
|
ThreadContext *ctx = reinterpret_cast<ThreadContext*>(ctx_);
|
||||||
|
void *ret = (*ctx->entry)(ctx->param);
|
||||||
|
delete ctx;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
/*!\brief simple thread class */
|
/*!\brief simple thread class */
|
||||||
class Thread {
|
class Thread {
|
||||||
private:
|
private:
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
|
|
||||||
public :
|
public :
|
||||||
inline void Start(void *entry(void*), void *param) {
|
inline void Start(void *entry(void*), void *param) {
|
||||||
pthread_attr_t attr;
|
pthread_attr_t attr;
|
||||||
pthread_attr_init(&attr);
|
pthread_attr_init(&attr);
|
||||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
||||||
pthread_create(&thread, &attr, entry, param);
|
ThreadContext *ctx = new ThreadContext();
|
||||||
|
ctx->entry = entry; ctx->param = param;
|
||||||
|
pthread_create(&thread, &attr, RunThreadContext, ctx);
|
||||||
}
|
}
|
||||||
inline int Join(void) {
|
inline int Join(void) {
|
||||||
void *status;
|
void *status;
|
||||||
@ -142,6 +159,7 @@ class Thread {
|
|||||||
inline void ThreadExit(void *status) {
|
inline void ThreadExit(void *status) {
|
||||||
pthread_exit(status);
|
pthread_exit(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace utils
|
} // namespace utils
|
||||||
} // namespace xgboost
|
} // namespace xgboost
|
||||||
#define XGBOOST_THREAD_PREFIX void *
|
#define XGBOOST_THREAD_PREFIX void *
|
||||||
|
|||||||
@ -167,7 +167,6 @@ class ThreadBuffer {
|
|||||||
/*!\brief entry point of loader thread */
|
/*!\brief entry point of loader thread */
|
||||||
inline static XGBOOST_THREAD_PREFIX LoaderEntry(void *pthread) {
|
inline static XGBOOST_THREAD_PREFIX LoaderEntry(void *pthread) {
|
||||||
static_cast< ThreadBuffer<Elem,ElemFactory>* >(pthread)->RunLoader();
|
static_cast< ThreadBuffer<Elem,ElemFactory>* >(pthread)->RunLoader();
|
||||||
ThreadExit(NULL);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
/*!\brief start loader thread */
|
/*!\brief start loader thread */
|
||||||
@ -199,6 +198,7 @@ class ThreadBuffer {
|
|||||||
loading_need.Post();
|
loading_need.Post();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace utils
|
} // namespace utils
|
||||||
} // namespace xgboost
|
} // namespace xgboost
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user