Skip to main content
gamedev.net
🔒 Locked

Multithreading

Started by d000hg 4 replies 200+ views
Original Post
d000hg
d000hg
OK, what''s the deal with making your code multithread-safe? Does any variable that may be accessed by >1 thread need to be protected, or just those that could potentially be written to by multiple threads ie can you sfely read from somewhere without any protection? Does the protection stuff simply give more reliable results or will the app crash if two threads try to use the same memory simultaneously? Since we all have single-CPU systems how can two threads do this anyway? How do I make variables/functions safe for multiple threads?
Read about my game, project #1 NEW (7th August): demo #3 is now released, with a new screenshot.
John 3:16
Kippesoep
Kippesoep
It won''t crash because of the access (although if it''s a pointer variable and the contents are changed by one thread, dereferencing it will result in undefined behaviour). Take a look at the C standard library. Functions such as printf use internal buffers. Calling printf from two different threads simultaneously will cause boths calls to use the same buffer. Serious problem.

In general, reading/writing single, simple variables should be okay, although you might want to make them volatile. If you access a larger block of data, then you should use the critical section stuff.


Kippesoep
dalleboy
dalleboy
Lets say you have a function that looks like this:

    
void foo()
{
static int iSomeValue = 0;
if (iSomeValue == 0)
{
iSomeValue = 1;
bar();
iSomeValue = 0;
}
}

Lets now say we have to theads that simultaneously(*) runs the function.
The first thread executes to the if-statement, it sees that iSomeValue has the value of 0 and thus the if-statement is true.
The OS comes in and switches to the second thread.
The second thread executes to the if-statement, it sees that iSomeValue has the value of 0 and thus the if-statement is true. It continues to execute and sets iSomeValue to 1 and calls the bar-function. In the middle of the bar-function the OS comes in, one again, and switches to the first thread again.
The first thread sets iSomeValue to 1 and calls the bar-function. The bar-function isn't reentrant and thus the results could be unexpected.

* Threads on a single CPU system does not run simultaneously, each thread is given a timeslice that it has access to the CPU. When the timeslice is over, another thread is selected and given a timeslice.

EDIT1: Added note.

[edited by - dalleboy on August 11, 2002 7:19:02 AM]
d000hg
d000hg
So generally you can have multiple threads with no safety stuff, e.g a world update thread and and separate render thread?

What exactly is a critical section, and what does it look like in code?



Read about my game, project #1
NEW (7th August): demo #3 is now released, with a new screenshot.


John 3:16
TangentZ
TangentZ
May I suggest "Multithreading Applications in Win32" and/or
"Programmingn with POSIX Threads"? Do a search on Amazon.com.
They are both pretty good books for writing multithreaded
programs on their respective platform.

Personally, I use POSIX Threads (pthreads) since it''s
cross-platform. I use the pthreads-win32 library so it''s
no problem.



Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
dmounty
dmounty
http://www.relisoft.com/win32/active.html

This is a very nice article on encapsulating the Thread and mutex paradigms, to avoid deadlock...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.