Last active
October 20, 2020 23:23
-
-
Save dulimarta/d2965b41adeda282b2c621d5b3ea4fca to your computer and use it in GitHub Desktop.
CSS452 Lab06 - Sample 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
void* swapper(void*); | |
int arr[2]; | |
int main(int argc, char* argv[]) { | |
pthread_t who; | |
long int loop; | |
// TODO: get value of loop var (from command line arg) | |
arr[0] = 0; | |
arr[1] = 1; | |
pthread_create(&who, NULL, swapper, &loop); | |
for (int k = 0; k < loop; k++) { | |
// TODO: swap the contents of arr[0] and arr[1] | |
} | |
int rc; | |
pthread_join(who, (void **) &rc); | |
printf ("Values: %5d %5d\n", arr[0], arr[1]); | |
} | |
void* swapper(void *arg) { | |
long int *num = (long int *) arg; | |
for (int k = 0; k < *num; k++) { | |
// TODO: swap the contents of arr[0] and arr[1] | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <thread> | |
void swapper(long int); | |
int arr[2]; | |
int main(int argc, char* argv[]) { | |
long int loop; | |
// TODO: get value of loop var (from command line arg) | |
arr[0] = 0; | |
arr[1] = 1; | |
std::thread t1(swapper, loop); | |
for (int k = 0; k < loop; k++) { | |
// TODO: swap the contents of arr[0] and arr[1] | |
} | |
t1.join(); | |
std::cout << "Values: " << std::setw(5) << arr[0] | |
<< std::setw(5) << arr[1] << std::endl; | |
} | |
void swapper(long int num) { | |
for (int k = 0; k < num; k++) { | |
// TODO: swap the contents of arr[0] and arr[1] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment