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