126 lines
2.6 KiB
C
126 lines
2.6 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "../../tthread.h"
|
|
|
|
MUTEX mutex;
|
|
COND cond;
|
|
MUTEX stdio;
|
|
|
|
void work1(void* arg) {
|
|
int* num = (int*)arg;
|
|
for (int i = 0; i < 100; i++) {
|
|
thread_mutex_lock(&mutex);
|
|
tfprintf(&stdio, stdout, "%d => %d\n", thread_self(), (*num)++);
|
|
thread_mutex_unlock(&mutex);
|
|
}
|
|
}
|
|
|
|
int test1() {
|
|
int res;
|
|
#define MAX1 2
|
|
TID tid[MAX1] = { 0 };
|
|
int num = 1;
|
|
for (int i = 0; i < MAX1; i++) {
|
|
if (thread_create(&tid[i], work1, &num) != 0) {
|
|
return -2;
|
|
}
|
|
}
|
|
//res = thread_join(tid[0]);
|
|
//if (res != 0) {
|
|
// return res;
|
|
//}
|
|
|
|
//res = thread_join(tid[1]);
|
|
//if (res != 0) {
|
|
// return res;
|
|
//}
|
|
for (int i = 0; i < MAX1; i++) {
|
|
res = thread_join(tid[i]);
|
|
if (res != 0 && res != ERR_THREAD_WIN_OPEN_THREAD) {
|
|
return res;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void work2(void* arg) {
|
|
thread_mutex_lock(&mutex);
|
|
thread_cond_wait(&cond, &mutex);
|
|
tfprintf(&stdio, stdout, "<%lu>\n", thread_self());
|
|
thread_mutex_unlock(&mutex);
|
|
}
|
|
|
|
int test2() {
|
|
int res;
|
|
#define MAX 6
|
|
TID tid[MAX] = { 0 };
|
|
for (int i = 0; i < MAX; i++) {
|
|
if (thread_create(&tid[i], work2, NULL) != 0) {
|
|
return -2;
|
|
}
|
|
}
|
|
//thread_mutex_lock(&mutex);
|
|
tfprintf(&stdio, stdout, "press enter to continue\n");
|
|
if (getchar());
|
|
//thread_mutex_unlock(&mutex);
|
|
thread_cond_singal(&cond);
|
|
Sleep(1000);
|
|
|
|
//thread_mutex_lock(&mutex);
|
|
tfprintf(&stdio, stdout, "press enter to continue\n");
|
|
if (getchar());
|
|
//thread_mutex_unlock(&mutex);
|
|
thread_cond_broadcast(&cond);
|
|
|
|
for (int i = 0; i < MAX; i++) {
|
|
res = thread_join(tid[i]);
|
|
if (res != 0 && res != ERR_THREAD_WIN_OPEN_THREAD) {
|
|
return res;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
//DWORD a[3] = { 1, 2, 3 };
|
|
//LPDWORD p = a;
|
|
//printf("a[0] = %d, a[1] = %d, a[3] = %d\n", a[0], a[1], a[2]);
|
|
//printf("p[0] = %d, p[1] = %d, p[3] = %d\n", p[0], p[1], p[2]);
|
|
//printf("DWORD:%lld LPDWORD:%lld\n", sizeof(a), sizeof(p));
|
|
//printf("DWORD:%lld LPDWORD:%lld long:%lld\n", sizeof(DWORD), sizeof(LPDWORD), sizeof(long));
|
|
//return 0;
|
|
int res;
|
|
if (thread_mutex_init(&mutex) != 0) {
|
|
return -1;
|
|
}
|
|
if (thread_mutex_init(&stdio) != 0) {
|
|
return -1;
|
|
}
|
|
tfprintf(&stdio, stdout, "test1 start...\n");
|
|
res = test1();
|
|
if (res != 0) {
|
|
return GetLastError();
|
|
}
|
|
tfprintf(&stdio, stdout, "test1 end...\n");
|
|
|
|
if (thread_cond_init(&cond) != 0) {
|
|
return -2;
|
|
}
|
|
tfprintf(&stdio, stdout, "test2 start...\n");
|
|
res = test2();
|
|
if (res != 0) {
|
|
return GetLastError();
|
|
}
|
|
tfprintf(&stdio, stdout, "test2 end...\n");
|
|
if(getchar());
|
|
|
|
if (thread_mutex_destroy(&mutex) != 0) {
|
|
return -3;
|
|
}
|
|
if (thread_cond_destroy(&cond) != 0) {
|
|
return -4;
|
|
}
|
|
|
|
return 0;
|
|
} |