2002-01-28 01:56:17 +00:00
|
|
|
#include <cothreads.h>
|
|
|
|
|
2002-01-29 05:49:57 +00:00
|
|
|
void co_thread (int argc, void **argv)
|
2002-01-28 01:56:17 +00:00
|
|
|
{
|
2002-01-29 05:49:57 +00:00
|
|
|
int pthreadnum = *(int*)argv[0];
|
|
|
|
int cothreadnum = *(int*)argv[1];
|
|
|
|
cothread *main = argv[2];
|
|
|
|
cothread *self = argv[3];
|
|
|
|
|
|
|
|
printf ("%d.%d: sleeping 1s...\n", pthreadnum, cothreadnum);
|
2002-01-28 01:56:17 +00:00
|
|
|
sleep (1);
|
2002-01-29 05:49:57 +00:00
|
|
|
printf ("%d.%d: returning to cothread 0\n", pthreadnum, cothreadnum);
|
|
|
|
|
|
|
|
cothread_switch (self, main);
|
2002-01-28 01:56:17 +00:00
|
|
|
}
|
|
|
|
|
2002-01-29 05:49:57 +00:00
|
|
|
void pthread (void* _pthreadnum)
|
2002-01-28 01:56:17 +00:00
|
|
|
{
|
2002-01-29 05:49:57 +00:00
|
|
|
int pthreadnum = *(int*) _pthreadnum;
|
|
|
|
int cothreadnum = 0;
|
|
|
|
cothread *main, *new;
|
|
|
|
char *argv[4];
|
2002-01-28 01:56:17 +00:00
|
|
|
|
2002-01-29 05:49:57 +00:00
|
|
|
main = cothread_create (NULL, 0, NULL);
|
2002-01-28 01:56:17 +00:00
|
|
|
|
2002-01-29 05:49:57 +00:00
|
|
|
while (cothreadnum++ < 25) {
|
|
|
|
printf ("%d: spawning a new cothread\n", pthreadnum);
|
2002-01-28 01:56:17 +00:00
|
|
|
|
2002-01-29 05:49:57 +00:00
|
|
|
argv[0] = &pthreadnum;
|
|
|
|
argv[1] = &cothreadnum;
|
|
|
|
argv[2] = main;
|
|
|
|
argv[3] = cothread_create (co_thread, 4, argv);
|
|
|
|
new = argv[3];
|
|
|
|
|
|
|
|
printf ("%d: switching to cothread %d...\n", pthreadnum, cothreadnum);
|
|
|
|
cothread_switch (main, new);
|
|
|
|
|
|
|
|
printf ("%d: back now, looping\n", pthreadnum);
|
2002-01-28 01:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-29 05:49:57 +00:00
|
|
|
#define NTHREADS 2
|
2002-01-28 01:56:17 +00:00
|
|
|
|
|
|
|
int main (int argc, char *argv[])
|
|
|
|
{
|
2002-01-29 05:49:57 +00:00
|
|
|
GThread *thread[NTHREADS];
|
|
|
|
int pthreadnum[4], i;
|
2002-01-28 01:56:17 +00:00
|
|
|
|
|
|
|
g_thread_init(NULL);
|
2002-01-29 05:49:57 +00:00
|
|
|
cothreads_init(NULL);
|
|
|
|
|
|
|
|
printf ("0: creating the gthreads\n");
|
|
|
|
|
|
|
|
for (i=0; i<NTHREADS; 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++) {
|
|
|
|
g_thread_join (thread[i]);
|
|
|
|
}
|
2002-01-28 01:56:17 +00:00
|
|
|
|
|
|
|
printf ("exiting\n");
|
|
|
|
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|