2010-05-26 19:38:11 +00:00
# ifdef __MINGW32__
# include <process.h> // MinGW has no POSIX support; use MSVC runtime
# else
# include <pthread.h>
# endif
2008-02-13 20:57:46 +00:00
# include <stdio.h>
# include <stdlib.h>
2010-05-26 19:38:11 +00:00
# include "Sleep.h"
2008-02-13 20:57:46 +00:00
# define NUM_THREADS 5
2010-05-26 19:38:11 +00:00
# ifdef __MINGW32__
typedef unsigned int TID ;
# else
typedef pthread_t TID ;
# endif
# ifdef __MINGW32__
unsigned int __stdcall PrintHello ( void * threadid )
# else
2008-02-13 20:57:46 +00:00
void * PrintHello ( void * threadid )
2010-05-26 19:38:11 +00:00
# endif
2008-02-13 20:57:46 +00:00
{
2013-01-14 06:23:04 -05:00
long tid = ( long ) threadid ;
printf ( " Hello World! It's me, thread #%ld! \n " , tid ) ;
2010-05-26 19:38:11 +00:00
SLEEP ( 2 ) ; // keep this thread around for a bit; the tests will check for its existence while the main thread is stopped at a breakpoint
# ifdef __MINGW32__
return 0 ;
# else
2008-02-13 20:57:46 +00:00
pthread_exit ( NULL ) ;
2010-05-26 19:38:11 +00:00
# endif
2008-02-13 20:57:46 +00:00
}
int main ( int argc , char * argv [ ] )
{
2010-05-26 19:38:11 +00:00
TID threads [ NUM_THREADS ] ;
int t ;
for ( t = 0 ; t < NUM_THREADS ; t + + )
{
printf ( " In main: creating thread %d \n " , t ) ;
# ifdef __MINGW32__
{
uintptr_t rc = _beginthreadex ( NULL , 0 , PrintHello , ( void * ) t , 0 , & threads [ t ] ) ;
2010-05-27 21:20:45 +00:00
SLEEP ( 1 ) ; // debugger should for sure receive thread creation event after stepping over this sleep; not guaranteed to happen simply stepping over the thread creation call
2010-05-26 19:38:11 +00:00
if ( rc = = 0 )
{
printf ( " ERROR; _beginthreadex() failed. errno = %d \n " , errno ) ;
exit ( - 1 ) ;
}
}
# else
{
int rc = pthread_create ( & threads [ t ] , NULL , PrintHello , ( void * ) t ) ;
2010-05-27 21:20:45 +00:00
SLEEP ( 1 ) ; // debugger should for sure receive thread creation event after stepping over this sleep; not guaranteed to happen simply stepping over the thread creation call
2010-05-26 19:38:11 +00:00
if ( rc )
{
printf ( " ERROR; return code from pthread_create() is %d \n " , rc ) ;
exit ( - 1 ) ;
}
}
# endif
}
2010-05-27 21:20:45 +00:00
return 0 ;
2008-02-13 20:57:46 +00:00
}