sttuff
This commit is contained in:
@@ -11,8 +11,10 @@
|
||||
// Returns 1 if prime, 0 if not prime
|
||||
int is_prime(int v)
|
||||
{
|
||||
for(int i = 2; i < v; i++) {
|
||||
if(v % i == 0) {
|
||||
for (int i = 2; i < v; i++)
|
||||
{
|
||||
if (v % i == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -23,12 +25,15 @@ int is_prime(int v)
|
||||
// calls is_prime to check if the input argument is prime
|
||||
//
|
||||
// NOTE: that parameters and return value are pointers
|
||||
void* thread_routine(void* args)
|
||||
void *thread_routine(void *args)
|
||||
{
|
||||
int* val = (int*)args;
|
||||
if(is_prime(*val)) {
|
||||
int *val = args;
|
||||
if (is_prime(*val))
|
||||
{
|
||||
printf("THREAD %lu FOUND that %d is prime\n", pthread_self(), *val);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("THREAD %lu FOUND that %d is not prime\n", pthread_self(), *val);
|
||||
}
|
||||
return NULL;
|
||||
@@ -42,11 +47,13 @@ int main()
|
||||
// Create 2 threads, passing a different value to each
|
||||
pthread_t thr1;
|
||||
pthread_t thr2;
|
||||
if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) {
|
||||
if (pthread_create(&thr1, NULL, thread_routine, &val1) == -1)
|
||||
{
|
||||
printf("COULD NOT CREATE A THREAD\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if(pthread_create(&thr2, NULL, thread_routine, (void*)&val2) == -1) {
|
||||
if (pthread_create(&thr2, NULL, thread_routine, &val2) == -1)
|
||||
{
|
||||
printf("COULD NOT CREATE A THREAD\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user