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

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

#1 fix test code for r147

File size: 983 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
10CF_Mutex_Ctx globalMutex;
11
12int worker (void * arg) {
13 if (CF_Mutex_Lock (globalMutex) < 0) { // for critical section
14 // error
15 }
16
17 fprintf (stderr, "here is critical section !\n");
18
19 if (CF_Mutex_Unlock (globalMutex) < 0) { // for critical section
20 // error
21 }
22
23 return 0;
24}
25
26int main (void) {
27 CF_Thread_Ctx tid[10];
28 int i = 0;
29
30 if (CF_Mutex_CreateCtx (&globalMutex) < 0) {
31 // error
32 }
33
34 for (i = 0 ; i < 10 ; i++) {
35 if (CF_Thread_CreateCtx (&tid[i], worker, NULL) < 0) {
36 // error
37 }
38 }
39
40 for (i = 0 ; i < 10 ; i++) {
41 if (CF_Thread_Start (tid[i]) < 0) {
42 // error
43 }
44 }
45
46 for (i = 0 ; i < 10 ; i++) {
47 if (CF_Thread_Join (tid[i]) < 0) { // block
48 // error
49 }
50 }
51
52 for (i = 0 ; i < 10 ; i++) {
53 if (CF_Thread_DestroyCtx (tid[i]) < 0) {
54 // error
55 }
56 }
57
58 if (CF_Mutex_DestoryCtx (globalMutex) < 0) {
59 // error
60 }
61 return 0;
62}
Note: See TracBrowser for help on using the repository browser.