1 | //------------------------------------------------------------------------------ |
2 | // Copyright (c) 2016 by Lukasz Janyst <lukasz@jany.st> |
3 | //------------------------------------------------------------------------------ |
4 | // This file is part of thread-bites. |
5 | // |
6 | // thread-bites is free software: you can redistribute it and/or modify |
7 | // it under the terms of the GNU General Public License as published by |
8 | // the Free Software Foundation, either version 3 of the License, or |
9 | // (at your option) any later version. |
10 | // |
11 | // thread-bites is distributed in the hope that it will be useful, |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | // GNU General Public License for more details. |
15 | // |
16 | // You should have received a copy of the GNU General Public License |
17 | // along with thread-bites. If not, see <http://www.gnu.org/licenses/>. |
18 | //------------------------------------------------------------------------------ |
19 | |
20 | #include <tb.h> |
21 | #include <string.h> |
22 | |
23 | //------------------------------------------------------------------------------ |
24 | // Thread function |
25 | //------------------------------------------------------------------------------ |
26 | void *thread_func(void *arg) |
27 | { |
28 | int num = *(int*)arg; |
29 | int i; |
30 | |
31 | for(i = 0; i < 5; ++i) { |
32 | tbprint("Hello from thread #%d\n" , num); |
33 | tbsleep(1); |
34 | } |
35 | return 0; |
36 | } |
37 | |
38 | //------------------------------------------------------------------------------ |
39 | // Start the show |
40 | //------------------------------------------------------------------------------ |
41 | int main(int argc, char **argv) |
42 | { |
43 | tbthread_init(); |
44 | tbthread_t thread[5]; |
45 | int targ[5]; |
46 | tbthread_attr_t attr; |
47 | void *ret; |
48 | int st = 0; |
49 | tbthread_attr_init(&attr); |
50 | for(int i = 0; i < 5; ++i) { |
51 | targ[i] = i; |
52 | st = tbthread_create(&thread[i], &attr, thread_func, &targ[i]); |
53 | if(st != 0) { |
54 | tbprint("Failed to spawn thread %d: %s\n" , i, strerror(-st)); |
55 | goto exit; |
56 | } |
57 | tbthread_detach(thread[i]); |
58 | } |
59 | |
60 | tbprint("Threads spawned successfully\n" ); |
61 | |
62 | tbsleep(10); |
63 | |
64 | exit: |
65 | tbthread_finit(); |
66 | return st; |
67 | }; |
68 | |