39 lines
651 B
C
39 lines
651 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "../../tthread.h"
|
|
|
|
MUTEX mutex;
|
|
|
|
void work(void* arg) {
|
|
int* num = (int*)arg;
|
|
for (int i = 0; i < 100; i++) {
|
|
thread_mutex_lock(mutex);
|
|
printf("%d => %d\n", 1, (*num)++);
|
|
thread_mutex_unlock(mutex);
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
int res;
|
|
TID tid;
|
|
int num = 0;
|
|
if (thread_mutex_init(&mutex) != 0) {
|
|
return -1;
|
|
}
|
|
if (thread_create(&tid, work, &num) != 0) {
|
|
return -2;
|
|
}
|
|
for (int i = 0; i < 100; i++) {
|
|
thread_mutex_lock(mutex);
|
|
printf("%d => %d\n", 0, num++);
|
|
thread_mutex_unlock(mutex);
|
|
}
|
|
res = thread_join(tid);
|
|
if (res != 0) {
|
|
//res = GetLastError();
|
|
return res;
|
|
}
|
|
|
|
return 0;
|
|
} |