Skip to content

Instantly share code, notes, and snippets.

@IshJ
Last active September 9, 2016 12:15
Show Gist options
  • Save IshJ/f6105735b15304cf3d6bffd7c2b4ac8c to your computer and use it in GitHub Desktop.
Save IshJ/f6105735b15304cf3d6bffd7c2b4ac8c to your computer and use it in GitHub Desktop.
void* Thread_Operation() {
while (totalCount < m) {
// Variable to randomly generate values for operations
int random_value = rand() % 65535;
// Variable to randomly select one of the three operations
int random_select = rand() % 3;
// Member operation
if (random_select == 0) {
pthread_mutex_lock(&member_mutex);
if (finished_member == 0) {
if (memberCount < m_member) {
memberCount++;
pthread_mutex_unlock(&member_mutex);
pthread_mutex_lock(&total_mutex);
totalCount++;
pthread_mutex_unlock(&total_mutex);
pthread_rwlock_rdlock(&rwlock);
Member(random_value, head);
pthread_rwlock_unlock(&rwlock);
} else {
finished_member = 1;
pthread_mutex_unlock(&member_mutex);
}
} else {
pthread_mutex_unlock(&member_mutex);
}
}
// Insert operation
else if (random_select == 1) {
pthread_mutex_lock(&insert_mutex);
if (finished_insert == 0) {
if (insertCount < m_insert) {
insertCount++;
pthread_mutex_unlock(&insert_mutex);
pthread_mutex_lock(&total_mutex);
totalCount++;
pthread_mutex_unlock(&total_mutex);
pthread_rwlock_wrlock(&rwlock);
Insert(random_value, &head);
pthread_rwlock_unlock(&rwlock);
} else {
finished_insert = 1;
pthread_mutex_unlock(&insert_mutex);
}
} else {
pthread_mutex_unlock(&insert_mutex);
}
}
// Delete operation
else if (random_select == 2) {
pthread_mutex_lock(&delete_mutex);
if (finished_delete == 0) {
if (deleteCount < m_delete) {
deleteCount++;
pthread_mutex_unlock(&delete_mutex);
pthread_mutex_lock(&total_mutex);
totalCount++;
pthread_mutex_unlock(&total_mutex);
pthread_rwlock_wrlock(&rwlock);
Delete(random_value, &head);
pthread_rwlock_unlock(&rwlock);
} else {
finished_delete = 1;
pthread_mutex_unlock(&delete_mutex);
}
} else {
pthread_mutex_unlock(&delete_mutex);
}
}
}
return NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment