comparison examples/twothreads.c @ 168:ead06911cbae

Add up-to-date examples
author jlarmour
date Wed, 04 Jul 2001 17:34:09 +0000
parents
children
comparison
equal deleted inserted replaced
167:59f91bb9f684 168:ead06911cbae
1 #include <cyg/kernel/kapi.h>
2
3 #include <stdio.h>
4 #include <math.h>
5 #include <stdlib.h>
6
7 /* now declare (and allocate space for) some kernel objects,
8 like the two threads we will use */
9 cyg_thread thread_s[2]; /* space for two thread objects */
10
11 char stack[2][4096]; /* space for two 4K stacks */
12
13 /* now the handles for the threads */
14 cyg_handle_t simple_threadA, simple_threadB;
15
16 /* and now variables for the procedure which is the thread */
17 cyg_thread_entry_t simple_program;
18
19 /* and now a mutex to protect calls to the C library */
20 cyg_mutex_t cliblock;
21
22 /* we install our own startup routine which sets up threads */
23 void cyg_user_start(void)
24 {
25 printf("Entering twothreads' cyg_user_start() function\n");
26
27 cyg_mutex_init(&cliblock);
28
29 cyg_thread_create(4, simple_program, (cyg_addrword_t) 0,
30 "Thread A", (void *) stack[0], 4096,
31 &simple_threadA, &thread_s[0]);
32 cyg_thread_create(4, simple_program, (cyg_addrword_t) 1,
33 "Thread B", (void *) stack[1], 4096,
34 &simple_threadB, &thread_s[1]);
35
36 cyg_thread_resume(simple_threadA);
37 cyg_thread_resume(simple_threadB);
38 }
39
40 /* this is a simple program which runs in a thread */
41 void simple_program(cyg_addrword_t data)
42 {
43 int message = (int) data;
44 int delay;
45
46 printf("Beginning execution; thread data is %d\n", message);
47
48 cyg_thread_delay(200);
49
50 for (;;) {
51 delay = 200 + (rand() % 50);
52
53 /* note: printf() must be protected by a
54 call to cyg_mutex_lock() */
55 cyg_mutex_lock(&cliblock); {
56 printf("Thread %d: and now a delay of %d clock ticks\n",
57 message, delay);
58 }
59 cyg_mutex_unlock(&cliblock);
60 cyg_thread_delay(delay);
61 }
62 }