// hash_test.c
//
// コンパイル方法
// gcc -o xxx hash_test.c -pthread
#include <stdlib.h>
unsigned short hash(char* s)
{
unsigned short a=0;
int i=0;
unsigned seed=0;
srand(0);
while (s[i]!='\0') {
srand(rand()*s[i]);
i++;
}
return(rand()/0xffff);
}
// test_code
#include <stdio.h>
#include <pthread.h>
int hash_aaa;
int hash_bbb;
void* _hash(void* s)
{
int i=1000;
while (i-->0) {
unsigned short a = hash((char*)s);
if ((a!=hash_aaa) && (a!=hash_bbb)) printf("err:%s=[%d]\n", (char*)s, a);
}
return(0);
}
void main()
{
char s0[]="AAA";
char s1[]="BBB";
hash_aaa = hash(s0); printf("%sのただしいハッシュ値=[%d]\n", s0, hash_aaa);
hash_bbb = hash(s1); printf("%sのただしいハッシュ値=[%d]\n", s1, hash_bbb);
// 2本のスレッドを平行処理する
pthread_t t0;
pthread_t t1;
pthread_create(&t0, 0, _hash, (void*)s0);
pthread_create(&t1, 0, _hash, (void*)s1);
pthread_join(t0,0);
pthread_join(t1,0);
}