Files
haiku-beta6/src/kernel/apps/tests/thread_test.c
T

67 lines
1.3 KiB
C
Raw Normal View History

2002-07-09 12:24:59 +00:00
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <syscalls.h>
#include <ktypes.h>
#include <resource.h>
#include <Errors.h>
#include <OS.h>
#include <stdlib.h>
#define THREADS 5
#define START_VAL 1
struct the_test {
int *current_val;
int thread_mult;
sem_id the_lock;
};
static int counter_thread(void *data)
{
struct the_test *tt = (struct the_test*)data;
acquire_sem(tt->the_lock);
*(tt->current_val) *= tt->thread_mult;
release_sem(tt->the_lock);
free(data);
2002-07-20 23:19:48 +00:00
return 0;
2002-07-09 12:24:59 +00:00
}
int main(int argc, char **argv)
{
thread_id t[THREADS];
sem_id lock;
int i;
int expected = START_VAL, current_val = START_VAL;
printf("OpenBeOS Thread Test - Simple\n"
"=============================\n");
lock = create_sem(1, "test_lock");
for (i=0;i<THREADS;i++) {
struct the_test *my_test = (struct the_test*)malloc(sizeof(struct the_test));
my_test->the_lock = lock;
my_test->current_val = &current_val;
my_test->thread_mult = i + 1;
t[i] = spawn_thread(counter_thread, "counter", B_NORMAL_PRIORITY, my_test);
2002-07-09 12:24:59 +00:00
if (t[i] > 0) {
resume_thread(t[i]);
expected *= (i + 1);
}
}
sys_wait_on_thread(t[THREADS - 1], NULL);
2002-07-09 12:24:59 +00:00
printf("Test value = %d vs expected of %d\n", current_val, expected);
delete_sem(lock);
return 0;
}