@@ -29,7 +29,7 @@ freely, subject to the following restrictions:
2929#elif defined(_GTHREADS_WIN32_)
3030 #include < process.h>
3131#endif
32-
32+ # include < string.h >
3333
3434// namespace tthread {
3535
@@ -146,6 +146,16 @@ static thread::id _pthread_t_to_ID(const pthread_t &aHandle)
146146#endif // _GTHREADS_POSIX_
147147*/
148148
149+ void gthreads_errExit (int err, const char * msg) {
150+ if (msg!=NULL )
151+ fprintf (stderr, " GThreads Error: %s (%s)\n " , msg, strerror (err));
152+ else
153+ fprintf (stderr, " GThreads Error: %s\n " , strerror (err));
154+ exit (EXIT_FAILURE);
155+ }
156+
157+
158+
149159void GThread::update_counter (int inc, GThread* t_update) {
150160 static GMutex counterLock;
151161 GLockGuard<GMutex> guard (counterLock);
@@ -158,7 +168,7 @@ void GThread::update_counter(int inc, GThread* t_update) {
158168 t_update->mId =0 ; // thread terminated
159169
160170 }
161-
171+
162172
163173// ------------------------------------------------------------------------------
164174// thread
@@ -240,7 +250,7 @@ void * GThread::wrapper_function(void * aArg)
240250}
241251
242252
243- void GThread::initStart (void * tidata) {
253+ void GThread::initStart (void * tidata, size_t stacksize ) {
244254 _thread_start_info * ti = (_thread_start_info *) tidata;
245255 /* ti->mFunction = aFunction;
246256 ti->mArg = aArg;
@@ -253,10 +263,29 @@ void GThread::initStart(void* tidata) {
253263#if defined(_GTHREADS_WIN32_)
254264 mHandle = (HANDLE) _beginthreadex (0 , 0 , wrapper_function, (void *) ti, 0 , &mWin32ThreadID );
255265#elif defined(_GTHREADS_POSIX_)
256- if (pthread_create (&mHandle , NULL , wrapper_function, (void *) ti) != 0 )
257- mHandle = 0 ;
258- #endif
266+ if (stacksize>0 ) {
267+ pthread_attr_t attr;
268+ int r=pthread_attr_init (&attr);
269+ if (r!=0 ) gthreads_errExit (r, " pthread_attr_init()" );
270+ r = pthread_attr_setstacksize (&attr, stacksize);
271+ if (r!=0 ) gthreads_errExit (r, " pthread_attr_setstacksize()" );
272+ stack_size=stacksize;
273+ r=pthread_create (&mHandle , &attr, wrapper_function, (void *) ti);
274+ if (r!=0 ) {
275+ gthreads_errExit (r, " pthread_create()" );
276+ // mHandle = 0;
277+ }
278+ r=pthread_attr_destroy (&attr);
279+ if (r!=0 ) gthreads_errExit (r, " pthread_attr_destroy()" );
280+ }
281+ else {
282+ int r=pthread_create (&mHandle , NULL , wrapper_function, (void *) ti);
283+ if (r!= 0 )
284+ gthreads_errExit (r, " pthread_create()" );
285+ // mHandle = 0;
286+ }
259287
288+ #endif
260289 // Did we fail to create the thread?
261290 if (!mHandle )
262291 {
@@ -266,39 +295,38 @@ void GThread::initStart(void* tidata) {
266295 else GThread::update_counter (1 , this );
267296}
268297
269- // GThread::GThread(void (*aFunction)(void *, GThread*), void * aArg)
270- GThread::GThread (void (*aFunction)(void *), void * aArg): mId(0 ), mHandle(0 ), mNotAThread(true )
298+ GThread::GThread (void (*aFunction)(void *), void * aArg, size_t stacksize): mId(0 ), mHandle(0 ), mNotAThread(true )
271299#if defined(_GTHREADS_WIN32_)
272300 , mWin32ThreadID (0 )
273301#endif
274302 {
275- kickStart (aFunction, aArg);
303+ kickStart (aFunction, aArg, stacksize );
276304}
277305
278- void GThread::kickStart (void (*aFunction)(void *), void * aArg) {
306+ void GThread::kickStart (void (*aFunction)(void *), void * aArg, size_t stacksize ) {
279307 // Serialize access to this thread structure
280308 GLockGuard<GMutex> guard (mDataMutex );
281309 // Fill out the thread startup information (passed to the thread wrapper,
282310 // which will eventually free it)
283311 _thread_start_info * ti = new _thread_start_info (this , aFunction, aArg);
284- initStart (ti);
312+ initStart (ti, stacksize );
285313}
286314
287315// custom alternate constructor (non-C++11 compatible), passing GThreadData back to the
288316// user function in order to easily retrieve current GThread object
289317// (better alternative to this_thread)
290- GThread::GThread (void (*gFunction )(GThreadData& thread_data), void * aArg) {
291- kickStart (gFunction , aArg);
318+ GThread::GThread (void (*gFunction )(GThreadData& thread_data), void * aArg, size_t stacksize ) {
319+ kickStart (gFunction , aArg, stacksize );
292320}
293321
294- void GThread::kickStart (void (*gFunction )(GThreadData& thread_data), void * aArg) {
322+ void GThread::kickStart (void (*gFunction )(GThreadData& thread_data), void * aArg, size_t stacksize ) {
295323 // Serialize access to this thread structure
296324 GLockGuard<GMutex> guard (mDataMutex );
297325
298326 // Fill out the thread startup information (passed to the thread wrapper,
299327 // which will eventually free it)
300328 _thread_start_info * ti = new _thread_start_info (this , gFunction , aArg);
301- initStart (ti);
329+ initStart (ti, stacksize );
302330}
303331
304332GThread::~GThread ()
0 commit comments