Compare commits
No commits in common. "1043680b195f0ba03f893a2aa9ebce8d0d7bf0c2" and "d9089c4348ce69c2bbb61c162e4ee4a3a90c1db5" have entirely different histories.
1043680b19
...
d9089c4348
1 changed files with 0 additions and 22 deletions
22
main.cpp
22
main.cpp
|
@ -5,7 +5,6 @@
|
||||||
|
|
||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#include <cstring> // memset
|
#include <cstring> // memset
|
||||||
#include <ctime>
|
|
||||||
#include <iostream> // cout
|
#include <iostream> // cout
|
||||||
#include <vector> // vector
|
#include <vector> // vector
|
||||||
|
|
||||||
|
@ -53,8 +52,6 @@ void *thread_task_increment(const struct thread_data &thread) {
|
||||||
<< "! (inside run loop, before mutex)" << std::endl;
|
<< "! (inside run loop, before mutex)" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
usleep(rand() % 200);
|
|
||||||
|
|
||||||
// mutex
|
// mutex
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -63,7 +60,6 @@ void *thread_task_increment(const struct thread_data &thread) {
|
||||||
;
|
;
|
||||||
// say we want the mutex
|
// say we want the mutex
|
||||||
(*thread.wants_mutex) = true;
|
(*thread.wants_mutex) = true;
|
||||||
usleep(rand() % 200);
|
|
||||||
// block until we have the mutex
|
// block until we have the mutex
|
||||||
while (!*thread.has_mutex)
|
while (!*thread.has_mutex)
|
||||||
;
|
;
|
||||||
|
@ -73,8 +69,6 @@ void *thread_task_increment(const struct thread_data &thread) {
|
||||||
std::cout << "Hello from thread " << thread.id
|
std::cout << "Hello from thread " << thread.id
|
||||||
<< "! (inside run loop, inside mutex)" << std::endl;
|
<< "! (inside run loop, inside mutex)" << std::endl;
|
||||||
|
|
||||||
usleep(rand() % 200);
|
|
||||||
|
|
||||||
*static_cast<int *>(thread.data) += 1;
|
*static_cast<int *>(thread.data) += 1;
|
||||||
|
|
||||||
// tell the thread manager we don't need the mutex
|
// tell the thread manager we don't need the mutex
|
||||||
|
@ -129,11 +123,9 @@ void do_threading(struct thread_group threads) {
|
||||||
|
|
||||||
// spawn threads (none will enter the mutex yet)
|
// spawn threads (none will enter the mutex yet)
|
||||||
for (size_t tid = 0; tid < threads.total_threads; ++tid) {
|
for (size_t tid = 0; tid < threads.total_threads; ++tid) {
|
||||||
usleep(rand() % 200);
|
|
||||||
pthread_create(&my_pthreads[tid], NULL,
|
pthread_create(&my_pthreads[tid], NULL,
|
||||||
reinterpret_cast<PthreadFun>(thread_task_increment),
|
reinterpret_cast<PthreadFun>(thread_task_increment),
|
||||||
&thread_data[tid]);
|
&thread_data[tid]);
|
||||||
usleep(rand() % 200);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Threads have been spawned." << std::endl;
|
std::cout << "Threads have been spawned." << std::endl;
|
||||||
|
@ -141,17 +133,14 @@ void do_threading(struct thread_group threads) {
|
||||||
// loop until all threads are done
|
// loop until all threads are done
|
||||||
for (size_t finished_threads = 0; finished_threads < threads.total_threads;) {
|
for (size_t finished_threads = 0; finished_threads < threads.total_threads;) {
|
||||||
|
|
||||||
usleep(rand() % 200);
|
|
||||||
// TODO: make sure we cycle the mutex through threads round-robin style
|
// TODO: make sure we cycle the mutex through threads round-robin style
|
||||||
|
|
||||||
// hand off the mutex to threads that want it
|
// hand off the mutex to threads that want it
|
||||||
for (size_t tid_wants = 0; tid_wants < threads.total_threads; ++tid_wants) {
|
for (size_t tid_wants = 0; tid_wants < threads.total_threads; ++tid_wants) {
|
||||||
usleep(rand() % 200);
|
|
||||||
|
|
||||||
if (threads.wants_mutex[tid_wants]) {
|
if (threads.wants_mutex[tid_wants]) {
|
||||||
// in case the mutex isn't used at all
|
// in case the mutex isn't used at all
|
||||||
bool mutex_was_found = false;
|
bool mutex_was_found = false;
|
||||||
usleep(rand() % 200);
|
|
||||||
|
|
||||||
// find which thread has the mutex and hand it off if it's done
|
// find which thread has the mutex and hand it off if it's done
|
||||||
for (size_t tid_has = 0; tid_has < threads.total_threads; ++tid_has) {
|
for (size_t tid_has = 0; tid_has < threads.total_threads; ++tid_has) {
|
||||||
|
@ -159,22 +148,16 @@ void do_threading(struct thread_group threads) {
|
||||||
if (threads.has_mutex[tid_has]) {
|
if (threads.has_mutex[tid_has]) {
|
||||||
// we found who has the mutex!
|
// we found who has the mutex!
|
||||||
mutex_was_found = true;
|
mutex_was_found = true;
|
||||||
usleep(rand() % 200);
|
|
||||||
|
|
||||||
// is the thread still using the mutex?
|
// is the thread still using the mutex?
|
||||||
if (!threads.wants_mutex[tid_has]) {
|
if (!threads.wants_mutex[tid_has]) {
|
||||||
|
|
||||||
usleep(rand() % 200);
|
|
||||||
// take the mutex from the thread that has it
|
// take the mutex from the thread that has it
|
||||||
threads.has_mutex[tid_has] = false;
|
threads.has_mutex[tid_has] = false;
|
||||||
usleep(rand() % 200);
|
|
||||||
threads.can_ask_for_mutex[tid_has] = true;
|
threads.can_ask_for_mutex[tid_has] = true;
|
||||||
usleep(rand() % 200);
|
|
||||||
// give the mutex to the thread that wants it
|
// give the mutex to the thread that wants it
|
||||||
threads.can_ask_for_mutex[tid_wants] = false;
|
threads.can_ask_for_mutex[tid_wants] = false;
|
||||||
usleep(rand() % 200);
|
|
||||||
threads.has_mutex[tid_wants] = true;
|
threads.has_mutex[tid_wants] = true;
|
||||||
usleep(rand() % 200);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break; // no need to look at the rest if we found who has the mutex
|
break; // no need to look at the rest if we found who has the mutex
|
||||||
|
@ -183,10 +166,8 @@ void do_threading(struct thread_group threads) {
|
||||||
|
|
||||||
// give the thread the mutex if it wasn't found to be in use
|
// give the thread the mutex if it wasn't found to be in use
|
||||||
if (!mutex_was_found) {
|
if (!mutex_was_found) {
|
||||||
usleep(rand() % 200);
|
|
||||||
threads.has_mutex[tid_wants] = true;
|
threads.has_mutex[tid_wants] = true;
|
||||||
}
|
}
|
||||||
usleep(rand() % 200);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,7 +176,6 @@ void do_threading(struct thread_group threads) {
|
||||||
for (size_t tid = 0; tid < threads.total_threads; ++tid) {
|
for (size_t tid = 0; tid < threads.total_threads; ++tid) {
|
||||||
if (threads.threads_finished[tid])
|
if (threads.threads_finished[tid])
|
||||||
finished_threads += 1;
|
finished_threads += 1;
|
||||||
usleep(rand() % 200);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -210,8 +190,6 @@ void do_threading(struct thread_group threads) {
|
||||||
int main(void) {
|
int main(void) {
|
||||||
struct thread_group mythreads;
|
struct thread_group mythreads;
|
||||||
|
|
||||||
srand(time(NULL));
|
|
||||||
|
|
||||||
// the count
|
// the count
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue