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
|
|
|
{
|
2010-03-04 22:14:37 +00:00
|
|
|
int tid = (int)threadid;
|
2008-02-13 20:57:46 +00:00
|
|
|
printf("Hello World! It's me, thread #%d!\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]);
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
printf("ERROR; _beginthreadex() failed. errno = %d\n", errno);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
int rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
printf("ERROR; return code from pthread_create() is %d\n", rc);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2008-02-13 20:57:46 +00:00
|
|
|
}
|