source: libcf/trunk/test/thread.c@ 151

Last change on this file since 151 was 151, checked in by cheese, 11 years ago

#1 fix interface and add util module

File size: 991 bytes
Line 
1/**
2 * @file thread.c
3 * @author myusgun <myusgun@gmail.com>
4 */
5#include "cf_thread.h"
6#include "cf_mutex.h"
7
8#include <stdio.h>
9
10#define COUNT 5
11
12cf_ctx globalMutex;
13
14int worker (void * arg)
15{
16 if (CF_Mutex_Lock (globalMutex) < 0) { // for critical section
17 // error
18 }
19
20 fprintf (stderr, "here is critical section !\n");
21
22 if (CF_Mutex_Unlock (globalMutex) < 0) { // for critical section
23 // error
24 }
25
26 return 0;
27}
28
29int main (void)
30{
31 cf_ctx tid[COUNT];
32 int i = 0;
33
34 if (CF_Mutex_Create (&globalMutex) < 0) {
35 // error
36 }
37
38 for (i = 0 ; i < COUNT ; i++) {
39 if (CF_Thread_Create (&tid[i], worker, NULL) < 0) {
40 // error
41 }
42 }
43
44 for (i = 0 ; i < COUNT ; i++) {
45 if (CF_Thread_Start (tid[i]) < 0) {
46 // error
47 }
48 }
49
50 for (i = 0 ; i < COUNT ; i++) {
51 if (CF_Thread_Join (tid[i]) < 0) { // block
52 // error
53 }
54 }
55
56 for (i = 0 ; i < COUNT ; i++) {
57 if (CF_Thread_Destroy (tid[i]) < 0) {
58 // error
59 }
60 }
61
62 if (CF_Mutex_Destory (globalMutex) < 0) {
63 // error
64 }
65 return 0;
66}
Note: See TracBrowser for help on using the repository browser.