Compare commits
2 commits
e9b4f8e389
...
d9089c4348
Author | SHA1 | Date | |
---|---|---|---|
d9089c4348 | |||
dc89fc4ce2 |
1 changed files with 14 additions and 12 deletions
26
main.cpp
26
main.cpp
|
@ -18,10 +18,10 @@ typedef void *(*ThreadTask)(const struct thread_data &);
|
||||||
typedef void *(*PthreadFun)(void *arg);
|
typedef void *(*PthreadFun)(void *arg);
|
||||||
|
|
||||||
struct thread_group {
|
struct thread_group {
|
||||||
bool wants_mutex[NUM_THREADS] = {0};
|
bool wants_mutex[NUM_THREADS] = {0}; // set by the thread
|
||||||
bool has_mutex[NUM_THREADS] = {0};
|
bool has_mutex[NUM_THREADS] = {0}; // set by us, read by thread
|
||||||
bool manager_took_mutex[NUM_THREADS] = {0};
|
bool can_ask_for_mutex[NUM_THREADS] = {1}; // set by us, read by thread
|
||||||
bool threads_finished[NUM_THREADS] = {0};
|
bool threads_finished[NUM_THREADS] = {0}; // set by thread
|
||||||
ThreadTask task;
|
ThreadTask task;
|
||||||
size_t total_threads;
|
size_t total_threads;
|
||||||
void *data;
|
void *data;
|
||||||
|
@ -31,7 +31,7 @@ struct thread_data {
|
||||||
size_t id;
|
size_t id;
|
||||||
bool *wants_mutex;
|
bool *wants_mutex;
|
||||||
const bool *has_mutex;
|
const bool *has_mutex;
|
||||||
const bool *manager_took_mutex;
|
const bool *can_ask_for_mutex;
|
||||||
bool *is_finished;
|
bool *is_finished;
|
||||||
void *data;
|
void *data;
|
||||||
};
|
};
|
||||||
|
@ -55,8 +55,10 @@ void *thread_task_increment(const struct thread_data &thread) {
|
||||||
// mutex
|
// mutex
|
||||||
{
|
{
|
||||||
|
|
||||||
// tell the thread manager we want the mutex
|
// wait for permission to ask for the mutex
|
||||||
while (!*thread.manager_took_mutex)
|
while (!*thread.can_ask_for_mutex)
|
||||||
|
;
|
||||||
|
// say we want the mutex
|
||||||
(*thread.wants_mutex) = true;
|
(*thread.wants_mutex) = true;
|
||||||
// block until we have the mutex
|
// block until we have the mutex
|
||||||
while (!*thread.has_mutex)
|
while (!*thread.has_mutex)
|
||||||
|
@ -103,8 +105,8 @@ void do_threading(struct thread_group threads) {
|
||||||
threads.total_threads * sizeof(*threads.threads_finished));
|
threads.total_threads * sizeof(*threads.threads_finished));
|
||||||
|
|
||||||
// at the start, we have the mutex and haven't given it
|
// at the start, we have the mutex and haven't given it
|
||||||
memset(threads.manager_took_mutex, 1,
|
memset(threads.can_ask_for_mutex, 1,
|
||||||
threads.total_threads * sizeof(*threads.manager_took_mutex));
|
threads.total_threads * sizeof(*threads.can_ask_for_mutex));
|
||||||
|
|
||||||
// create thread data
|
// create thread data
|
||||||
for (size_t tid = 0; tid < threads.total_threads; ++tid) {
|
for (size_t tid = 0; tid < threads.total_threads; ++tid) {
|
||||||
|
@ -112,7 +114,7 @@ void do_threading(struct thread_group threads) {
|
||||||
.id = tid,
|
.id = tid,
|
||||||
.wants_mutex = &threads.wants_mutex[tid],
|
.wants_mutex = &threads.wants_mutex[tid],
|
||||||
.has_mutex = &threads.has_mutex[tid],
|
.has_mutex = &threads.has_mutex[tid],
|
||||||
.manager_took_mutex = &threads.manager_took_mutex[tid],
|
.can_ask_for_mutex = &threads.can_ask_for_mutex[tid],
|
||||||
.is_finished = &threads.threads_finished[tid],
|
.is_finished = &threads.threads_finished[tid],
|
||||||
.data = threads.data,
|
.data = threads.data,
|
||||||
};
|
};
|
||||||
|
@ -152,9 +154,9 @@ void do_threading(struct thread_group threads) {
|
||||||
|
|
||||||
// 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;
|
||||||
threads.manager_took_mutex[tid_has] = true;
|
threads.can_ask_for_mutex[tid_has] = true;
|
||||||
// give the mutex to the thread that wants it
|
// give the mutex to the thread that wants it
|
||||||
threads.manager_took_mutex[tid_wants] = false;
|
threads.can_ask_for_mutex[tid_wants] = false;
|
||||||
threads.has_mutex[tid_wants] = true;
|
threads.has_mutex[tid_wants] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue