I have an InterlockedExchangeAdd function within my bank object's transaction method which I've been told solves that problem? |
It's impossible for me to say whether or not it addresses the problem without seeing that function, but based on what I'm seeing in this code... I highly doubt it does.
In fact, I see you doing this in the support thread:
1 2
|
int account1 = (rand()%bank->numAccounts) + 90000000;
int account2 = (rand()%bank->numAccounts) + 90000000;
|
numAccounts is a member of the bank object, which is being shared across multiple threads. Reading these variables without any guard (as you are doing) is ok
only if no other thread will be writing to them. If any thread will be writing to them, then
all reads and writes need to be guarded.
You do similar reads with bankBal.
Additionally...
numTransfers ++;
This is a read+write... so if multiple threads are accessing the same 'numTransfers' var (ie: if it's global, which it appears to be), then this
also needs to be protected.
How do I shut down a thread then once it's running but I want it to stop? |
You
return
out of the thread's function. Once that function exits the thread stops.
I would presume what I'd be looking for is something along the lines of:
1 2 3 4 5
|
while(!close)
{
one.join();
}
one.finish();
|
|
You do not want to put this inside of thread 'one'. one.join() tells
the current thread to wait for 'one' to finish. If you call that from within the 'one' thread... it'll either deadlock (the thread is waiting for itself to finish) or it'll have some other equally terrible behavior.
It's actually pretty simple:
1 2 3 4 5 6 7 8 9
|
void yourThreadHandler() // <- the function called for this thread
{
while(running) // <- (this is a bad example... see below)
{
// do thread work
}
// as soon as 'running' is false, the loop exits and the function returns
// so this thread = closed.
}
|
But of course... it's not as simple as
while(running)
because 'running' has to be a variable shared between multiple threads, which means
it needs to be guarded.
The two common ways to do this are:
1) Put accesses behind a mutex (see the <mutex> header)
or
2) Make it atomic (see the <atomic> header)
I'm not going to get into details of these as this is quite a large topic. I will say that multithreading in C++ is definitely not a "beginner" task because it's
very easy to get wrong... and when it goes wrong, it's
very hard to find and fix the bugs.
Also I think the reason for using this multithreading is just to show us how additional threads effects processing speed. |
Ack... is this a school assignment? *shudder*
Edit: Although the likelihood of them accessing the same variables is quite slim, there's a couple of thousand variables and each function call will pick a random one of them :) |
Maybe so. But that's exactly why thread-safety bugs are so hard to find and fix: Your program works just fine 95% of the time... except for the 5% of the time where it does really weird crap and you have no idea why.