Skip to content

Instantly share code, notes, and snippets.

@chiehmin
Last active December 20, 2015 03:09
Show Gist options
  • Save chiehmin/6061800 to your computer and use it in GitHub Desktop.
Save chiehmin/6061800 to your computer and use it in GitHub Desktop.
static void* doit1(void *arg) {
time_t startTime, endTime;
while(true) {
pthread_mutex_lock(&t1_mutex);
while(t1 == 0)
pthread_cond_wait(&t1_cond, &t1_mutex);
startTime = timeInUs();
getBestSwapTarget(bin_index1, cell_index1, curr_swap_target1, curr_target_profit1);
endTime = timeInUs();
t_doit1 += endTime - startTime;
pthread_mutex_unlock(&t1_mutex);
pthread_mutex_lock(&t1_mutex);
t1 = 0;
pthread_cond_signal(&t1_cond);
pthread_mutex_unlock(&t1_mutex);
}
}
static void* doit2(void *arg) {
time_t startTime, endTime;
while(true) {
pthread_mutex_lock(&t2_mutex);
while(t2 == 0)
pthread_cond_wait(&t2_cond, &t2_mutex);
startTime = timeInUs();
getBestSwapTarget(bin_index2, cell_index2, curr_swap_target2, curr_target_profit2);
endTime = timeInUs();
t_doit2 += endTime - startTime;
pthread_mutex_unlock(&t2_mutex);
pthread_mutex_lock(&t2_mutex);
t2 = 0;
pthread_cond_signal(&t2_cond);
pthread_mutex_unlock(&t2_mutex);
}
}
// in the main thread
bin_index1 = bin_collection[i];
cell_index1 = cell_index;
bin_index2 = bin_collection[i + 1];
cell_index2 = cell_index;
pthread_mutex_lock(&t1_mutex);
t1 = 1;
pthread_cond_signal(&t1_cond);
pthread_mutex_unlock(&t1_mutex);
pthread_mutex_lock(&t2_mutex);
t2 = 1;
pthread_cond_signal(&t2_cond);
pthread_mutex_unlock(&t2_mutex);
pthread_mutex_lock(&t1_mutex);
while(t1 == 1)
pthread_cond_wait(&t1_cond, &t1_mutex);
pthread_mutex_unlock(&t1_mutex);
pthread_mutex_lock(&t2_mutex);
while(t2 == 1)
pthread_cond_wait(&t2_cond, &t2_mutex);
pthread_mutex_unlock(&t2_mutex);
if(curr_target_profit1 > curr_target_profit2) {
if (curr_target_profit1 > best_target_profit){
best_target_profit = curr_target_profit1;
best_swap_target = curr_swap_target1;
}
}
else {
if (curr_target_profit2 > best_target_profit){
best_target_profit = curr_target_profit2;
best_swap_target = curr_swap_target2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment