#include <cothreads.h>
+#define METHOD COTHREADS_CONFIG_GTHREAD_INITIALIZER
+
+#define NGTHREADS 2
+#define NCOTHREADS 5
+
+//#define USE_GTHREADS
+
void co_thread (int argc, void **argv)
{
int pthreadnum = *(int*)argv[0];
cothread_switch (self, main);
}
-void pthread (void* _pthreadnum)
+void *pthread (void* _pthreadnum)
{
int pthreadnum = *(int*) _pthreadnum;
int cothreadnum = 0;
cothread *main, *new;
- char *argv[4];
+ void *argv[4];
main = cothread_create (NULL, 0, NULL);
- while (cothreadnum++ < 25) {
+ while (cothreadnum++ < NCOTHREADS) {
printf ("%d: spawning a new cothread\n", pthreadnum);
argv[0] = &pthreadnum;
printf ("%d: switching to cothread %d...\n", pthreadnum, cothreadnum);
cothread_switch (main, new);
-
- printf ("%d: back now, looping\n", pthreadnum);
}
+ return NULL;
}
-#define NTHREADS 2
-
int main (int argc, char *argv[])
{
- GThread *thread[NTHREADS];
+ GThread *thread[NGTHREADS];
int pthreadnum[4], i;
+ cothreads_config config = METHOD;
g_thread_init(NULL);
- cothreads_init(NULL);
+
+ cothreads_init(&config);
- printf ("0: creating the gthreads\n");
+#ifdef USE_GTHREADS
+ cothread_create (NULL, 0, NULL); /* just to see where the stack is */
+#endif
- for (i=0; i<NTHREADS; i++) {
+#ifdef USE_GTHREADS
+ printf ("0: creating the gthreads\n");
+ for (i=0; i<NGTHREADS; i++) {
pthreadnum[i] = i+1;
thread[i] = g_thread_create (pthread, &pthreadnum[i], TRUE, NULL);
}
printf ("0: joining the gthreads\n");
- for (i=0; i<NTHREADS; i++) {
+ for (i=0; i<NGTHREADS; i++) {
g_thread_join (thread[i]);
}
+#else
+ printf ("0: calling the pthread function directly\n");
+ pthreadnum[0] = 1;
+ pthread (&pthreadnum[0]);
+#endif
printf ("exiting\n");