I “get” parallelism just fine…
While you can have multiple threads accessing different parts of the same data
set at the same time, you can’t have them accessing the same parts of the same
data set. You need to code your program to avoid such situations. One way to
do that, though often not the best way, is using mutexes.
As far as global / passed state is concerned, if the underlying system is
using a global state then you still need that mutex whether you like it or
not.
You could make a passed state API on top of it, but every time you call a
function in that API you need to lock a mutex, check your state handle, set
the state if necessary, perform your operation and unlock the mutex. If you
don’t use a mutex, then between checking the state and performing your
operation, some other thread may change the state and screw things up.
And yes, this is serial:
void Thread1(void* param) {
while (running) {
SDL_mutexP((SDL_mutex*)param);
fart_loudly();
SDL_mutexV((SDL_mutex*)param);
}
}
void Thread2(void* param) {
while(running){
SDL_mutexP((SDL_mutex*)param);
inhale();
SDL_mutexV((SDL_mutex*)param);
}
}
This is not:
void Thread1(void* param) {
while (running) {
SDL_mutexP((SDL_mutex*)param);
fart_loudly();
SDL_mutexV((SDL_mutex*)param);
belch();
}
}
void Thread2(void* param) {
while(running){
SDL_mutexP((SDL_mutex*)param);
inhale();
SDL_mutexV((SDL_mutex*)param);
exhale();
}
}
Nor is this:
void Thread1(void* param) {
while (running) {
SDL_mutexP(((SDL_mutex*)param)[0]);
fart_loudly();
SDL_mutexV(((SDL_mutex*)param)[0]);
SDL_mutexP(((SDL_mutex*)param)[1]);
belch();
SDL_mutexV(((SDL_mutex*)param)[1]);
}
}
void Thread2(void* param) {
while(running){
SDL_mutexP(((SDL_mutex*)param)[0]);
inhale();
SDL_mutexV(((SDL_mutex*)param)[0]);
SDL_mutexP(((SDL_mutex*)param)[1]);
exhale();
SDL_mutexV(((SDL_mutex*)param)[1]);
}
}
This is (more or less), but it’s useful anyway:
void Thread1(void* param) {
while (running) {
SDL_mutexP((SDL_mutex*)param);
fart_loudly();
SDL_mutexV((SDL_mutex*)param);
}
}
void Thread2(void* param) {
while(running){
SDL_mutexP((SDL_mutex*)param);
inhale();
SDL_mutexV((SDL_mutex*)param);
SDL_Delay(30);
}
}On Monday, 6 July 2009 22:39:07 Mason Wheeler wrote:
…you don’t “get” parallelism at all, do you? Yes, there are some
advantages to having completely different tasks running in different
threads. Sound is a perfect example. But the biggest benefit comes from
doing the same thing in multiple places at the same time. For example,
Google manages to return a nice, prompt search result from a data set that
contains pretty much the entire Internet by breaking the search down into
thousands of smaller parts and having a few thousand computers each process
a bit of it at the same time. This is an extreme example, of course, but
it illustrates the true power of parallelism. And that sort of computing
is just not possible with shared global state.
The only time I ever use mutexes or critcal sections is to deliberately
block a thread for a non-trivial amount of time. For example, in my game
engine, if two threads are both running scripts and both scripts want to
open a text box, there’s only one text box to open, so it’s protected by a
critical section. One of them displays its message, and the other script
has to wait its turn. But dropping a heavyweight lock into a pair of loops
is just asking for trouble.