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