-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 72d5b8d
Showing
57 changed files
with
7,540 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <pthread.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
int glob = 0; | ||
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; | ||
|
||
void *thread_func(void *arg) { | ||
int loops = *((int *)arg); | ||
int loc; | ||
|
||
for (int i = 0; i < loops; i++) { | ||
pthread_mutex_lock(&mtx); | ||
loc = glob; | ||
loc++; | ||
glob = loc; | ||
pthread_mutex_unlock(&mtx); | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
int main() { | ||
printf("******* Threading Example *******\n"); | ||
pthread_t thread1; | ||
pthread_t thread2; | ||
int loops = 10000000; | ||
|
||
if (pthread_create(&thread1, NULL, thread_func, &loops)) { | ||
printf("An error occurred while creating thread.\n"); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
if (pthread_create(&thread2, NULL, thread_func, &loops)) { | ||
printf("An error occurred while creating thread.\n"); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
pthread_join(thread1, NULL); | ||
pthread_join(thread2, NULL); | ||
|
||
printf("glob = %d\n", glob); | ||
|
||
return EXIT_SUCCESS; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <pthread.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
void *do_work(void *arg); | ||
|
||
int main() { | ||
printf("******* Threading Example *******\n"); | ||
pthread_t thread; | ||
int i = 0; | ||
|
||
if (pthread_create(&thread, NULL, do_work, NULL)) { | ||
printf("An error occurred while creating thread.\n"); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
printf("Parent: Hi!\n"); | ||
for (int i = 0; i < 10; i++) { | ||
printf("Parent: Doing work... (%d)\n", i); | ||
sleep(1); | ||
} | ||
|
||
pthread_exit(NULL); | ||
return EXIT_SUCCESS; | ||
} | ||
|
||
void *do_work(void *arg) { | ||
printf("Child: Hi!\n"); | ||
|
||
for (int i = 0; i < 10; i++) { | ||
printf("Child: Doing work... (%d)\n", i); | ||
sleep(1); | ||
} | ||
pthread_exit(NULL); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.