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