sstuff
This commit is contained in:
BIN
Examples/concurrency/con1
Executable file
BIN
Examples/concurrency/con1
Executable file
Binary file not shown.
@@ -4,11 +4,11 @@
|
|||||||
* Also does not work if a process does not want to access the critical section
|
* Also does not work if a process does not want to access the critical section
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
// shared global
|
// shared global
|
||||||
static int counter1 = 0;
|
static int counter1 = 0;
|
||||||
@@ -20,7 +20,7 @@ volatile int start = 0;
|
|||||||
// turn flag
|
// turn flag
|
||||||
volatile int turn = 0;
|
volatile int turn = 0;
|
||||||
|
|
||||||
void* thread_routine(void* args)
|
void *thread_routine(void *args)
|
||||||
{
|
{
|
||||||
int me = *((int *)args);
|
int me = *((int *)args);
|
||||||
int you = me ? 0 : 1;
|
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);
|
printf("Worker thread: %d ready, you are %d\n", me, you);
|
||||||
|
|
||||||
// wait for start from master thread
|
// 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 */
|
/* do nothing */
|
||||||
}
|
}
|
||||||
@@ -55,21 +56,23 @@ int main()
|
|||||||
|
|
||||||
pthread_t thr1;
|
pthread_t thr1;
|
||||||
pthread_t thr2;
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
start = 1;
|
start = 1;
|
||||||
|
|
||||||
pthread_join(thr1,NULL);
|
pthread_join(thr1, NULL);
|
||||||
pthread_join(thr2,NULL);
|
pthread_join(thr2, NULL);
|
||||||
|
|
||||||
printf("counter1: %d\n",counter1);
|
printf("counter1: %d\n", counter1);
|
||||||
printf("counter2: %d\n",counter2);
|
printf("counter2: %d\n", counter2);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
20
Examples/concurrency/con1.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/con1.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.con1</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
BIN
Examples/concurrency/con1.dSYM/Contents/Resources/DWARF/con1
Normal file
BIN
Examples/concurrency/con1.dSYM/Contents/Resources/DWARF/con1
Normal file
Binary file not shown.
BIN
Examples/concurrency/con2
Executable file
BIN
Examples/concurrency/con2
Executable file
Binary file not shown.
@@ -4,11 +4,11 @@
|
|||||||
* Due to race conditions with the flag array, it cannot guarentee mutual exclusion
|
* Due to race conditions with the flag array, it cannot guarentee mutual exclusion
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
// shared global
|
// shared global
|
||||||
static int counter1 = 0;
|
static int counter1 = 0;
|
||||||
@@ -18,21 +18,22 @@ static int counter2 = 0;
|
|||||||
volatile int start = 0;
|
volatile int start = 0;
|
||||||
|
|
||||||
// flag array
|
// 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;
|
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
|
// wait for start from master thread
|
||||||
while(!start);
|
while (!start)
|
||||||
|
;
|
||||||
|
|
||||||
for (int j = 0; j < 100000000; j++)
|
for (int j = 0; j < 100000000; j++)
|
||||||
{
|
{
|
||||||
while(flag[you])
|
while (flag[you])
|
||||||
{
|
{
|
||||||
/* do nothing */
|
/* do nothing */
|
||||||
}
|
}
|
||||||
@@ -44,12 +45,11 @@ void* thread_routine(void* args)
|
|||||||
flag[me] = 0;
|
flag[me] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Worker thread: %d done\n",me);
|
printf("Worker thread: %d done\n", me);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int val1 = 0;
|
int val1 = 0;
|
||||||
@@ -57,21 +57,23 @@ int main()
|
|||||||
|
|
||||||
pthread_t thr1;
|
pthread_t thr1;
|
||||||
pthread_t thr2;
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
start = 1;
|
start = 1;
|
||||||
|
|
||||||
pthread_join(thr1,NULL);
|
pthread_join(thr1, NULL);
|
||||||
pthread_join(thr2,NULL);
|
pthread_join(thr2, NULL);
|
||||||
|
|
||||||
printf("counter1: %d\n",counter1);
|
printf("counter1: %d\n", counter1);
|
||||||
printf("counter2: %d\n",counter2);
|
printf("counter2: %d\n", counter2);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
20
Examples/concurrency/con2.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/con2.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.con2</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
BIN
Examples/concurrency/con2.dSYM/Contents/Resources/DWARF/con2
Normal file
BIN
Examples/concurrency/con2.dSYM/Contents/Resources/DWARF/con2
Normal file
Binary file not shown.
BIN
Examples/concurrency/con3
Executable file
BIN
Examples/concurrency/con3
Executable file
Binary file not shown.
@@ -6,11 +6,11 @@
|
|||||||
* if both threads set the flag at the same time
|
* if both threads set the flag at the same time
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
// shared global
|
// shared global
|
||||||
static int counter1 = 0;
|
static int counter1 = 0;
|
||||||
@@ -20,22 +20,23 @@ static int counter2 = 0;
|
|||||||
volatile int start = 0;
|
volatile int start = 0;
|
||||||
|
|
||||||
// flag array
|
// 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;
|
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
|
// wait for start from master thread
|
||||||
while(!start);
|
while (!start)
|
||||||
|
;
|
||||||
|
|
||||||
for (int j = 0; j < 100000000; j++)
|
for (int j = 0; j < 100000000; j++)
|
||||||
{
|
{
|
||||||
flag[me]=1;
|
flag[me] = 1;
|
||||||
while(flag[you])
|
while (flag[you])
|
||||||
{
|
{
|
||||||
/* do nothing */
|
/* do nothing */
|
||||||
}
|
}
|
||||||
@@ -43,10 +44,10 @@ void* thread_routine(void* args)
|
|||||||
counter1++;
|
counter1++;
|
||||||
counter2++;
|
counter2++;
|
||||||
// leaving critical section
|
// 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;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -58,21 +59,23 @@ int main()
|
|||||||
|
|
||||||
pthread_t thr1;
|
pthread_t thr1;
|
||||||
pthread_t thr2;
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
start = 1;
|
start = 1;
|
||||||
|
|
||||||
pthread_join(thr1,NULL);
|
pthread_join(thr1, NULL);
|
||||||
pthread_join(thr2,NULL);
|
pthread_join(thr2, NULL);
|
||||||
|
|
||||||
printf("counter1: %d\n",counter1);
|
printf("counter1: %d\n", counter1);
|
||||||
printf("counter2: %d\n",counter2);
|
printf("counter2: %d\n", counter2);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
20
Examples/concurrency/con3.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/con3.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.con3</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
BIN
Examples/concurrency/con3.dSYM/Contents/Resources/DWARF/con3
Normal file
BIN
Examples/concurrency/con3.dSYM/Contents/Resources/DWARF/con3
Normal file
Binary file not shown.
BIN
Examples/concurrency/con4
Executable file
BIN
Examples/concurrency/con4
Executable file
Binary file not shown.
@@ -7,11 +7,11 @@
|
|||||||
* Prone to livelock since threads might be constantly releasing their flag
|
* Prone to livelock since threads might be constantly releasing their flag
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
// shared global
|
// shared global
|
||||||
static int counter1 = 0;
|
static int counter1 = 0;
|
||||||
@@ -21,23 +21,24 @@ static int counter2 = 0;
|
|||||||
volatile int start = 0;
|
volatile int start = 0;
|
||||||
|
|
||||||
// flag array
|
// 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;
|
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
|
// wait for start from master thread
|
||||||
while(!start);
|
while (!start)
|
||||||
|
;
|
||||||
|
|
||||||
for (int j = 0; j < 1000000; j++)
|
for (int j = 0; j < 1000000; j++)
|
||||||
{
|
{
|
||||||
flag[me] = 1;
|
flag[me] = 1;
|
||||||
while(flag[you])
|
while (flag[you])
|
||||||
{
|
{
|
||||||
// just in case there is deadlock
|
// just in case there is deadlock
|
||||||
flag[me] = 0;
|
flag[me] = 0;
|
||||||
@@ -51,12 +52,11 @@ void* thread_routine(void* args)
|
|||||||
flag[me] = 0;
|
flag[me] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Worker thread: %d done\n",me);
|
printf("Worker thread: %d done\n", me);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int val1 = 0;
|
int val1 = 0;
|
||||||
@@ -64,21 +64,23 @@ int main()
|
|||||||
|
|
||||||
pthread_t thr1;
|
pthread_t thr1;
|
||||||
pthread_t thr2;
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
start = 1;
|
start = 1;
|
||||||
|
|
||||||
pthread_join(thr1,NULL);
|
pthread_join(thr1, NULL);
|
||||||
pthread_join(thr2,NULL);
|
pthread_join(thr2, NULL);
|
||||||
|
|
||||||
printf("counter1: %d\n",counter1);
|
printf("counter1: %d\n", counter1);
|
||||||
printf("counter2: %d\n",counter2);
|
printf("counter2: %d\n", counter2);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
20
Examples/concurrency/con4.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/con4.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.con4</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
BIN
Examples/concurrency/con4.dSYM/Contents/Resources/DWARF/con4
Normal file
BIN
Examples/concurrency/con4.dSYM/Contents/Resources/DWARF/con4
Normal file
Binary file not shown.
BIN
Examples/concurrency/con_dekkers
Executable file
BIN
Examples/concurrency/con_dekkers
Executable file
Binary file not shown.
@@ -7,11 +7,11 @@
|
|||||||
* Also does not work on modern CPUs that perform out-of-order execution
|
* Also does not work on modern CPUs that perform out-of-order execution
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
// shared global
|
// shared global
|
||||||
static int counter1 = 0;
|
static int counter1 = 0;
|
||||||
@@ -24,30 +24,32 @@ volatile int start = 0;
|
|||||||
volatile int turn = 0;
|
volatile int turn = 0;
|
||||||
|
|
||||||
// flag array
|
// 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;
|
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
|
// wait for start from master thread
|
||||||
while(!start);
|
while (!start)
|
||||||
|
;
|
||||||
|
|
||||||
for (int j = 0; j < 1000000; j++)
|
for (int j = 0; j < 1000000; j++)
|
||||||
{
|
{
|
||||||
flag[me] = 1;
|
flag[me] = 1;
|
||||||
while(flag[you])
|
while (flag[you])
|
||||||
{
|
{
|
||||||
if(turn == you)
|
if (turn == you)
|
||||||
{
|
{
|
||||||
// just in case there is deadlock
|
// just in case there is deadlock
|
||||||
flag[me] = 0;
|
flag[me] = 0;
|
||||||
}
|
}
|
||||||
while (turn == you);
|
while (turn == you)
|
||||||
|
;
|
||||||
flag[me] = 1;
|
flag[me] = 1;
|
||||||
}
|
}
|
||||||
// this is the critical section
|
// this is the critical section
|
||||||
@@ -58,12 +60,11 @@ void* thread_routine(void* args)
|
|||||||
flag[me] = 0;
|
flag[me] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Worker thread: %d done\n",me);
|
printf("Worker thread: %d done\n", me);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int val1 = 0;
|
int val1 = 0;
|
||||||
@@ -71,21 +72,23 @@ int main()
|
|||||||
|
|
||||||
pthread_t thr1;
|
pthread_t thr1;
|
||||||
pthread_t thr2;
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
start = 1;
|
start = 1;
|
||||||
|
|
||||||
pthread_join(thr1,NULL);
|
pthread_join(thr1, NULL);
|
||||||
pthread_join(thr2,NULL);
|
pthread_join(thr2, NULL);
|
||||||
|
|
||||||
printf("counter1: %d\n",counter1);
|
printf("counter1: %d\n", counter1);
|
||||||
printf("counter2: %d\n",counter2);
|
printf("counter2: %d\n", counter2);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
20
Examples/concurrency/con_dekkers.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/con_dekkers.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.con_dekkers</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
Binary file not shown.
BIN
Examples/concurrency/mutex1
Executable file
BIN
Examples/concurrency/mutex1
Executable file
Binary file not shown.
@@ -3,11 +3,11 @@
|
|||||||
* that increments two global variables
|
* that increments two global variables
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
// shared global
|
// shared global
|
||||||
static int counter1 = 0;
|
static int counter1 = 0;
|
||||||
@@ -20,16 +20,17 @@ volatile int start;
|
|||||||
pthread_mutex_t flag = PTHREAD_MUTEX_INITIALIZER;
|
pthread_mutex_t flag = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
// The thread process
|
// 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;
|
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
|
// wait for start from master thread
|
||||||
while(!start);
|
while (!start)
|
||||||
|
;
|
||||||
|
|
||||||
for (int j = 0; j < 1000000; j++)
|
for (int j = 0; j < 1000000; j++)
|
||||||
{
|
{
|
||||||
@@ -41,12 +42,11 @@ void* thread_routine(void* args)
|
|||||||
pthread_mutex_unlock(&flag);
|
pthread_mutex_unlock(&flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Worker thread: %d done\n",me);
|
printf("Worker thread: %d done\n", me);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Main process
|
// Main process
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -55,22 +55,24 @@ int main()
|
|||||||
|
|
||||||
pthread_t thr1;
|
pthread_t thr1;
|
||||||
pthread_t thr2;
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
start = 1;
|
start = 1;
|
||||||
|
|
||||||
pthread_join(thr1,NULL);
|
pthread_join(thr1, NULL);
|
||||||
pthread_join(thr2,NULL);
|
pthread_join(thr2, NULL);
|
||||||
|
|
||||||
printf("counter1: %d\n",counter1);
|
printf("counter1: %d\n", counter1);
|
||||||
printf("counter2: %d\n",counter2);
|
printf("counter2: %d\n", counter2);
|
||||||
|
|
||||||
pthread_mutex_destroy(&flag);
|
pthread_mutex_destroy(&flag);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|||||||
20
Examples/concurrency/mutex1.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/mutex1.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.mutex1</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
BIN
Examples/concurrency/mutex1.dSYM/Contents/Resources/DWARF/mutex1
Normal file
BIN
Examples/concurrency/mutex1.dSYM/Contents/Resources/DWARF/mutex1
Normal file
Binary file not shown.
BIN
Examples/concurrency/mutex2
Executable file
BIN
Examples/concurrency/mutex2
Executable file
Binary file not shown.
@@ -4,11 +4,11 @@
|
|||||||
* unlocked
|
* unlocked
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
// shared global
|
// shared global
|
||||||
static int counter1 = 0;
|
static int counter1 = 0;
|
||||||
@@ -21,16 +21,17 @@ volatile int start;
|
|||||||
pthread_mutex_t flag = PTHREAD_MUTEX_INITIALIZER;
|
pthread_mutex_t flag = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
// The thread process
|
// 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;
|
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
|
// wait for start from master thread
|
||||||
while(!start);
|
while (!start)
|
||||||
|
;
|
||||||
|
|
||||||
for (int j = 0; j < 1000000; j++)
|
for (int j = 0; j < 1000000; j++)
|
||||||
{
|
{
|
||||||
@@ -41,17 +42,17 @@ void* thread_routine(void* args)
|
|||||||
// leaving critical section
|
// leaving critical section
|
||||||
pthread_mutex_unlock(&flag);
|
pthread_mutex_unlock(&flag);
|
||||||
|
|
||||||
if(me == 0) {
|
if (me == 0)
|
||||||
|
{
|
||||||
pthread_mutex_unlock(&flag);
|
pthread_mutex_unlock(&flag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Worker thread: %d done\n",me);
|
printf("Worker thread: %d done\n", me);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Main process
|
// Main process
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -60,22 +61,24 @@ int main()
|
|||||||
|
|
||||||
pthread_t thr1;
|
pthread_t thr1;
|
||||||
pthread_t thr2;
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
start = 1;
|
start = 1;
|
||||||
|
|
||||||
pthread_join(thr1,NULL);
|
pthread_join(thr1, NULL);
|
||||||
pthread_join(thr2,NULL);
|
pthread_join(thr2, NULL);
|
||||||
|
|
||||||
printf("counter1: %d\n",counter1);
|
printf("counter1: %d\n", counter1);
|
||||||
printf("counter2: %d\n",counter2);
|
printf("counter2: %d\n", counter2);
|
||||||
|
|
||||||
pthread_mutex_destroy(&flag);
|
pthread_mutex_destroy(&flag);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|||||||
20
Examples/concurrency/mutex2.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/mutex2.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.mutex2</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
BIN
Examples/concurrency/mutex2.dSYM/Contents/Resources/DWARF/mutex2
Normal file
BIN
Examples/concurrency/mutex2.dSYM/Contents/Resources/DWARF/mutex2
Normal file
Binary file not shown.
BIN
Examples/concurrency/mutex3
Executable file
BIN
Examples/concurrency/mutex3
Executable file
Binary file not shown.
@@ -4,11 +4,11 @@
|
|||||||
* locked
|
* locked
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
// shared global
|
// shared global
|
||||||
static int counter1 = 0;
|
static int counter1 = 0;
|
||||||
@@ -21,21 +21,23 @@ volatile int start;
|
|||||||
pthread_mutex_t flag = PTHREAD_MUTEX_INITIALIZER;
|
pthread_mutex_t flag = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
// The thread process
|
// 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;
|
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
|
// wait for start from master thread
|
||||||
while(!start);
|
while (!start)
|
||||||
|
;
|
||||||
|
|
||||||
for (int j = 0; j < 1000000; j++)
|
for (int j = 0; j < 1000000; j++)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&flag);
|
pthread_mutex_lock(&flag);
|
||||||
if(me == 0) {
|
if (me == 0)
|
||||||
|
{
|
||||||
pthread_mutex_lock(&flag);
|
pthread_mutex_lock(&flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,12 +48,11 @@ void* thread_routine(void* args)
|
|||||||
pthread_mutex_unlock(&flag);
|
pthread_mutex_unlock(&flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Worker thread: %d done\n",me);
|
printf("Worker thread: %d done\n", me);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Main process
|
// Main process
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -60,22 +61,24 @@ int main()
|
|||||||
|
|
||||||
pthread_t thr1;
|
pthread_t thr1;
|
||||||
pthread_t thr2;
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
start = 1;
|
start = 1;
|
||||||
|
|
||||||
pthread_join(thr1,NULL);
|
pthread_join(thr1, NULL);
|
||||||
pthread_join(thr2,NULL);
|
pthread_join(thr2, NULL);
|
||||||
|
|
||||||
printf("counter1: %d\n",counter1);
|
printf("counter1: %d\n", counter1);
|
||||||
printf("counter2: %d\n",counter2);
|
printf("counter2: %d\n", counter2);
|
||||||
|
|
||||||
pthread_mutex_destroy(&flag);
|
pthread_mutex_destroy(&flag);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|||||||
20
Examples/concurrency/mutex3.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/mutex3.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.mutex3</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
BIN
Examples/concurrency/mutex3.dSYM/Contents/Resources/DWARF/mutex3
Normal file
BIN
Examples/concurrency/mutex3.dSYM/Contents/Resources/DWARF/mutex3
Normal file
Binary file not shown.
BIN
Examples/concurrency/prod_cons1
Executable file
BIN
Examples/concurrency/prod_cons1
Executable file
Binary file not shown.
@@ -6,19 +6,20 @@
|
|||||||
* a consumer might attempt to read when the space is empty
|
* a consumer might attempt to read when the space is empty
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#define BUFFER_SIZE 30
|
#define BUFFER_SIZE 30
|
||||||
#define ELEMENTS 100
|
#define ELEMENTS 100
|
||||||
|
|
||||||
// Shared Buffer
|
// Shared Buffer
|
||||||
typedef struct circular_buffer {
|
typedef struct circular_buffer
|
||||||
|
{
|
||||||
unsigned char values[BUFFER_SIZE];
|
unsigned char values[BUFFER_SIZE];
|
||||||
int out_idx;
|
int out_idx;
|
||||||
int in_idx;
|
int in_idx;
|
||||||
@@ -26,32 +27,34 @@ typedef struct circular_buffer {
|
|||||||
|
|
||||||
circular_buffer buffer;
|
circular_buffer buffer;
|
||||||
|
|
||||||
void buffer_init(circular_buffer* b)
|
void buffer_init(circular_buffer *b)
|
||||||
{
|
{
|
||||||
b->out_idx = 0;
|
b->out_idx = 0;
|
||||||
b->in_idx = 0;
|
b->in_idx = 0;
|
||||||
memset(b->values, 0, BUFFER_SIZE);
|
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) {
|
if (b->values[b->in_idx] != 0)
|
||||||
|
{
|
||||||
printf("ERROR: Inserting into buffer when an element exists. Empty was expected\n");
|
printf("ERROR: Inserting into buffer when an element exists. Empty was expected\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
b->values[b->in_idx] = value;
|
b->values[b->in_idx] = value;
|
||||||
b->in_idx = b->in_idx < BUFFER_SIZE-1 ? b->in_idx + 1 : 0;
|
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];
|
unsigned char return_value = b->values[b->out_idx];
|
||||||
if(return_value == 0) {
|
if (return_value == 0)
|
||||||
|
{
|
||||||
printf("ERROR: Removing from a buffer at an empty element. Something was expected\n");
|
printf("ERROR: Removing from a buffer at an empty element. Something was expected\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
b->values[b->out_idx] = 0;
|
b->values[b->out_idx] = 0;
|
||||||
b->out_idx = b->out_idx < BUFFER_SIZE-1 ? b->out_idx + 1 : 0;
|
b->out_idx = b->out_idx < BUFFER_SIZE - 1 ? b->out_idx + 1 : 0;
|
||||||
return return_value;
|
return return_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,12 +62,14 @@ unsigned char buffer_remove(circular_buffer* b)
|
|||||||
volatile int start = 0;
|
volatile int start = 0;
|
||||||
|
|
||||||
// Producer Routine
|
// Producer Routine
|
||||||
void* producer()
|
void *producer()
|
||||||
{
|
{
|
||||||
// wait for start from master thread
|
// wait for start from master thread
|
||||||
while(!start);
|
while (!start)
|
||||||
|
;
|
||||||
|
|
||||||
for(int i = 0; i < ELEMENTS; i++) {
|
for (int i = 0; i < ELEMENTS; i++)
|
||||||
|
{
|
||||||
// Add an element to the buffer
|
// Add an element to the buffer
|
||||||
unsigned char value = (i % 100) + 1; // Make sure the value isn't zero
|
unsigned char value = (i % 100) + 1; // Make sure the value isn't zero
|
||||||
buffer_insert(&buffer, value);
|
buffer_insert(&buffer, value);
|
||||||
@@ -75,12 +80,14 @@ void* producer()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Consumer Routine
|
// Consumer Routine
|
||||||
void* consumer()
|
void *consumer()
|
||||||
{
|
{
|
||||||
// wait for start from master thread
|
// wait for start from master thread
|
||||||
while(!start);
|
while (!start)
|
||||||
|
;
|
||||||
|
|
||||||
for(int i = 0; i < ELEMENTS; i++) {
|
for (int i = 0; i < ELEMENTS; i++)
|
||||||
|
{
|
||||||
// Remove an element from the buffer
|
// Remove an element from the buffer
|
||||||
printf("Consumer removed: %d\n", buffer_remove(&buffer));
|
printf("Consumer removed: %d\n", buffer_remove(&buffer));
|
||||||
}
|
}
|
||||||
@@ -88,7 +95,6 @@ void* consumer()
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Main process
|
// Main process
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -96,11 +102,13 @@ int main()
|
|||||||
|
|
||||||
pthread_t prod;
|
pthread_t prod;
|
||||||
pthread_t cons;
|
pthread_t cons;
|
||||||
if(pthread_create(&prod, NULL, producer, NULL) == -1) {
|
if (pthread_create(&prod, NULL, producer, NULL) == -1)
|
||||||
|
{
|
||||||
printf("COULD NOT CREATE PRODUCER\n");
|
printf("COULD NOT CREATE PRODUCER\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if(pthread_create(&cons, NULL, consumer, NULL) == -1) {
|
if (pthread_create(&cons, NULL, consumer, NULL) == -1)
|
||||||
|
{
|
||||||
printf("COULD NOT CREATE CONSUMER\n");
|
printf("COULD NOT CREATE CONSUMER\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|||||||
20
Examples/concurrency/prod_cons1.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/prod_cons1.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.prod_cons1</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
Binary file not shown.
BIN
Examples/concurrency/prod_cons2
Executable file
BIN
Examples/concurrency/prod_cons2
Executable file
Binary file not shown.
@@ -7,19 +7,20 @@
|
|||||||
* Consusmers wait for at least one filled spot before reading
|
* Consusmers wait for at least one filled spot before reading
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#define BUFFER_SIZE 30
|
#define BUFFER_SIZE 30
|
||||||
#define ELEMENTS 100
|
#define ELEMENTS 100
|
||||||
|
|
||||||
// Shared Buffer
|
// Shared Buffer
|
||||||
typedef struct circular_buffer {
|
typedef struct circular_buffer
|
||||||
|
{
|
||||||
unsigned char values[BUFFER_SIZE];
|
unsigned char values[BUFFER_SIZE];
|
||||||
int out_idx;
|
int out_idx;
|
||||||
int in_idx;
|
int in_idx;
|
||||||
@@ -27,32 +28,34 @@ typedef struct circular_buffer {
|
|||||||
|
|
||||||
circular_buffer buffer;
|
circular_buffer buffer;
|
||||||
|
|
||||||
void buffer_init(circular_buffer* b)
|
void buffer_init(circular_buffer *b)
|
||||||
{
|
{
|
||||||
b->out_idx = 0;
|
b->out_idx = 0;
|
||||||
b->in_idx = 0;
|
b->in_idx = 0;
|
||||||
memset(b->values, 0, BUFFER_SIZE);
|
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) {
|
if (b->values[b->in_idx] != 0)
|
||||||
|
{
|
||||||
printf("ERROR: Inserting into buffer when an element exists. Empty was expected\n");
|
printf("ERROR: Inserting into buffer when an element exists. Empty was expected\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
b->values[b->in_idx] = value;
|
b->values[b->in_idx] = value;
|
||||||
b->in_idx = b->in_idx < BUFFER_SIZE-1 ? b->in_idx + 1 : 0;
|
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];
|
unsigned char return_value = b->values[b->out_idx];
|
||||||
if(return_value == 0) {
|
if (return_value == 0)
|
||||||
|
{
|
||||||
printf("ERROR: Removing from a buffer at an empty element. Something was expected\n");
|
printf("ERROR: Removing from a buffer at an empty element. Something was expected\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
b->values[b->out_idx] = 0;
|
b->values[b->out_idx] = 0;
|
||||||
b->out_idx = b->out_idx < BUFFER_SIZE-1 ? b->out_idx + 1 : 0;
|
b->out_idx = b->out_idx < BUFFER_SIZE - 1 ? b->out_idx + 1 : 0;
|
||||||
return return_value;
|
return return_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,12 +67,14 @@ sem_t emptyCount;
|
|||||||
volatile int start = 0;
|
volatile int start = 0;
|
||||||
|
|
||||||
// Producer Routine
|
// Producer Routine
|
||||||
void* producer()
|
void *producer()
|
||||||
{
|
{
|
||||||
// wait for start from master thread
|
// wait for start from master thread
|
||||||
while(!start);
|
while (!start)
|
||||||
|
;
|
||||||
|
|
||||||
for(int i = 0; i < ELEMENTS; i++) {
|
for (int i = 0; i < ELEMENTS; i++)
|
||||||
|
{
|
||||||
// Wait for a free spot in the buffer
|
// Wait for a free spot in the buffer
|
||||||
sem_wait(&emptyCount);
|
sem_wait(&emptyCount);
|
||||||
|
|
||||||
@@ -86,12 +91,14 @@ void* producer()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Consumer Routine
|
// Consumer Routine
|
||||||
void* consumer()
|
void *consumer()
|
||||||
{
|
{
|
||||||
// wait for start from master thread
|
// wait for start from master thread
|
||||||
while(!start);
|
while (!start)
|
||||||
|
;
|
||||||
|
|
||||||
for(int i = 0; i < ELEMENTS; i++) {
|
for (int i = 0; i < ELEMENTS; i++)
|
||||||
|
{
|
||||||
// Wait for element to consume
|
// Wait for element to consume
|
||||||
sem_wait(&fullCount);
|
sem_wait(&fullCount);
|
||||||
|
|
||||||
@@ -105,7 +112,6 @@ void* consumer()
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Main process
|
// Main process
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -115,11 +121,13 @@ int main()
|
|||||||
|
|
||||||
pthread_t prod;
|
pthread_t prod;
|
||||||
pthread_t cons;
|
pthread_t cons;
|
||||||
if(pthread_create(&prod, NULL, producer, NULL) == -1) {
|
if (pthread_create(&prod, NULL, producer, NULL) == -1)
|
||||||
|
{
|
||||||
printf("COULD NOT CREATE PRODUCER\n");
|
printf("COULD NOT CREATE PRODUCER\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if(pthread_create(&cons, NULL, consumer, NULL) == -1) {
|
if (pthread_create(&cons, NULL, consumer, NULL) == -1)
|
||||||
|
{
|
||||||
printf("COULD NOT CREATE CONSUMER\n");
|
printf("COULD NOT CREATE CONSUMER\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|||||||
20
Examples/concurrency/prod_cons2.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/prod_cons2.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.prod_cons2</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
Binary file not shown.
BIN
Examples/concurrency/race
Executable file
BIN
Examples/concurrency/race
Executable file
Binary file not shown.
@@ -1,23 +1,23 @@
|
|||||||
/*
|
s /*
|
||||||
* race.c - Shows the race condition that can happen
|
* race.c - Shows the race condition that can happen
|
||||||
* then threads try to modify multiple global
|
* then threads try to modify multiple global
|
||||||
* values at the same time
|
* values at the same time
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
// start flag
|
// start flag
|
||||||
volatile int start = 0;
|
volatile int start = 0;
|
||||||
|
|
||||||
// shared global
|
// shared global
|
||||||
static int counter1 = 0;
|
static int counter1 = 0;
|
||||||
static int counter2 = 0;
|
static int counter2 = 0;
|
||||||
|
|
||||||
void* thread_routine(void* args)
|
void *thread_routine(void *args)
|
||||||
{
|
{
|
||||||
int me = *((int *)args);
|
int me = *((int *)args);
|
||||||
int you = me ? 0 : 1;
|
int you = me ? 0 : 1;
|
||||||
@@ -25,7 +25,8 @@ void* thread_routine(void* args)
|
|||||||
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
|
// wait for start from master thread
|
||||||
while(!start);
|
while (!start)
|
||||||
|
;
|
||||||
|
|
||||||
for (int j = 0; j < 100000000; j++)
|
for (int j = 0; j < 100000000; j++)
|
||||||
{
|
{
|
||||||
@@ -35,7 +36,7 @@ void* thread_routine(void* args)
|
|||||||
// leaving critical section
|
// leaving critical section
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Worker thread: %d done\n",me);
|
printf("Worker thread: %d done\n", me);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -47,22 +48,23 @@ int main()
|
|||||||
|
|
||||||
pthread_t thr1;
|
pthread_t thr1;
|
||||||
pthread_t thr2;
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
start = 1;
|
start = 1;
|
||||||
|
|
||||||
pthread_join(thr1,NULL);
|
pthread_join(thr1, NULL);
|
||||||
pthread_join(thr2,NULL);
|
pthread_join(thr2, NULL);
|
||||||
|
|
||||||
printf("counter1: %d\n",counter1);
|
printf("counter1: %d\n", counter1);
|
||||||
printf("counter2: %d\n",counter2);
|
printf("counter2: %d\n", counter2);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
20
Examples/concurrency/race.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/race.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.race</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
BIN
Examples/concurrency/race.dSYM/Contents/Resources/DWARF/race
Normal file
BIN
Examples/concurrency/race.dSYM/Contents/Resources/DWARF/race
Normal file
Binary file not shown.
BIN
Examples/concurrency/sem1
Executable file
BIN
Examples/concurrency/sem1
Executable file
Binary file not shown.
@@ -3,12 +3,12 @@
|
|||||||
* critical section
|
* critical section
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <semaphore.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
|
||||||
#include <semaphore.h>
|
|
||||||
|
|
||||||
// shared global
|
// shared global
|
||||||
static int counter1 = 0;
|
static int counter1 = 0;
|
||||||
@@ -21,16 +21,17 @@ volatile int start;
|
|||||||
sem_t flag;
|
sem_t flag;
|
||||||
|
|
||||||
// The thread process
|
// 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;
|
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
|
// wait for start from master thread
|
||||||
while(!start);
|
while (!start)
|
||||||
|
;
|
||||||
|
|
||||||
for (int j = 0; j < 1000000; j++)
|
for (int j = 0; j < 1000000; j++)
|
||||||
{
|
{
|
||||||
@@ -42,12 +43,11 @@ void* thread_routine(void* args)
|
|||||||
sem_post(&flag);
|
sem_post(&flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Worker thread: %d done\n",me);
|
printf("Worker thread: %d done\n", me);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Main process
|
// Main process
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -59,22 +59,24 @@ int main()
|
|||||||
|
|
||||||
pthread_t thr1;
|
pthread_t thr1;
|
||||||
pthread_t thr2;
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
start = 1;
|
start = 1;
|
||||||
|
|
||||||
pthread_join(thr1,NULL);
|
pthread_join(thr1, NULL);
|
||||||
pthread_join(thr2,NULL);
|
pthread_join(thr2, NULL);
|
||||||
|
|
||||||
printf("counter1: %d\n",counter1);
|
printf("counter1: %d\n", counter1);
|
||||||
printf("counter2: %d\n",counter2);
|
printf("counter2: %d\n", counter2);
|
||||||
|
|
||||||
// Destroy the semaphore
|
// Destroy the semaphore
|
||||||
sem_destroy(&flag);
|
sem_destroy(&flag);
|
||||||
|
|||||||
20
Examples/concurrency/sem1.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/sem1.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.sem1</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
BIN
Examples/concurrency/sem1.dSYM/Contents/Resources/DWARF/sem1
Normal file
BIN
Examples/concurrency/sem1.dSYM/Contents/Resources/DWARF/sem1
Normal file
Binary file not shown.
BIN
Examples/concurrency/sem2
Executable file
BIN
Examples/concurrency/sem2
Executable file
Binary file not shown.
@@ -4,12 +4,12 @@
|
|||||||
* the count regardless of the current value
|
* the count regardless of the current value
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <semaphore.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
|
||||||
#include <semaphore.h>
|
|
||||||
|
|
||||||
// shared global
|
// shared global
|
||||||
static int counter1 = 0;
|
static int counter1 = 0;
|
||||||
@@ -22,16 +22,17 @@ volatile int start;
|
|||||||
sem_t flag;
|
sem_t flag;
|
||||||
|
|
||||||
// The thread process
|
// 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;
|
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
|
// wait for start from master thread
|
||||||
while(!start);
|
while (!start)
|
||||||
|
;
|
||||||
|
|
||||||
for (int j = 0; j < 100000000; j++)
|
for (int j = 0; j < 100000000; j++)
|
||||||
{
|
{
|
||||||
@@ -42,17 +43,17 @@ void* thread_routine(void* args)
|
|||||||
// leaving critical section
|
// leaving critical section
|
||||||
sem_post(&flag);
|
sem_post(&flag);
|
||||||
|
|
||||||
if(me == 0) {
|
if (me == 0)
|
||||||
|
{
|
||||||
sem_post(&flag);
|
sem_post(&flag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Worker thread: %d done\n",me);
|
printf("Worker thread: %d done\n", me);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Main process
|
// Main process
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -63,22 +64,24 @@ int main()
|
|||||||
|
|
||||||
pthread_t thr1;
|
pthread_t thr1;
|
||||||
pthread_t thr2;
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
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");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
start = 1;
|
start = 1;
|
||||||
|
|
||||||
pthread_join(thr1,NULL);
|
pthread_join(thr1, NULL);
|
||||||
pthread_join(thr2,NULL);
|
pthread_join(thr2, NULL);
|
||||||
|
|
||||||
printf("counter1: %d\n",counter1);
|
printf("counter1: %d\n", counter1);
|
||||||
printf("counter2: %d\n",counter2);
|
printf("counter2: %d\n", counter2);
|
||||||
|
|
||||||
sem_destroy(&flag);
|
sem_destroy(&flag);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|||||||
20
Examples/concurrency/sem2.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/sem2.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.sem2</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
BIN
Examples/concurrency/sem2.dSYM/Contents/Resources/DWARF/sem2
Normal file
BIN
Examples/concurrency/sem2.dSYM/Contents/Resources/DWARF/sem2
Normal file
Binary file not shown.
BIN
Examples/concurrency/sem_list1
Executable file
BIN
Examples/concurrency/sem_list1
Executable file
Binary file not shown.
@@ -3,20 +3,22 @@
|
|||||||
* uncontrolled access to a singly linked list
|
* uncontrolled access to a singly linked list
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <semaphore.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
|
||||||
#include <semaphore.h>
|
|
||||||
|
|
||||||
typedef struct node {
|
typedef struct node
|
||||||
|
{
|
||||||
int val;
|
int val;
|
||||||
struct node* next;
|
struct node *next;
|
||||||
} node;
|
} node;
|
||||||
|
|
||||||
typedef struct list {
|
typedef struct list
|
||||||
node* head;
|
{
|
||||||
|
node *head;
|
||||||
} list;
|
} list;
|
||||||
|
|
||||||
// shared global
|
// shared global
|
||||||
@@ -26,16 +28,17 @@ static list mylist;
|
|||||||
volatile int start = 0;
|
volatile int start = 0;
|
||||||
|
|
||||||
// The thread process
|
// The thread process
|
||||||
void* thread_routine()
|
void *thread_routine()
|
||||||
{
|
{
|
||||||
printf("Worker thread: %lu ready\n", pthread_self());
|
printf("Worker thread: %lu ready\n", pthread_self());
|
||||||
|
|
||||||
// wait for start from master thread
|
// wait for start from master thread
|
||||||
while(start == 0);
|
while (start == 0)
|
||||||
|
;
|
||||||
|
|
||||||
for (int j = 0; j < 1000000; j++)
|
for (int j = 0; j < 1000000; j++)
|
||||||
{
|
{
|
||||||
node* new_node = malloc(sizeof(node));
|
node *new_node = malloc(sizeof(node));
|
||||||
new_node->val = j;
|
new_node->val = j;
|
||||||
new_node->next = mylist.head;
|
new_node->next = mylist.head;
|
||||||
mylist.head = new_node;
|
mylist.head = new_node;
|
||||||
@@ -49,14 +52,16 @@ void* thread_routine()
|
|||||||
// Main process
|
// Main process
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
#define THREAD_COUNT 10
|
#define THREAD_COUNT 10
|
||||||
pthread_t thr_ids[THREAD_COUNT];
|
pthread_t thr_ids[THREAD_COUNT];
|
||||||
|
|
||||||
mylist.head = NULL;
|
mylist.head = NULL;
|
||||||
|
|
||||||
// Create the threads
|
// Create the threads
|
||||||
for(int i = 0; i < THREAD_COUNT; i++) {
|
for (int i = 0; i < THREAD_COUNT; i++)
|
||||||
if(pthread_create(&thr_ids[i], NULL, thread_routine, NULL) == -1) {
|
{
|
||||||
|
if (pthread_create(&thr_ids[i], NULL, thread_routine, NULL) == -1)
|
||||||
|
{
|
||||||
printf("COULD NOT CREATE A THREAD\n");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@@ -66,22 +71,25 @@ int main()
|
|||||||
start = 1;
|
start = 1;
|
||||||
|
|
||||||
// Wait for all threads to finish
|
// Wait for all threads to finish
|
||||||
for(int i = 0; i < THREAD_COUNT; i++) {
|
for (int i = 0; i < THREAD_COUNT; i++)
|
||||||
pthread_join(thr_ids[i],NULL);
|
{
|
||||||
|
pthread_join(thr_ids[i], NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count the elements in the list
|
// Count the elements in the list
|
||||||
int length = 0;
|
int length = 0;
|
||||||
node* itr = mylist.head;
|
node *itr = mylist.head;
|
||||||
while(itr != NULL) {
|
while (itr != NULL)
|
||||||
|
{
|
||||||
length++;
|
length++;
|
||||||
itr = itr->next;
|
itr = itr->next;
|
||||||
}
|
}
|
||||||
printf("List contains %d elements\n", length);
|
printf("List contains %d elements\n", length);
|
||||||
|
|
||||||
// Free the list
|
// Free the list
|
||||||
while(mylist.head != NULL) {
|
while (mylist.head != NULL)
|
||||||
node* to_delete = mylist.head;
|
{
|
||||||
|
node *to_delete = mylist.head;
|
||||||
mylist.head = mylist.head->next;
|
mylist.head = mylist.head->next;
|
||||||
free(to_delete);
|
free(to_delete);
|
||||||
}
|
}
|
||||||
|
|||||||
20
Examples/concurrency/sem_list1.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/sem_list1.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.sem_list1</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
Binary file not shown.
BIN
Examples/concurrency/sem_list2
Executable file
BIN
Examples/concurrency/sem_list2
Executable file
Binary file not shown.
@@ -1,22 +1,24 @@
|
|||||||
/*
|
s /*
|
||||||
* sem_list2.c - Uses a semaphore as a way to control access
|
* sem_list2.c - Uses a semaphore as a way to control access
|
||||||
* to a critical section for a singly linked list
|
* to a critical section for a singly linked list
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <semaphore.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
|
||||||
#include <semaphore.h>
|
|
||||||
|
|
||||||
typedef struct node {
|
typedef struct node
|
||||||
|
{
|
||||||
int val;
|
int val;
|
||||||
struct node* next;
|
struct node *next;
|
||||||
} node;
|
} node;
|
||||||
|
|
||||||
typedef struct list {
|
typedef struct list
|
||||||
node* head;
|
{
|
||||||
|
node *head;
|
||||||
} list;
|
} list;
|
||||||
|
|
||||||
// shared global
|
// shared global
|
||||||
@@ -29,17 +31,18 @@ volatile int start = 0;
|
|||||||
sem_t flag;
|
sem_t flag;
|
||||||
|
|
||||||
// The thread process
|
// The thread process
|
||||||
void* thread_routine()
|
void *thread_routine()
|
||||||
{
|
{
|
||||||
printf("Worker thread: %lu ready\n", pthread_self());
|
printf("Worker thread: %lu ready\n", pthread_self());
|
||||||
|
|
||||||
// wait for start from master thread
|
// wait for start from master thread
|
||||||
while(start == 0);
|
while (start == 0)
|
||||||
|
;
|
||||||
|
|
||||||
for (int j = 0; j < 1000000; j++)
|
for (int j = 0; j < 1000000; j++)
|
||||||
{
|
{
|
||||||
sem_wait(&flag);
|
sem_wait(&flag);
|
||||||
node* new_node = malloc(sizeof(node));
|
node *new_node = malloc(sizeof(node));
|
||||||
new_node->val = j;
|
new_node->val = j;
|
||||||
new_node->next = mylist.head;
|
new_node->next = mylist.head;
|
||||||
mylist.head = new_node;
|
mylist.head = new_node;
|
||||||
@@ -56,14 +59,16 @@ int main()
|
|||||||
{
|
{
|
||||||
sem_init(&flag, 0, 1);
|
sem_init(&flag, 0, 1);
|
||||||
|
|
||||||
#define THREAD_COUNT 10
|
#define THREAD_COUNT 10
|
||||||
pthread_t thr_ids[THREAD_COUNT];
|
pthread_t thr_ids[THREAD_COUNT];
|
||||||
|
|
||||||
mylist.head = NULL;
|
mylist.head = NULL;
|
||||||
|
|
||||||
// Create the threads
|
// Create the threads
|
||||||
for(int i = 0; i < THREAD_COUNT; i++) {
|
for (int i = 0; i < THREAD_COUNT; i++)
|
||||||
if(pthread_create(&thr_ids[i], NULL, thread_routine, NULL) == -1) {
|
{
|
||||||
|
if (pthread_create(&thr_ids[i], NULL, thread_routine, NULL) == -1)
|
||||||
|
{
|
||||||
printf("COULD NOT CREATE A THREAD\n");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@@ -73,22 +78,25 @@ int main()
|
|||||||
start = 1;
|
start = 1;
|
||||||
|
|
||||||
// Wait for all threads to finish
|
// Wait for all threads to finish
|
||||||
for(int i = 0; i < THREAD_COUNT; i++) {
|
for (int i = 0; i < THREAD_COUNT; i++)
|
||||||
pthread_join(thr_ids[i],NULL);
|
{
|
||||||
|
pthread_join(thr_ids[i], NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count the elements in the list
|
// Count the elements in the list
|
||||||
int length = 0;
|
int length = 0;
|
||||||
node* itr = mylist.head;
|
node *itr = mylist.head;
|
||||||
while(itr != NULL) {
|
while (itr != NULL)
|
||||||
|
{
|
||||||
length++;
|
length++;
|
||||||
itr = itr->next;
|
itr = itr->next;
|
||||||
}
|
}
|
||||||
printf("List contains %d elements\n", length);
|
printf("List contains %d elements\n", length);
|
||||||
|
|
||||||
// Free the list
|
// Free the list
|
||||||
while(mylist.head != NULL) {
|
while (mylist.head != NULL)
|
||||||
node* to_delete = mylist.head;
|
{
|
||||||
|
node *to_delete = mylist.head;
|
||||||
mylist.head = mylist.head->next;
|
mylist.head = mylist.head->next;
|
||||||
free(to_delete);
|
free(to_delete);
|
||||||
}
|
}
|
||||||
|
|||||||
20
Examples/concurrency/sem_list2.dSYM/Contents/Info.plist
Normal file
20
Examples/concurrency/sem_list2.dSYM/Contents/Info.plist
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>CFBundleDevelopmentRegion</key>
|
||||||
|
<string>English</string>
|
||||||
|
<key>CFBundleIdentifier</key>
|
||||||
|
<string>com.apple.xcode.dsym.sem_list2</string>
|
||||||
|
<key>CFBundleInfoDictionaryVersion</key>
|
||||||
|
<string>6.0</string>
|
||||||
|
<key>CFBundlePackageType</key>
|
||||||
|
<string>dSYM</string>
|
||||||
|
<key>CFBundleSignature</key>
|
||||||
|
<string>????</string>
|
||||||
|
<key>CFBundleShortVersionString</key>
|
||||||
|
<string>1.0</string>
|
||||||
|
<key>CFBundleVersion</key>
|
||||||
|
<string>1</string>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
Binary file not shown.
Reference in New Issue
Block a user