sttuff
This commit is contained in:
@@ -95,7 +95,6 @@ int main()
|
|||||||
// Chop the commands into arguments and execute one at a time
|
// Chop the commands into arguments and execute one at a time
|
||||||
for (int i = 0; i < cmd_count; i++)
|
for (int i = 0; i < cmd_count; i++)
|
||||||
{
|
{
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// 1) Chop the command into command line arguments
|
// 1) Chop the command into command line arguments
|
||||||
// need to handle up to 10 arguments
|
// need to handle up to 10 arguments
|
||||||
|
|||||||
BIN
Examples/pthreads/pthreads1
Executable file
BIN
Examples/pthreads/pthreads1
Executable file
Binary file not shown.
@@ -15,9 +15,10 @@ int global_value = 10;
|
|||||||
// global data is shared between threads
|
// global data is shared between threads
|
||||||
//
|
//
|
||||||
// NOTE: that parameters and return value are pointers
|
// NOTE: that parameters and return value are pointers
|
||||||
void* thread_routine()
|
void *thread_routine()
|
||||||
{
|
{
|
||||||
for(int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
printf("THREAD READS GLOBAL VALUE: %d\n", global_value);
|
printf("THREAD READS GLOBAL VALUE: %d\n", global_value);
|
||||||
global_value++;
|
global_value++;
|
||||||
sleep(1);
|
sleep(1);
|
||||||
@@ -29,13 +30,15 @@ int main()
|
|||||||
{
|
{
|
||||||
// Create a thread
|
// Create a thread
|
||||||
pthread_t thr_id;
|
pthread_t thr_id;
|
||||||
if(pthread_create(&thr_id, NULL, thread_routine, NULL) == -1) {
|
if (pthread_create(&thr_id, 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write to global data
|
// Write to global data
|
||||||
for(int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
printf("PARENT READS GLOBAL: %d\n", global_value);
|
printf("PARENT READS GLOBAL: %d\n", global_value);
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
|
|||||||
20
Examples/pthreads/pthreads1.dSYM/Contents/Info.plist
Normal file
20
Examples/pthreads/pthreads1.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.pthreads1</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/pthreads/pthreads2
Executable file
BIN
Examples/pthreads/pthreads2
Executable file
Binary file not shown.
20
Examples/pthreads/pthreads2.dSYM/Contents/Info.plist
Normal file
20
Examples/pthreads/pthreads2.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.pthreads2</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/pthreads/pthreads3
Executable file
BIN
Examples/pthreads/pthreads3
Executable file
Binary file not shown.
@@ -11,8 +11,10 @@
|
|||||||
// Returns 1 if prime, 0 if not prime
|
// Returns 1 if prime, 0 if not prime
|
||||||
int is_prime(int v)
|
int is_prime(int v)
|
||||||
{
|
{
|
||||||
for(int i = 2; i < v; i++) {
|
for (int i = 2; i < v; i++)
|
||||||
if(v % i == 0) {
|
{
|
||||||
|
if (v % i == 0)
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,12 +25,15 @@ int is_prime(int v)
|
|||||||
// calls is_prime to check if the input argument is prime
|
// calls is_prime to check if the input argument is prime
|
||||||
//
|
//
|
||||||
// NOTE: that parameters and return value are pointers
|
// NOTE: that parameters and return value are pointers
|
||||||
void* thread_routine(void* args)
|
void *thread_routine(void *args)
|
||||||
{
|
{
|
||||||
int* val = (int*)args;
|
int *val = args;
|
||||||
if(is_prime(*val)) {
|
if (is_prime(*val))
|
||||||
|
{
|
||||||
printf("THREAD %lu FOUND that %d is prime\n", pthread_self(), *val);
|
printf("THREAD %lu FOUND that %d is prime\n", pthread_self(), *val);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
printf("THREAD %lu FOUND that %d is not prime\n", pthread_self(), *val);
|
printf("THREAD %lu FOUND that %d is not prime\n", pthread_self(), *val);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -42,11 +47,13 @@ int main()
|
|||||||
// Create 2 threads, passing a different value to each
|
// Create 2 threads, passing a different value to each
|
||||||
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, &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, &val2) == -1)
|
||||||
|
{
|
||||||
printf("COULD NOT CREATE A THREAD\n");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|||||||
20
Examples/pthreads/pthreads3.dSYM/Contents/Info.plist
Normal file
20
Examples/pthreads/pthreads3.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.pthreads3</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/pthreads/pthreads4
Executable file
BIN
Examples/pthreads/pthreads4
Executable file
Binary file not shown.
@@ -14,8 +14,10 @@
|
|||||||
// Returns 1 if prime, 0 if not prime
|
// Returns 1 if prime, 0 if not prime
|
||||||
int is_prime(int v)
|
int is_prime(int v)
|
||||||
{
|
{
|
||||||
for(int i = 2; i < v; i++) {
|
for (int i = 2; i < v; i++)
|
||||||
if(v % i == 0) {
|
{
|
||||||
|
if (v % i == 0)
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,13 +29,16 @@ int is_prime(int v)
|
|||||||
// Modifies the input parameter by dereferencing the pointer
|
// Modifies the input parameter by dereferencing the pointer
|
||||||
//
|
//
|
||||||
// NOTE: that parameters and return value are pointers
|
// NOTE: that parameters and return value are pointers
|
||||||
void* thread_routine(void* args)
|
void *thread_routine(void *args)
|
||||||
{
|
{
|
||||||
int* val = (int*)args;
|
int *val = (int *)args;
|
||||||
if(is_prime(*val)) {
|
if (is_prime(*val))
|
||||||
|
{
|
||||||
printf("THREAD %lu FOUND that %d is prime\n", pthread_self(), *val);
|
printf("THREAD %lu FOUND that %d is prime\n", pthread_self(), *val);
|
||||||
*val = 1;
|
*val = 1;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
printf("THREAD %lu FOUND that %d is not prime\n", pthread_self(), *val);
|
printf("THREAD %lu FOUND that %d is not prime\n", pthread_self(), *val);
|
||||||
*val = 0;
|
*val = 0;
|
||||||
}
|
}
|
||||||
@@ -51,11 +56,13 @@ int main()
|
|||||||
// Create 2 threads
|
// Create 2 threads
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|||||||
20
Examples/pthreads/pthreads4.dSYM/Contents/Info.plist
Normal file
20
Examples/pthreads/pthreads4.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.pthreads4</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/pthreads/pthreads5
Executable file
BIN
Examples/pthreads/pthreads5
Executable file
Binary file not shown.
@@ -17,8 +17,10 @@
|
|||||||
// Returns 1 if prime, 0 if not prime
|
// Returns 1 if prime, 0 if not prime
|
||||||
int is_prime(int v)
|
int is_prime(int v)
|
||||||
{
|
{
|
||||||
for(int i = 2; i < v; i++) {
|
for (int i = 2; i < v; i++)
|
||||||
if(v % i == 0) {
|
{
|
||||||
|
if (v % i == 0)
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,12 +31,15 @@ int is_prime(int v)
|
|||||||
// calls is_prime to check if the input argument is prime
|
// calls is_prime to check if the input argument is prime
|
||||||
//
|
//
|
||||||
// NOTE: that parameters and return value are pointers
|
// NOTE: that parameters and return value are pointers
|
||||||
void* thread_routine(void* args)
|
void *thread_routine(void *args)
|
||||||
{
|
{
|
||||||
int* val = (int*)args;
|
int *val = (int *)args;
|
||||||
if(is_prime(*val)) {
|
if (is_prime(*val))
|
||||||
|
{
|
||||||
printf("THREAD %lu FOUND that %d is prime\n", pthread_self(), *val);
|
printf("THREAD %lu FOUND that %d is prime\n", pthread_self(), *val);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
printf("THREAD %lu FOUND that %d is not prime\n", pthread_self(), *val);
|
printf("THREAD %lu FOUND that %d is not prime\n", pthread_self(), *val);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -49,7 +54,8 @@ pthread_t start_thread(int val)
|
|||||||
{
|
{
|
||||||
pthread_t thr;
|
pthread_t thr;
|
||||||
printf("Before thread creation val=%d\n", val);
|
printf("Before thread creation val=%d\n", val);
|
||||||
if(pthread_create(&thr, NULL, thread_routine, (void*)&val) == -1) {
|
if (pthread_create(&thr, NULL, thread_routine, (void *)&val) == -1)
|
||||||
|
{
|
||||||
printf("COULD NOT CREATE A THREAD\n");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|||||||
20
Examples/pthreads/pthreads5.dSYM/Contents/Info.plist
Normal file
20
Examples/pthreads/pthreads5.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.pthreads5</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/pthreads/pthreads6
Executable file
BIN
Examples/pthreads/pthreads6
Executable file
Binary file not shown.
@@ -16,8 +16,10 @@
|
|||||||
// Returns 1 if prime, 0 if not prime
|
// Returns 1 if prime, 0 if not prime
|
||||||
int is_prime(int v)
|
int is_prime(int v)
|
||||||
{
|
{
|
||||||
for(int i = 2; i < v; i++) {
|
for (int i = 2; i < v; i++)
|
||||||
if(v % i == 0) {
|
{
|
||||||
|
if (v % i == 0)
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,13 +31,16 @@ int is_prime(int v)
|
|||||||
// Modifies the input parameter by dereferencing the pointer
|
// Modifies the input parameter by dereferencing the pointer
|
||||||
//
|
//
|
||||||
// NOTE: that parameters and return value are pointers
|
// NOTE: that parameters and return value are pointers
|
||||||
void* thread_routine(void* args)
|
void *thread_routine(void *args)
|
||||||
{
|
{
|
||||||
int* val = (int*)args;
|
int *val = (int *)args;
|
||||||
if(is_prime(*val)) {
|
if (is_prime(*val))
|
||||||
|
{
|
||||||
printf("THREAD %lu FOUND that %d is prime %p\n", pthread_self(), *val, val);
|
printf("THREAD %lu FOUND that %d is prime %p\n", pthread_self(), *val, val);
|
||||||
*val = 1;
|
*val = 1;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
printf("THREAD %lu FOUND that %d is not prime %p\n", pthread_self(), *val, val);
|
printf("THREAD %lu FOUND that %d is not prime %p\n", pthread_self(), *val, val);
|
||||||
*val = 0;
|
*val = 0;
|
||||||
}
|
}
|
||||||
@@ -48,18 +53,21 @@ int main()
|
|||||||
|
|
||||||
// Create 10 threads
|
// Create 10 threads
|
||||||
pthread_t thr[10];
|
pthread_t thr[10];
|
||||||
for(int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
|
||||||
// NOTE: there is a single memory location for val which is passed to all threads
|
// NOTE: there is a single memory location for val which is passed to all threads
|
||||||
val = i + 2;
|
val = i + 2;
|
||||||
if(pthread_create(&thr[i], NULL, thread_routine, (void*)&val) == -1) {
|
if (pthread_create(&thr[i], NULL, thread_routine, (void *)&val) == -1)
|
||||||
|
{
|
||||||
printf("COULD NOT CREATE A THREAD\n");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for threads to finish
|
// Wait for threads to finish
|
||||||
for(int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
pthread_join(thr[i], NULL);
|
pthread_join(thr[i], NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
20
Examples/pthreads/pthreads6.dSYM/Contents/Info.plist
Normal file
20
Examples/pthreads/pthreads6.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.pthreads6</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/pthreads/pthreads_race
Executable file
BIN
Examples/pthreads/pthreads_race
Executable file
Binary file not shown.
@@ -11,12 +11,14 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int global_value = 0;
|
int global_value[5] = {0};
|
||||||
|
|
||||||
void* thread_routine()
|
void *thread_routine(void *arg)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < 100000000; i++) {
|
int *idx = (int *)(arg);
|
||||||
global_value++;
|
for (int i = 0; i < 1000000; i++)
|
||||||
|
{
|
||||||
|
global_value[*idx]++;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -25,21 +27,26 @@ int main()
|
|||||||
{
|
{
|
||||||
// Create 5 threads
|
// Create 5 threads
|
||||||
pthread_t thr_id[5];
|
pthread_t thr_id[5];
|
||||||
for(int i = 0; i < 5; i++) {
|
int args[5] = {0, 1, 2, 3, 4};
|
||||||
if(pthread_create(&thr_id[i], NULL, thread_routine, NULL) == -1) {
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
if (pthread_create(&thr_id[i], NULL, thread_routine, (void *)&args[i]) == -1)
|
||||||
|
{
|
||||||
printf("COULD NOT CREATE A THREAD\n");
|
printf("COULD NOT CREATE A THREAD\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for all threads to finish
|
// Wait for all threads to finish
|
||||||
for(int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
pthread_join(thr_id[i], NULL);
|
pthread_join(thr_id[i], NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print out final value of global value
|
// Print out final value of global value
|
||||||
// 5 threads * 100000000 iterations - expected value is 500000000
|
// 5 threads * 100000000 iterations - expected value is 500000000
|
||||||
printf("PARENT READS GLOBAL: %d\n", global_value);
|
for (int i = 0; i < 5; i++)
|
||||||
|
printf("PARENT READS GLOBAL: %d\n", global_value[i]);
|
||||||
|
|
||||||
// Return success
|
// Return success
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
20
Examples/pthreads/pthreads_race.dSYM/Contents/Info.plist
Normal file
20
Examples/pthreads/pthreads_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.pthreads_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>
|
||||||
Binary file not shown.
BIN
Examples/pthreads/pthreads_race_atomic
Executable file
BIN
Examples/pthreads/pthreads_race_atomic
Executable file
Binary file not shown.
@@ -6,16 +6,17 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <stdatomic.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdatomic.h>
|
|
||||||
|
|
||||||
atomic_int global_value = 0;
|
atomic_int global_value = 0;
|
||||||
|
|
||||||
void* thread_routine()
|
void *thread_routine()
|
||||||
{
|
{
|
||||||
for(int i = 0; i < 100000000; i++) {
|
for (int i = 0; i < 1000000; i++)
|
||||||
|
{
|
||||||
atomic_fetch_add(&global_value, 1);
|
atomic_fetch_add(&global_value, 1);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -25,15 +26,18 @@ int main()
|
|||||||
{
|
{
|
||||||
// Create 5 threads
|
// Create 5 threads
|
||||||
pthread_t thr_id[5];
|
pthread_t thr_id[5];
|
||||||
for(int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++)
|
||||||
if(pthread_create(&thr_id[i], NULL, thread_routine, NULL) == -1) {
|
{
|
||||||
|
if (pthread_create(&thr_id[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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for all threads to finish
|
// Wait for all threads to finish
|
||||||
for(int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
pthread_join(thr_id[i], NULL);
|
pthread_join(thr_id[i], NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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.pthreads_race_atomic</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