diff --git a/Examples/concurrency/con1 b/Examples/concurrency/con1 new file mode 100755 index 0000000..267abf7 Binary files /dev/null and b/Examples/concurrency/con1 differ diff --git a/Examples/concurrency/con1.c b/Examples/concurrency/con1.c index 643432b..f6a23dc 100644 --- a/Examples/concurrency/con1.c +++ b/Examples/concurrency/con1.c @@ -4,11 +4,11 @@ * Also does not work if a process does not want to access the critical section */ +#include #include #include #include #include -#include // shared global static int counter1 = 0; @@ -20,7 +20,7 @@ volatile int start = 0; // turn flag volatile int turn = 0; -void* thread_routine(void* args) +void *thread_routine(void *args) { int me = *((int *)args); int you = me ? 0 : 1; @@ -28,11 +28,12 @@ void* thread_routine(void* args) printf("Worker thread: %d ready, you are %d\n", me, you); // wait for start from master thread - while(!start); + while (!start) + ; - for (int j = 0; j < 1000; j++) + for (int j = 0; j < 100000; j++) { - while(turn != me) + while (turn != me) { /* do nothing */ } @@ -55,21 +56,23 @@ int main() pthread_t thr1; pthread_t thr2; - if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) { + if (pthread_create(&thr1, NULL, thread_routine, (void *)&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, (void *)&val2) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } start = 1; - pthread_join(thr1,NULL); - pthread_join(thr2,NULL); + pthread_join(thr1, NULL); + pthread_join(thr2, NULL); - printf("counter1: %d\n",counter1); - printf("counter2: %d\n",counter2); + printf("counter1: %d\n", counter1); + printf("counter2: %d\n", counter2); return EXIT_SUCCESS; } diff --git a/Examples/concurrency/con1.dSYM/Contents/Info.plist b/Examples/concurrency/con1.dSYM/Contents/Info.plist new file mode 100644 index 0000000..ef5dcb9 --- /dev/null +++ b/Examples/concurrency/con1.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.con1 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/con1.dSYM/Contents/Resources/DWARF/con1 b/Examples/concurrency/con1.dSYM/Contents/Resources/DWARF/con1 new file mode 100644 index 0000000..b688738 Binary files /dev/null and b/Examples/concurrency/con1.dSYM/Contents/Resources/DWARF/con1 differ diff --git a/Examples/concurrency/con2 b/Examples/concurrency/con2 new file mode 100755 index 0000000..2c92d7b Binary files /dev/null and b/Examples/concurrency/con2 differ diff --git a/Examples/concurrency/con2.c b/Examples/concurrency/con2.c index 00d6530..cd0de14 100644 --- a/Examples/concurrency/con2.c +++ b/Examples/concurrency/con2.c @@ -4,11 +4,11 @@ * Due to race conditions with the flag array, it cannot guarentee mutual exclusion */ +#include #include #include #include #include -#include // shared global static int counter1 = 0; @@ -18,21 +18,22 @@ static int counter2 = 0; volatile int start = 0; // flag array -volatile int flag[2] = {0,0}; +volatile int flag[2] = {0, 0}; -void* thread_routine(void* args) +void *thread_routine(void *args) { - int me = *((int *) args); + int me = *((int *)args); int you = me ? 0 : 1; - printf("Worker thread: %d ready, you are %d\n",me,you); + printf("Worker thread: %d ready, you are %d\n", me, you); // wait for start from master thread - while(!start); + while (!start) + ; for (int j = 0; j < 100000000; j++) { - while(flag[you]) + while (flag[you]) { /* do nothing */ } @@ -44,12 +45,11 @@ void* thread_routine(void* args) flag[me] = 0; } - printf("Worker thread: %d done\n",me); + printf("Worker thread: %d done\n", me); return NULL; } - int main() { int val1 = 0; @@ -57,21 +57,23 @@ int main() pthread_t thr1; pthread_t thr2; - if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) { + if (pthread_create(&thr1, NULL, thread_routine, (void *)&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, (void *)&val2) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } start = 1; - pthread_join(thr1,NULL); - pthread_join(thr2,NULL); + pthread_join(thr1, NULL); + pthread_join(thr2, NULL); - printf("counter1: %d\n",counter1); - printf("counter2: %d\n",counter2); + printf("counter1: %d\n", counter1); + printf("counter2: %d\n", counter2); return EXIT_SUCCESS; } diff --git a/Examples/concurrency/con2.dSYM/Contents/Info.plist b/Examples/concurrency/con2.dSYM/Contents/Info.plist new file mode 100644 index 0000000..0680b80 --- /dev/null +++ b/Examples/concurrency/con2.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.con2 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/con2.dSYM/Contents/Resources/DWARF/con2 b/Examples/concurrency/con2.dSYM/Contents/Resources/DWARF/con2 new file mode 100644 index 0000000..d995592 Binary files /dev/null and b/Examples/concurrency/con2.dSYM/Contents/Resources/DWARF/con2 differ diff --git a/Examples/concurrency/con3 b/Examples/concurrency/con3 new file mode 100755 index 0000000..889c4a6 Binary files /dev/null and b/Examples/concurrency/con3 differ diff --git a/Examples/concurrency/con3.c b/Examples/concurrency/con3.c index 52a1c10..b31d09c 100644 --- a/Examples/concurrency/con3.c +++ b/Examples/concurrency/con3.c @@ -6,11 +6,11 @@ * if both threads set the flag at the same time */ +#include #include #include #include #include -#include // shared global static int counter1 = 0; @@ -20,22 +20,23 @@ static int counter2 = 0; volatile int start = 0; // flag array -volatile int flag[2] = {0,0}; +volatile int flag[2] = {0, 0}; -void* thread_routine(void* args) +void *thread_routine(void *args) { - int me = *((int *) args); + int me = *((int *)args); int you = me ? 0 : 1; - printf("Worker thread: %d ready, you are %d\n",me,you); + printf("Worker thread: %d ready, you are %d\n", me, you); // wait for start from master thread - while(!start); + while (!start) + ; for (int j = 0; j < 100000000; j++) { - flag[me]=1; - while(flag[you]) + flag[me] = 1; + while (flag[you]) { /* do nothing */ } @@ -43,10 +44,10 @@ void* thread_routine(void* args) counter1++; counter2++; // leaving critical section - flag[me]=0; + flag[me] = 0; } - printf("Worker thread: %d done\n",me); + printf("Worker thread: %d done\n", me); return NULL; } @@ -58,21 +59,23 @@ int main() pthread_t thr1; pthread_t thr2; - if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) { + if (pthread_create(&thr1, NULL, thread_routine, (void *)&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, (void *)&val2) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } start = 1; - pthread_join(thr1,NULL); - pthread_join(thr2,NULL); + pthread_join(thr1, NULL); + pthread_join(thr2, NULL); - printf("counter1: %d\n",counter1); - printf("counter2: %d\n",counter2); + printf("counter1: %d\n", counter1); + printf("counter2: %d\n", counter2); return EXIT_SUCCESS; } diff --git a/Examples/concurrency/con3.dSYM/Contents/Info.plist b/Examples/concurrency/con3.dSYM/Contents/Info.plist new file mode 100644 index 0000000..b4a0069 --- /dev/null +++ b/Examples/concurrency/con3.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.con3 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/con3.dSYM/Contents/Resources/DWARF/con3 b/Examples/concurrency/con3.dSYM/Contents/Resources/DWARF/con3 new file mode 100644 index 0000000..c25bdd6 Binary files /dev/null and b/Examples/concurrency/con3.dSYM/Contents/Resources/DWARF/con3 differ diff --git a/Examples/concurrency/con4 b/Examples/concurrency/con4 new file mode 100755 index 0000000..ebe9fef Binary files /dev/null and b/Examples/concurrency/con4 differ diff --git a/Examples/concurrency/con4.c b/Examples/concurrency/con4.c index 97e955e..70c0de8 100644 --- a/Examples/concurrency/con4.c +++ b/Examples/concurrency/con4.c @@ -7,11 +7,11 @@ * Prone to livelock since threads might be constantly releasing their flag */ +#include #include #include #include #include -#include // shared global static int counter1 = 0; @@ -21,23 +21,24 @@ static int counter2 = 0; volatile int start = 0; // flag array -volatile int flag[2] = {0,0}; +volatile int flag[2] = {0, 0}; -void* thread_routine(void* args) +void *thread_routine(void *args) { - int me = *((int *) args); + int me = *((int *)args); int you = me ? 0 : 1; - printf("Worker thread: %d ready, you are %d\n",me,you); + printf("Worker thread: %d ready, you are %d\n", me, you); // wait for start from master thread - while(!start); + while (!start) + ; for (int j = 0; j < 1000000; j++) { flag[me] = 1; - while(flag[you]) + while (flag[you]) { // just in case there is deadlock flag[me] = 0; @@ -51,12 +52,11 @@ void* thread_routine(void* args) flag[me] = 0; } - printf("Worker thread: %d done\n",me); + printf("Worker thread: %d done\n", me); return NULL; } - int main() { int val1 = 0; @@ -64,21 +64,23 @@ int main() pthread_t thr1; pthread_t thr2; - if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) { + if (pthread_create(&thr1, NULL, thread_routine, (void *)&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, (void *)&val2) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } start = 1; - pthread_join(thr1,NULL); - pthread_join(thr2,NULL); + pthread_join(thr1, NULL); + pthread_join(thr2, NULL); - printf("counter1: %d\n",counter1); - printf("counter2: %d\n",counter2); + printf("counter1: %d\n", counter1); + printf("counter2: %d\n", counter2); return EXIT_SUCCESS; } diff --git a/Examples/concurrency/con4.dSYM/Contents/Info.plist b/Examples/concurrency/con4.dSYM/Contents/Info.plist new file mode 100644 index 0000000..274a7eb --- /dev/null +++ b/Examples/concurrency/con4.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.con4 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/con4.dSYM/Contents/Resources/DWARF/con4 b/Examples/concurrency/con4.dSYM/Contents/Resources/DWARF/con4 new file mode 100644 index 0000000..2a7bf2d Binary files /dev/null and b/Examples/concurrency/con4.dSYM/Contents/Resources/DWARF/con4 differ diff --git a/Examples/concurrency/con_dekkers b/Examples/concurrency/con_dekkers new file mode 100755 index 0000000..aa9435d Binary files /dev/null and b/Examples/concurrency/con_dekkers differ diff --git a/Examples/concurrency/con_dekkers.c b/Examples/concurrency/con_dekkers.c index 14ae8f3..4cd5f5f 100644 --- a/Examples/concurrency/con_dekkers.c +++ b/Examples/concurrency/con_dekkers.c @@ -7,11 +7,11 @@ * Also does not work on modern CPUs that perform out-of-order execution */ +#include #include #include #include #include -#include // shared global static int counter1 = 0; @@ -24,30 +24,32 @@ volatile int start = 0; volatile int turn = 0; // flag array -volatile int flag[2] = {0,0}; +volatile int flag[2] = {0, 0}; -void* thread_routine(void* args) +void *thread_routine(void *args) { - int me = *((int *) args); + int me = *((int *)args); int you = me ? 0 : 1; - printf("Worker thread: %d ready, you are %d\n",me,you); + printf("Worker thread: %d ready, you are %d\n", me, you); // wait for start from master thread - while(!start); + while (!start) + ; for (int j = 0; j < 1000000; j++) { flag[me] = 1; - while(flag[you]) + while (flag[you]) { - if(turn == you) + if (turn == you) { // just in case there is deadlock flag[me] = 0; } - while (turn == you); + while (turn == you) + ; flag[me] = 1; } // this is the critical section @@ -58,12 +60,11 @@ void* thread_routine(void* args) flag[me] = 0; } - printf("Worker thread: %d done\n",me); + printf("Worker thread: %d done\n", me); return NULL; } - int main() { int val1 = 0; @@ -71,21 +72,23 @@ int main() pthread_t thr1; pthread_t thr2; - if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) { + if (pthread_create(&thr1, NULL, thread_routine, (void *)&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, (void *)&val2) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } start = 1; - pthread_join(thr1,NULL); - pthread_join(thr2,NULL); + pthread_join(thr1, NULL); + pthread_join(thr2, NULL); - printf("counter1: %d\n",counter1); - printf("counter2: %d\n",counter2); + printf("counter1: %d\n", counter1); + printf("counter2: %d\n", counter2); return EXIT_SUCCESS; } diff --git a/Examples/concurrency/con_dekkers.dSYM/Contents/Info.plist b/Examples/concurrency/con_dekkers.dSYM/Contents/Info.plist new file mode 100644 index 0000000..288e358 --- /dev/null +++ b/Examples/concurrency/con_dekkers.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.con_dekkers + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/con_dekkers.dSYM/Contents/Resources/DWARF/con_dekkers b/Examples/concurrency/con_dekkers.dSYM/Contents/Resources/DWARF/con_dekkers new file mode 100644 index 0000000..f304e21 Binary files /dev/null and b/Examples/concurrency/con_dekkers.dSYM/Contents/Resources/DWARF/con_dekkers differ diff --git a/Examples/concurrency/mutex1 b/Examples/concurrency/mutex1 new file mode 100755 index 0000000..2082eaa Binary files /dev/null and b/Examples/concurrency/mutex1 differ diff --git a/Examples/concurrency/mutex1.c b/Examples/concurrency/mutex1.c index 3891398..7dd1d80 100644 --- a/Examples/concurrency/mutex1.c +++ b/Examples/concurrency/mutex1.c @@ -3,33 +3,34 @@ * that increments two global variables */ +#include #include #include #include #include -#include // shared global static int counter1 = 0; static int counter2 = 0; -// start flag +// start flag volatile int start; // flag mutex pthread_mutex_t flag = PTHREAD_MUTEX_INITIALIZER; // The thread process -void* thread_routine(void* args) +void *thread_routine(void *args) { - int me = *((int *) args); + int me = *((int *)args); int you = me ? 0 : 1; - printf("Worker thread: %d ready, you are %d\n",me,you); + printf("Worker thread: %d ready, you are %d\n", me, you); // wait for start from master thread - while(!start); + while (!start) + ; for (int j = 0; j < 1000000; j++) { @@ -41,12 +42,11 @@ void* thread_routine(void* args) pthread_mutex_unlock(&flag); } - printf("Worker thread: %d done\n",me); + printf("Worker thread: %d done\n", me); return NULL; } - // Main process int main() { @@ -55,22 +55,24 @@ int main() pthread_t thr1; pthread_t thr2; - if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) { + if (pthread_create(&thr1, NULL, thread_routine, (void *)&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, (void *)&val2) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } start = 1; - pthread_join(thr1,NULL); - pthread_join(thr2,NULL); + pthread_join(thr1, NULL); + pthread_join(thr2, NULL); - printf("counter1: %d\n",counter1); - printf("counter2: %d\n",counter2); + printf("counter1: %d\n", counter1); + printf("counter2: %d\n", counter2); pthread_mutex_destroy(&flag); return EXIT_SUCCESS; diff --git a/Examples/concurrency/mutex1.dSYM/Contents/Info.plist b/Examples/concurrency/mutex1.dSYM/Contents/Info.plist new file mode 100644 index 0000000..aa6a1ae --- /dev/null +++ b/Examples/concurrency/mutex1.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.mutex1 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/mutex1.dSYM/Contents/Resources/DWARF/mutex1 b/Examples/concurrency/mutex1.dSYM/Contents/Resources/DWARF/mutex1 new file mode 100644 index 0000000..85aaf3b Binary files /dev/null and b/Examples/concurrency/mutex1.dSYM/Contents/Resources/DWARF/mutex1 differ diff --git a/Examples/concurrency/mutex2 b/Examples/concurrency/mutex2 new file mode 100755 index 0000000..cfe1c62 Binary files /dev/null and b/Examples/concurrency/mutex2 differ diff --git a/Examples/concurrency/mutex2.c b/Examples/concurrency/mutex2.c index a784596..0361826 100644 --- a/Examples/concurrency/mutex2.c +++ b/Examples/concurrency/mutex2.c @@ -4,33 +4,34 @@ * unlocked */ +#include #include #include #include #include -#include // shared global static int counter1 = 0; static int counter2 = 0; -// start flag +// start flag volatile int start; // flag mutex pthread_mutex_t flag = PTHREAD_MUTEX_INITIALIZER; // The thread process -void* thread_routine(void* args) +void *thread_routine(void *args) { - int me = *((int *) args); + int me = *((int *)args); int you = me ? 0 : 1; - printf("Worker thread: %d ready, you are %d\n",me,you); + printf("Worker thread: %d ready, you are %d\n", me, you); // wait for start from master thread - while(!start); + while (!start) + ; for (int j = 0; j < 1000000; j++) { @@ -41,17 +42,17 @@ void* thread_routine(void* args) // leaving critical section pthread_mutex_unlock(&flag); - if(me == 0) { + if (me == 0) + { pthread_mutex_unlock(&flag); } } - printf("Worker thread: %d done\n",me); + printf("Worker thread: %d done\n", me); return NULL; } - // Main process int main() { @@ -60,22 +61,24 @@ int main() pthread_t thr1; pthread_t thr2; - if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) { + if (pthread_create(&thr1, NULL, thread_routine, (void *)&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, (void *)&val2) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } start = 1; - pthread_join(thr1,NULL); - pthread_join(thr2,NULL); + pthread_join(thr1, NULL); + pthread_join(thr2, NULL); - printf("counter1: %d\n",counter1); - printf("counter2: %d\n",counter2); + printf("counter1: %d\n", counter1); + printf("counter2: %d\n", counter2); pthread_mutex_destroy(&flag); return EXIT_SUCCESS; diff --git a/Examples/concurrency/mutex2.dSYM/Contents/Info.plist b/Examples/concurrency/mutex2.dSYM/Contents/Info.plist new file mode 100644 index 0000000..87312b4 --- /dev/null +++ b/Examples/concurrency/mutex2.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.mutex2 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/mutex2.dSYM/Contents/Resources/DWARF/mutex2 b/Examples/concurrency/mutex2.dSYM/Contents/Resources/DWARF/mutex2 new file mode 100644 index 0000000..3482b46 Binary files /dev/null and b/Examples/concurrency/mutex2.dSYM/Contents/Resources/DWARF/mutex2 differ diff --git a/Examples/concurrency/mutex3 b/Examples/concurrency/mutex3 new file mode 100755 index 0000000..f983520 Binary files /dev/null and b/Examples/concurrency/mutex3 differ diff --git a/Examples/concurrency/mutex3.c b/Examples/concurrency/mutex3.c index 7d6c2a9..ac1ca46 100644 --- a/Examples/concurrency/mutex3.c +++ b/Examples/concurrency/mutex3.c @@ -4,38 +4,40 @@ * locked */ +#include #include #include #include #include -#include // shared global static int counter1 = 0; static int counter2 = 0; -// start flag +// start flag volatile int start; // flag mutex pthread_mutex_t flag = PTHREAD_MUTEX_INITIALIZER; // The thread process -void* thread_routine(void* args) +void *thread_routine(void *args) { - int me = *((int *) args); + int me = *((int *)args); int you = me ? 0 : 1; - printf("Worker thread: %d ready, you are %d\n",me,you); + printf("Worker thread: %d ready, you are %d\n", me, you); // wait for start from master thread - while(!start); + while (!start) + ; for (int j = 0; j < 1000000; j++) { pthread_mutex_lock(&flag); - if(me == 0) { + if (me == 0) + { pthread_mutex_lock(&flag); } @@ -46,12 +48,11 @@ void* thread_routine(void* args) pthread_mutex_unlock(&flag); } - printf("Worker thread: %d done\n",me); + printf("Worker thread: %d done\n", me); return NULL; } - // Main process int main() { @@ -60,22 +61,24 @@ int main() pthread_t thr1; pthread_t thr2; - if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) { + if (pthread_create(&thr1, NULL, thread_routine, (void *)&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, (void *)&val2) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } start = 1; - pthread_join(thr1,NULL); - pthread_join(thr2,NULL); + pthread_join(thr1, NULL); + pthread_join(thr2, NULL); - printf("counter1: %d\n",counter1); - printf("counter2: %d\n",counter2); + printf("counter1: %d\n", counter1); + printf("counter2: %d\n", counter2); pthread_mutex_destroy(&flag); return EXIT_SUCCESS; diff --git a/Examples/concurrency/mutex3.dSYM/Contents/Info.plist b/Examples/concurrency/mutex3.dSYM/Contents/Info.plist new file mode 100644 index 0000000..29ad191 --- /dev/null +++ b/Examples/concurrency/mutex3.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.mutex3 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/mutex3.dSYM/Contents/Resources/DWARF/mutex3 b/Examples/concurrency/mutex3.dSYM/Contents/Resources/DWARF/mutex3 new file mode 100644 index 0000000..626d7d9 Binary files /dev/null and b/Examples/concurrency/mutex3.dSYM/Contents/Resources/DWARF/mutex3 differ diff --git a/Examples/concurrency/prod_cons1 b/Examples/concurrency/prod_cons1 new file mode 100755 index 0000000..b425879 Binary files /dev/null and b/Examples/concurrency/prod_cons1 differ diff --git a/Examples/concurrency/prod_cons1.c b/Examples/concurrency/prod_cons1.c index e24dc97..0c9f237 100644 --- a/Examples/concurrency/prod_cons1.c +++ b/Examples/concurrency/prod_cons1.c @@ -6,109 +6,117 @@ * a consumer might attempt to read when the space is empty */ -#include -#include -#include -#include -#include #include #include +#include +#include +#include +#include +#include #define BUFFER_SIZE 30 #define ELEMENTS 100 // Shared Buffer -typedef struct circular_buffer { - unsigned char values[BUFFER_SIZE]; - int out_idx; - int in_idx; +typedef struct circular_buffer +{ + unsigned char values[BUFFER_SIZE]; + int out_idx; + int in_idx; } circular_buffer; circular_buffer buffer; -void buffer_init(circular_buffer* b) +void buffer_init(circular_buffer *b) { - b->out_idx = 0; - b->in_idx = 0; - memset(b->values, 0, BUFFER_SIZE); + b->out_idx = 0; + b->in_idx = 0; + memset(b->values, 0, BUFFER_SIZE); } -void buffer_insert(circular_buffer* b, unsigned char value) +void buffer_insert(circular_buffer *b, unsigned char value) { - if(b->values[b->in_idx] != 0) { - printf("ERROR: Inserting into buffer when an element exists. Empty was expected\n"); - exit(EXIT_FAILURE); - } - b->values[b->in_idx] = value; - b->in_idx = b->in_idx < BUFFER_SIZE-1 ? b->in_idx + 1 : 0; + if (b->values[b->in_idx] != 0) + { + printf("ERROR: Inserting into buffer when an element exists. Empty was expected\n"); + exit(EXIT_FAILURE); + } + b->values[b->in_idx] = value; + b->in_idx = b->in_idx < BUFFER_SIZE - 1 ? b->in_idx + 1 : 0; } -unsigned char buffer_remove(circular_buffer* b) +unsigned char buffer_remove(circular_buffer *b) { - unsigned char return_value = b->values[b->out_idx]; - if(return_value == 0) { - printf("ERROR: Removing from a buffer at an empty element. Something was expected\n"); - exit(EXIT_FAILURE); - } - b->values[b->out_idx] = 0; - b->out_idx = b->out_idx < BUFFER_SIZE-1 ? b->out_idx + 1 : 0; - return return_value; + unsigned char return_value = b->values[b->out_idx]; + if (return_value == 0) + { + printf("ERROR: Removing from a buffer at an empty element. Something was expected\n"); + exit(EXIT_FAILURE); + } + b->values[b->out_idx] = 0; + b->out_idx = b->out_idx < BUFFER_SIZE - 1 ? b->out_idx + 1 : 0; + return return_value; } // Start flag volatile int start = 0; // Producer Routine -void* producer() +void *producer() { - // wait for start from master thread - while(!start); + // wait for start from master thread + while (!start) + ; - for(int i = 0; i < ELEMENTS; i++) { - // Add an element to the buffer - unsigned char value = (i % 100) + 1; // Make sure the value isn't zero - buffer_insert(&buffer, value); - printf("Producer added: %d\n", value); - } + for (int i = 0; i < ELEMENTS; i++) + { + // Add an element to the buffer + unsigned char value = (i % 100) + 1; // Make sure the value isn't zero + buffer_insert(&buffer, value); + printf("Producer added: %d\n", value); + } - return NULL; + return NULL; } // Consumer Routine -void* consumer() +void *consumer() { - // wait for start from master thread - while(!start); + // wait for start from master thread + while (!start) + ; - for(int i = 0; i < ELEMENTS; i++) { - // Remove an element from the buffer - printf("Consumer removed: %d\n", buffer_remove(&buffer)); - } + for (int i = 0; i < ELEMENTS; i++) + { + // Remove an element from the buffer + printf("Consumer removed: %d\n", buffer_remove(&buffer)); + } - return NULL; + return NULL; } - // Main process int main() { - buffer_init(&buffer); - - pthread_t prod; - pthread_t cons; - if(pthread_create(&prod, NULL, producer, NULL) == -1) { - printf("COULD NOT CREATE PRODUCER\n"); - exit(EXIT_FAILURE); - } - if(pthread_create(&cons, NULL, consumer, NULL) == -1) { - printf("COULD NOT CREATE CONSUMER\n"); - exit(EXIT_FAILURE); - } + buffer_init(&buffer); - start = 1; + pthread_t prod; + pthread_t cons; + if (pthread_create(&prod, NULL, producer, NULL) == -1) + { + printf("COULD NOT CREATE PRODUCER\n"); + exit(EXIT_FAILURE); + } + if (pthread_create(&cons, NULL, consumer, NULL) == -1) + { + printf("COULD NOT CREATE CONSUMER\n"); + exit(EXIT_FAILURE); + } - pthread_join(prod, NULL); - pthread_join(cons, NULL); + start = 1; - return EXIT_SUCCESS; + pthread_join(prod, NULL); + pthread_join(cons, NULL); + + return EXIT_SUCCESS; } diff --git a/Examples/concurrency/prod_cons1.dSYM/Contents/Info.plist b/Examples/concurrency/prod_cons1.dSYM/Contents/Info.plist new file mode 100644 index 0000000..146ba33 --- /dev/null +++ b/Examples/concurrency/prod_cons1.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.prod_cons1 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/prod_cons1.dSYM/Contents/Resources/DWARF/prod_cons1 b/Examples/concurrency/prod_cons1.dSYM/Contents/Resources/DWARF/prod_cons1 new file mode 100644 index 0000000..e58008c Binary files /dev/null and b/Examples/concurrency/prod_cons1.dSYM/Contents/Resources/DWARF/prod_cons1 differ diff --git a/Examples/concurrency/prod_cons2 b/Examples/concurrency/prod_cons2 new file mode 100755 index 0000000..048d1c4 Binary files /dev/null and b/Examples/concurrency/prod_cons2 differ diff --git a/Examples/concurrency/prod_cons2.c b/Examples/concurrency/prod_cons2.c index 287bcd1..90ee7b0 100644 --- a/Examples/concurrency/prod_cons2.c +++ b/Examples/concurrency/prod_cons2.c @@ -7,53 +7,56 @@ * Consusmers wait for at least one filled spot before reading */ -#include -#include -#include -#include -#include #include #include +#include +#include +#include +#include +#include #define BUFFER_SIZE 30 #define ELEMENTS 100 // Shared Buffer -typedef struct circular_buffer { - unsigned char values[BUFFER_SIZE]; - int out_idx; - int in_idx; +typedef struct circular_buffer +{ + unsigned char values[BUFFER_SIZE]; + int out_idx; + int in_idx; } circular_buffer; circular_buffer buffer; -void buffer_init(circular_buffer* b) +void buffer_init(circular_buffer *b) { - b->out_idx = 0; - b->in_idx = 0; - memset(b->values, 0, BUFFER_SIZE); + b->out_idx = 0; + b->in_idx = 0; + memset(b->values, 0, BUFFER_SIZE); } -void buffer_insert(circular_buffer* b, unsigned char value) +void buffer_insert(circular_buffer *b, unsigned char value) { - if(b->values[b->in_idx] != 0) { - printf("ERROR: Inserting into buffer when an element exists. Empty was expected\n"); - exit(EXIT_FAILURE); - } - b->values[b->in_idx] = value; - b->in_idx = b->in_idx < BUFFER_SIZE-1 ? b->in_idx + 1 : 0; + if (b->values[b->in_idx] != 0) + { + printf("ERROR: Inserting into buffer when an element exists. Empty was expected\n"); + exit(EXIT_FAILURE); + } + b->values[b->in_idx] = value; + b->in_idx = b->in_idx < BUFFER_SIZE - 1 ? b->in_idx + 1 : 0; } -unsigned char buffer_remove(circular_buffer* b) +unsigned char buffer_remove(circular_buffer *b) { - unsigned char return_value = b->values[b->out_idx]; - if(return_value == 0) { - printf("ERROR: Removing from a buffer at an empty element. Something was expected\n"); - exit(EXIT_FAILURE); - } - b->values[b->out_idx] = 0; - b->out_idx = b->out_idx < BUFFER_SIZE-1 ? b->out_idx + 1 : 0; - return return_value; + unsigned char return_value = b->values[b->out_idx]; + if (return_value == 0) + { + printf("ERROR: Removing from a buffer at an empty element. Something was expected\n"); + exit(EXIT_FAILURE); + } + b->values[b->out_idx] = 0; + b->out_idx = b->out_idx < BUFFER_SIZE - 1 ? b->out_idx + 1 : 0; + return return_value; } // Semaphores for controlling access @@ -64,73 +67,78 @@ sem_t emptyCount; volatile int start = 0; // Producer Routine -void* producer() +void *producer() { - // wait for start from master thread - while(!start); + // wait for start from master thread + while (!start) + ; - for(int i = 0; i < ELEMENTS; i++) { - // Wait for a free spot in the buffer - sem_wait(&emptyCount); + for (int i = 0; i < ELEMENTS; i++) + { + // Wait for a free spot in the buffer + sem_wait(&emptyCount); - // Add an element to the buffer - unsigned char value = (i % 100) + 1; // Make sure the value isn't zero - buffer_insert(&buffer, value); - printf("Producer added: %d\n", value); + // Add an element to the buffer + unsigned char value = (i % 100) + 1; // Make sure the value isn't zero + buffer_insert(&buffer, value); + printf("Producer added: %d\n", value); - // Signal the consumer that there something to consume - sem_post(&fullCount); - } + // Signal the consumer that there something to consume + sem_post(&fullCount); + } - return NULL; + return NULL; } // Consumer Routine -void* consumer() +void *consumer() { - // wait for start from master thread - while(!start); + // wait for start from master thread + while (!start) + ; - for(int i = 0; i < ELEMENTS; i++) { - // Wait for element to consume - sem_wait(&fullCount); + for (int i = 0; i < ELEMENTS; i++) + { + // Wait for element to consume + sem_wait(&fullCount); - // Remove an element from the buffer - printf("Consumer removed: %d\n", buffer_remove(&buffer)); + // Remove an element from the buffer + printf("Consumer removed: %d\n", buffer_remove(&buffer)); - // Signal the producer that there is a free spot - sem_post(&emptyCount); - } + // Signal the producer that there is a free spot + sem_post(&emptyCount); + } - return NULL; + return NULL; } - // Main process int main() { - sem_init(&fullCount, 0, 0); - sem_init(&emptyCount, 0, BUFFER_SIZE); - buffer_init(&buffer); - - pthread_t prod; - pthread_t cons; - if(pthread_create(&prod, NULL, producer, NULL) == -1) { - printf("COULD NOT CREATE PRODUCER\n"); - exit(EXIT_FAILURE); - } - if(pthread_create(&cons, NULL, consumer, NULL) == -1) { - printf("COULD NOT CREATE CONSUMER\n"); - exit(EXIT_FAILURE); - } + sem_init(&fullCount, 0, 0); + sem_init(&emptyCount, 0, BUFFER_SIZE); + buffer_init(&buffer); - start = 1; + pthread_t prod; + pthread_t cons; + if (pthread_create(&prod, NULL, producer, NULL) == -1) + { + printf("COULD NOT CREATE PRODUCER\n"); + exit(EXIT_FAILURE); + } + if (pthread_create(&cons, NULL, consumer, NULL) == -1) + { + printf("COULD NOT CREATE CONSUMER\n"); + exit(EXIT_FAILURE); + } - pthread_join(prod, NULL); - pthread_join(cons, NULL); + start = 1; - sem_destroy(&fullCount); - sem_destroy(&emptyCount); + pthread_join(prod, NULL); + pthread_join(cons, NULL); - return EXIT_SUCCESS; + sem_destroy(&fullCount); + sem_destroy(&emptyCount); + + return EXIT_SUCCESS; } diff --git a/Examples/concurrency/prod_cons2.dSYM/Contents/Info.plist b/Examples/concurrency/prod_cons2.dSYM/Contents/Info.plist new file mode 100644 index 0000000..c0182b8 --- /dev/null +++ b/Examples/concurrency/prod_cons2.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.prod_cons2 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/prod_cons2.dSYM/Contents/Resources/DWARF/prod_cons2 b/Examples/concurrency/prod_cons2.dSYM/Contents/Resources/DWARF/prod_cons2 new file mode 100644 index 0000000..ba6b269 Binary files /dev/null and b/Examples/concurrency/prod_cons2.dSYM/Contents/Resources/DWARF/prod_cons2 differ diff --git a/Examples/concurrency/race b/Examples/concurrency/race new file mode 100755 index 0000000..1f5b9de Binary files /dev/null and b/Examples/concurrency/race differ diff --git a/Examples/concurrency/race.c b/Examples/concurrency/race.c index 601e78d..84063f9 100644 --- a/Examples/concurrency/race.c +++ b/Examples/concurrency/race.c @@ -1,68 +1,70 @@ -/* - * race.c - Shows the race condition that can happen - * then threads try to modify multiple global - * values at the same time - */ +s /* + * race.c - Shows the race condition that can happen + * then threads try to modify multiple global + * values at the same time + */ +#include #include #include #include #include -#include -// start flag -volatile int start = 0; + // start flag + volatile int start = 0; // shared global static int counter1 = 0; static int counter2 = 0; -void* thread_routine(void* args) +void *thread_routine(void *args) { - int me = *((int *)args); - int you = me ? 0 : 1; + int me = *((int *)args); + int you = me ? 0 : 1; - printf("Worker thread: %d ready, you are %d\n", me, you); + printf("Worker thread: %d ready, you are %d\n", me, you); - // wait for start from master thread - while(!start); + // wait for start from master thread + while (!start) + ; - for (int j = 0; j < 100000000; j++) - { - // this is the critical section - counter1++; - counter2++; - // leaving critical section - } + for (int j = 0; j < 100000000; j++) + { + // this is the critical section + counter1++; + counter2++; + // leaving critical section + } - printf("Worker thread: %d done\n",me); + printf("Worker thread: %d done\n", me); - return NULL; + return NULL; } int main() { - int val1 = 0; - int val2 = 1; - - pthread_t thr1; - pthread_t thr2; - if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) { - printf("COULD NOT CREATE A THREAD\n"); - exit(EXIT_FAILURE); - } - if(pthread_create(&thr2, NULL, thread_routine, (void*)&val2) == -1) { - printf("COULD NOT CREATE A THREAD\n"); - exit(EXIT_FAILURE); - } + int val1 = 0; + int val2 = 1; - start = 1; + pthread_t thr1; + pthread_t thr2; + if (pthread_create(&thr1, NULL, thread_routine, (void *)&val1) == -1) + { + printf("COULD NOT CREATE A THREAD\n"); + exit(EXIT_FAILURE); + } + if (pthread_create(&thr2, NULL, thread_routine, (void *)&val2) == -1) + { + printf("COULD NOT CREATE A THREAD\n"); + exit(EXIT_FAILURE); + } - pthread_join(thr1,NULL); - pthread_join(thr2,NULL); + start = 1; - printf("counter1: %d\n",counter1); - printf("counter2: %d\n",counter2); - return EXIT_SUCCESS; + pthread_join(thr1, NULL); + pthread_join(thr2, NULL); + + printf("counter1: %d\n", counter1); + printf("counter2: %d\n", counter2); + return EXIT_SUCCESS; } - diff --git a/Examples/concurrency/race.dSYM/Contents/Info.plist b/Examples/concurrency/race.dSYM/Contents/Info.plist new file mode 100644 index 0000000..7221399 --- /dev/null +++ b/Examples/concurrency/race.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.race + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/race.dSYM/Contents/Resources/DWARF/race b/Examples/concurrency/race.dSYM/Contents/Resources/DWARF/race new file mode 100644 index 0000000..3cafe76 Binary files /dev/null and b/Examples/concurrency/race.dSYM/Contents/Resources/DWARF/race differ diff --git a/Examples/concurrency/sem1 b/Examples/concurrency/sem1 new file mode 100755 index 0000000..da09824 Binary files /dev/null and b/Examples/concurrency/sem1 differ diff --git a/Examples/concurrency/sem1.c b/Examples/concurrency/sem1.c index 297fa70..efd728d 100644 --- a/Examples/concurrency/sem1.c +++ b/Examples/concurrency/sem1.c @@ -3,34 +3,35 @@ * critical section */ +#include +#include #include #include #include #include -#include -#include // shared global static int counter1 = 0; static int counter2 = 0; -// start flag +// start flag volatile int start; // flag semaphore sem_t flag; // The thread process -void* thread_routine(void* args) +void *thread_routine(void *args) { - int me = *((int *) args); + int me = *((int *)args); int you = me ? 0 : 1; - printf("Worker thread: %d ready, you are %d\n",me,you); + printf("Worker thread: %d ready, you are %d\n", me, you); // wait for start from master thread - while(!start); + while (!start) + ; for (int j = 0; j < 1000000; j++) { @@ -42,39 +43,40 @@ void* thread_routine(void* args) sem_post(&flag); } - printf("Worker thread: %d done\n",me); + printf("Worker thread: %d done\n", me); return NULL; } - // Main process int main() { int val1 = 0; int val2 = 1; - + // Initialize the semaphore - initial value of 1 sem_init(&flag, 0, 1); pthread_t thr1; pthread_t thr2; - if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) { + if (pthread_create(&thr1, NULL, thread_routine, (void *)&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, (void *)&val2) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } start = 1; - pthread_join(thr1,NULL); - pthread_join(thr2,NULL); + pthread_join(thr1, NULL); + pthread_join(thr2, NULL); - printf("counter1: %d\n",counter1); - printf("counter2: %d\n",counter2); + printf("counter1: %d\n", counter1); + printf("counter2: %d\n", counter2); // Destroy the semaphore sem_destroy(&flag); diff --git a/Examples/concurrency/sem1.dSYM/Contents/Info.plist b/Examples/concurrency/sem1.dSYM/Contents/Info.plist new file mode 100644 index 0000000..30a46b1 --- /dev/null +++ b/Examples/concurrency/sem1.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.sem1 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/sem1.dSYM/Contents/Resources/DWARF/sem1 b/Examples/concurrency/sem1.dSYM/Contents/Resources/DWARF/sem1 new file mode 100644 index 0000000..df14295 Binary files /dev/null and b/Examples/concurrency/sem1.dSYM/Contents/Resources/DWARF/sem1 differ diff --git a/Examples/concurrency/sem2 b/Examples/concurrency/sem2 new file mode 100755 index 0000000..bdc6026 Binary files /dev/null and b/Examples/concurrency/sem2 differ diff --git a/Examples/concurrency/sem2.c b/Examples/concurrency/sem2.c index 7c284ba..9e0def7 100644 --- a/Examples/concurrency/sem2.c +++ b/Examples/concurrency/sem2.c @@ -4,34 +4,35 @@ * the count regardless of the current value */ +#include +#include #include #include #include #include -#include -#include // shared global static int counter1 = 0; static int counter2 = 0; -// start flag +// start flag volatile int start; // flag semaphore sem_t flag; // The thread process -void* thread_routine(void* args) +void *thread_routine(void *args) { - int me = *((int *) args); + int me = *((int *)args); int you = me ? 0 : 1; - printf("Worker thread: %d ready, you are %d\n",me,you); + printf("Worker thread: %d ready, you are %d\n", me, you); // wait for start from master thread - while(!start); + while (!start) + ; for (int j = 0; j < 100000000; j++) { @@ -42,17 +43,17 @@ void* thread_routine(void* args) // leaving critical section sem_post(&flag); - if(me == 0) { + if (me == 0) + { sem_post(&flag); } } - printf("Worker thread: %d done\n",me); + printf("Worker thread: %d done\n", me); return NULL; } - // Main process int main() { @@ -63,22 +64,24 @@ int main() pthread_t thr1; pthread_t thr2; - if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) { + if (pthread_create(&thr1, NULL, thread_routine, (void *)&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, (void *)&val2) == -1) + { printf("COULD NOT CREATE A THREAD\n"); exit(EXIT_FAILURE); } start = 1; - pthread_join(thr1,NULL); - pthread_join(thr2,NULL); + pthread_join(thr1, NULL); + pthread_join(thr2, NULL); - printf("counter1: %d\n",counter1); - printf("counter2: %d\n",counter2); + printf("counter1: %d\n", counter1); + printf("counter2: %d\n", counter2); sem_destroy(&flag); return EXIT_SUCCESS; diff --git a/Examples/concurrency/sem2.dSYM/Contents/Info.plist b/Examples/concurrency/sem2.dSYM/Contents/Info.plist new file mode 100644 index 0000000..8fbeb0b --- /dev/null +++ b/Examples/concurrency/sem2.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.sem2 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/sem2.dSYM/Contents/Resources/DWARF/sem2 b/Examples/concurrency/sem2.dSYM/Contents/Resources/DWARF/sem2 new file mode 100644 index 0000000..8c02e5b Binary files /dev/null and b/Examples/concurrency/sem2.dSYM/Contents/Resources/DWARF/sem2 differ diff --git a/Examples/concurrency/sem_list1 b/Examples/concurrency/sem_list1 new file mode 100755 index 0000000..9907e03 Binary files /dev/null and b/Examples/concurrency/sem_list1 differ diff --git a/Examples/concurrency/sem_list1.c b/Examples/concurrency/sem_list1.c index fe8f6d9..6bdaa7e 100644 --- a/Examples/concurrency/sem_list1.c +++ b/Examples/concurrency/sem_list1.c @@ -3,20 +3,22 @@ * uncontrolled access to a singly linked list */ +#include +#include #include #include #include #include -#include -#include -typedef struct node { - int val; - struct node* next; +typedef struct node +{ + int val; + struct node *next; } node; -typedef struct list { - node* head; +typedef struct list +{ + node *head; } list; // shared global @@ -26,65 +28,71 @@ static list mylist; volatile int start = 0; // The thread process -void* thread_routine() +void *thread_routine() { - printf("Worker thread: %lu ready\n", pthread_self()); - - // wait for start from master thread - while(start == 0); + printf("Worker thread: %lu ready\n", pthread_self()); - for (int j = 0; j < 1000000; j++) - { - node* new_node = malloc(sizeof(node)); - new_node->val = j; - new_node->next = mylist.head; - mylist.head = new_node; - } + // wait for start from master thread + while (start == 0) + ; - printf("Worker thread: %lu done\n", pthread_self()); + for (int j = 0; j < 1000000; j++) + { + node *new_node = malloc(sizeof(node)); + new_node->val = j; + new_node->next = mylist.head; + mylist.head = new_node; + } - return NULL; + printf("Worker thread: %lu done\n", pthread_self()); + + return NULL; } // Main process int main() { - #define THREAD_COUNT 10 - pthread_t thr_ids[THREAD_COUNT]; +#define THREAD_COUNT 10 + pthread_t thr_ids[THREAD_COUNT]; + + mylist.head = NULL; - mylist.head = NULL; - // Create the threads - for(int i = 0; i < THREAD_COUNT; i++) { - if(pthread_create(&thr_ids[i], NULL, thread_routine, NULL) == -1) { - printf("COULD NOT CREATE A THREAD\n"); - exit(EXIT_FAILURE); - } - } + for (int i = 0; i < THREAD_COUNT; i++) + { + if (pthread_create(&thr_ids[i], NULL, thread_routine, NULL) == -1) + { + printf("COULD NOT CREATE A THREAD\n"); + exit(EXIT_FAILURE); + } + } // Signal threads to start - start = 1; + start = 1; // Wait for all threads to finish - for(int i = 0; i < THREAD_COUNT; i++) { - pthread_join(thr_ids[i],NULL); - } + for (int i = 0; i < THREAD_COUNT; i++) + { + pthread_join(thr_ids[i], NULL); + } // Count the elements in the list - int length = 0; - node* itr = mylist.head; - while(itr != NULL) { - length++; - itr = itr->next; - } - printf("List contains %d elements\n", length); + int length = 0; + node *itr = mylist.head; + while (itr != NULL) + { + length++; + itr = itr->next; + } + printf("List contains %d elements\n", length); // Free the list - while(mylist.head != NULL) { - node* to_delete = mylist.head; + while (mylist.head != NULL) + { + node *to_delete = mylist.head; mylist.head = mylist.head->next; free(to_delete); - } + } - return EXIT_SUCCESS; + return EXIT_SUCCESS; } diff --git a/Examples/concurrency/sem_list1.dSYM/Contents/Info.plist b/Examples/concurrency/sem_list1.dSYM/Contents/Info.plist new file mode 100644 index 0000000..12b931b --- /dev/null +++ b/Examples/concurrency/sem_list1.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.sem_list1 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/sem_list1.dSYM/Contents/Resources/DWARF/sem_list1 b/Examples/concurrency/sem_list1.dSYM/Contents/Resources/DWARF/sem_list1 new file mode 100644 index 0000000..1fafc19 Binary files /dev/null and b/Examples/concurrency/sem_list1.dSYM/Contents/Resources/DWARF/sem_list1 differ diff --git a/Examples/concurrency/sem_list2 b/Examples/concurrency/sem_list2 new file mode 100755 index 0000000..aad093d Binary files /dev/null and b/Examples/concurrency/sem_list2 differ diff --git a/Examples/concurrency/sem_list2.c b/Examples/concurrency/sem_list2.c index cadbb59..51c66f5 100644 --- a/Examples/concurrency/sem_list2.c +++ b/Examples/concurrency/sem_list2.c @@ -1,22 +1,24 @@ -/* - * sem_list2.c - Uses a semaphore as a way to control access - * to a critical section for a singly linked list - */ +s /* + * sem_list2.c - Uses a semaphore as a way to control access + * to a critical section for a singly linked list + */ +#include +#include #include #include #include #include -#include -#include -typedef struct node { - int val; - struct node* next; + typedef struct node +{ + int val; + struct node *next; } node; -typedef struct list { - node* head; +typedef struct list +{ + node *head; } list; // shared global @@ -29,71 +31,77 @@ volatile int start = 0; sem_t flag; // The thread process -void* thread_routine() +void *thread_routine() { - printf("Worker thread: %lu ready\n", pthread_self()); - - // wait for start from master thread - while(start == 0); + printf("Worker thread: %lu ready\n", pthread_self()); - for (int j = 0; j < 1000000; j++) - { - sem_wait(&flag); - node* new_node = malloc(sizeof(node)); - new_node->val = j; - new_node->next = mylist.head; - mylist.head = new_node; - sem_post(&flag); - } + // wait for start from master thread + while (start == 0) + ; - printf("Worker thread: %lu done\n", pthread_self()); + for (int j = 0; j < 1000000; j++) + { + sem_wait(&flag); + node *new_node = malloc(sizeof(node)); + new_node->val = j; + new_node->next = mylist.head; + mylist.head = new_node; + sem_post(&flag); + } - return NULL; + printf("Worker thread: %lu done\n", pthread_self()); + + return NULL; } // Main process int main() { - sem_init(&flag, 0, 1); + sem_init(&flag, 0, 1); - #define THREAD_COUNT 10 - pthread_t thr_ids[THREAD_COUNT]; +#define THREAD_COUNT 10 + pthread_t thr_ids[THREAD_COUNT]; + + mylist.head = NULL; - mylist.head = NULL; - // Create the threads - for(int i = 0; i < THREAD_COUNT; i++) { - if(pthread_create(&thr_ids[i], NULL, thread_routine, NULL) == -1) { - printf("COULD NOT CREATE A THREAD\n"); - exit(EXIT_FAILURE); - } - } + for (int i = 0; i < THREAD_COUNT; i++) + { + if (pthread_create(&thr_ids[i], NULL, thread_routine, NULL) == -1) + { + printf("COULD NOT CREATE A THREAD\n"); + exit(EXIT_FAILURE); + } + } // Signal threads to start - start = 1; + start = 1; // Wait for all threads to finish - for(int i = 0; i < THREAD_COUNT; i++) { - pthread_join(thr_ids[i],NULL); - } + for (int i = 0; i < THREAD_COUNT; i++) + { + pthread_join(thr_ids[i], NULL); + } // Count the elements in the list - int length = 0; - node* itr = mylist.head; - while(itr != NULL) { - length++; - itr = itr->next; - } - printf("List contains %d elements\n", length); + int length = 0; + node *itr = mylist.head; + while (itr != NULL) + { + length++; + itr = itr->next; + } + printf("List contains %d elements\n", length); // Free the list - while(mylist.head != NULL) { - node* to_delete = mylist.head; + while (mylist.head != NULL) + { + node *to_delete = mylist.head; mylist.head = mylist.head->next; free(to_delete); - } - + } + sem_destroy(&flag); - return EXIT_SUCCESS; + return EXIT_SUCCESS; } diff --git a/Examples/concurrency/sem_list2.dSYM/Contents/Info.plist b/Examples/concurrency/sem_list2.dSYM/Contents/Info.plist new file mode 100644 index 0000000..545b887 --- /dev/null +++ b/Examples/concurrency/sem_list2.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.sem_list2 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/Examples/concurrency/sem_list2.dSYM/Contents/Resources/DWARF/sem_list2 b/Examples/concurrency/sem_list2.dSYM/Contents/Resources/DWARF/sem_list2 new file mode 100644 index 0000000..9cb3041 Binary files /dev/null and b/Examples/concurrency/sem_list2.dSYM/Contents/Resources/DWARF/sem_list2 differ