From c8bf38e5fb717d40635a2a89b22ed71b0de4266b Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Tue, 1 Dec 2020 13:33:34 +0100 Subject: Squashed 'dependencies/uthash/' content from commit 8e67ced git-subtree-dir: dependencies/uthash git-subtree-split: 8e67ced1d1c5bd8141c542a22630e6de78aa6b90 --- tests/threads/test1.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 tests/threads/test1.c (limited to 'tests/threads/test1.c') diff --git a/tests/threads/test1.c b/tests/threads/test1.c new file mode 100644 index 000000000..8d6cb6747 --- /dev/null +++ b/tests/threads/test1.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "uthash.h" + +#undef uthash_noexpand_fyi +#define uthash_noexpand_fyi(tbl) fprintf(stderr,"warning: bucket expansion inhibited\n") + +#define LOOPS 100000 + +typedef struct { + int i; + UT_hash_handle hh; +} elt; + +elt *elts=NULL; /* this is our hash table which two threads will use */ +pthread_rwlock_t lock; + +void *thread_routine_r( void *arg ) { + int i; + long num_found=0; + elt *e; + + for(i=0;ii = i; + HASH_ADD_INT(elts, i, e); + } + pthread_rwlock_unlock(&lock); + } + return (void*)num_deld; +} + +int main() { + unsigned i; + long num_added=0; + int status; + pthread_t thread_r1,thread_r2,thread_w1,thread_w2; + void *thread_result; + elt tmp, *e; + + if (pthread_rwlock_init(&lock,NULL) != 0) { + fprintf(stderr,"lock init failed\n"); + exit(-1); + } + + if (( status = pthread_create( &thread_r1, NULL, thread_routine_r, NULL) )) { + printf("failure: status %d\n", status); + exit(-1); + } + if (( status = pthread_create( &thread_r2, NULL, thread_routine_r, NULL) )) { + printf("failure: status %d\n", status); + exit(-1); + } + if (( status = pthread_create( &thread_w1, NULL, thread_routine_w, NULL) )) { + printf("failure: status %d\n", status); + exit(-1); + } + if (( status = pthread_create( &thread_w2, NULL, thread_routine_w, NULL) )) { + printf("failure: status %d\n", status); + exit(-1); + } + + status = pthread_join( thread_r1, &thread_result ); + printf("thread result: %d %ld\n", status, (long)thread_result); + + status = pthread_join( thread_r2, &thread_result ); + printf("thread result: %d %ld\n", status, (long)thread_result); + + status = pthread_join( thread_w1, &thread_result ); + printf("thread result: %d %ld\n", status, (long)thread_result); + + status = pthread_join( thread_w2, &thread_result ); + printf("thread result: %d %ld\n", status, (long)thread_result); + + i = HASH_COUNT(elts); + printf("final count of items in hash: %u\n", i); + + if (pthread_rwlock_destroy(&lock) != 0) { + fprintf(stderr,"lock destroy failed\n"); + exit(-1); + } +} -- cgit v1.2.3