Download presentation
Presentation is loading. Please wait.
Published bySilvester Watson Modified over 9 years ago
1
P4: Multithreaded Programming Zhenxiao Luo CS537 Spring 2010
2
Outline Motivation Thread Basics Thread Operations Thread Synchronizations Pthread: counter, HashTable SpinLock: SpinLock, SpinCounter, SpinList, SpinHash
3
Setting Environment Variable setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:. As done in P3 Or, you may have: – error while loading shared libraries: libcounter.so: cannot open shared object file: No such file or directory
4
Motivation: Why Use Threads? To Execute Software Faster Improve performance through parallel or distributed computing Processes are too expensive Threads are effective on both multiprocessor systems and uniprocessor systems
5
Thread Basics All threads within a process share the same address space Threads in the same process share: –Process instructions –open files (descriptors) –current working directory Each thread has a unique: –Thread ID –set of registers, stack pointer –priority
6
Thread Operations Thread Creation int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg); Thread Termination void pthread_exit(void *retval);
7
Thread Synchronization mutexes - Mutual exclusion lock joins - Make a thread wait till others are complete condition variables – wait() and signal()
8
Pthread Counter pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; static int global_counter = 0; void Counter_Init(){ – global_counter = 0; } void Counter_Increment(){ –pthread_mutex_lock( &mutex1 ); –global_counter++; –pthread_mutex_unlock( &mutex1 ); –}
9
Pthread HashTable struct hash_element{ int number; struct hash_element *next; }; struct hash_bucket{ int elemCount; struct hash_element *head; }; static struct hash_bucket *hash_buckets; static int g_numOfBuckets; pthread_mutex_t *mutexArray;
10
Hash Table - Init void Hash_Init(int buckets){ int i = 0; hash_buckets = malloc(buckets * sizeof(struct hash_bucket)); g_numOfBuckets = buckets; mutexArray = (pthread_mutex_t*)malloc(buckets * sizeof(pthread_mutex_t)); for (i = 0; i < buckets; i++) { hash_buckets[i].head = NULL; hash_buckets[i].elemCount = 0; pthread_mutex_init(&(mutexArray[i]), NULL); }
11
Hash Table - Insert Get BucketNumber: –int bucketNum = x % g_numOfBuckets; Traverse the list of elements: –If x is found, return -1 –If x is not found, create new element, insert into the end of the list –Remember to allocate memory for new element –Remember to add mutex lock when inserting to the list
12
Hash Table - Remove Get BucketNumber: –int bucketNum = x % g_numOfBuckets; Traverse the list of elements: –If x is not found, return -1 –If x is found, remove it from the list, update pointers –Remember to add mutex lock when removing element from the list
13
SpinLock TestAndSet: –xchg(volatile unsigned int *addr, unsigned int newval) –Typedef struct spinlock_t { –int flag; –} spinlock_t; –void spinlock_acquire(spinlock_t *lock) { –while (xchg(&lock->flag, 1) == 1); –// spin wait}
14
SpinLock Cond void spinlock_release(spinlock_t *lock) { lock->flag = 0; } void spinlock_init(spinlock_t *lock) { lock->flag = 0; }
15
SpinCounter –typedef struct __counter_t { –int counter; –spinlock_t lock; –} counter_t; –void Counter_Increment(counter_t *c) –{ – spinlock_acquire(&c->lock); – c->counter++; – spinlock_release(&c->lock); –}
16
SpinList typedef struct __listnode_t { unsigned int key; void *value; struct __listnode_t *next; } node; typedef struct __list_t { node *head; spinlock_t lock; } list_t;
17
SpinList Cond void List_Insert(list_t *l, void *element, unsigned int key) { node* tmp = malloc(sizeof(node)); tmp->key = key; tmp->value = element; spinlock_acquire(&l->lock); tmp->next = l->head; l->head = tmp; spinlock_release(&l->lock); }
18
SpinHash typedef struct __hash_t { int numbuckets; // # of buckets list_t *hash_buckets; } hash_t; void Hash_Insert(hash_t *h, void *element, unsigned int key) { // compute hash; int index = key % h->numbuckets; List_Insert(&h->hash_buckets[index], element, key); } void Hash_Remove(hash_t *h, unsigned int key) { // compute hash; int index = key % h->numbuckets; List_Remove(&h->hash_buckets[index], key); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.