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

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

#1 modify id to context in thread

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