EDIT: Using C++
Edited by EnigmaticProgrammer, 17 May 2012 - 01:44 AM.
Posted 16 May 2012 - 07:18 PM
Edited by EnigmaticProgrammer, 17 May 2012 - 01:44 AM.
Posted 16 May 2012 - 07:24 PM
Posted 16 May 2012 - 10:03 PM
Posted 16 May 2012 - 11:03 PM
x = 1; y = 2; z = x + y; x = 4;
Posted 17 May 2012 - 01:07 AM
The game I'm working on tops at 125fps on target machines using 80% of a core. On lower spec machines, it usually runs about 60 fps with ease. The game is simple. It will be a while before I'll need multithreading.I've not used multithreading before but I think it's time I start.
Posted 17 May 2012 - 01:54 AM
Posted 17 May 2012 - 02:11 AM
Posted 17 May 2012 - 02:59 AM
Pop quiz! Two threads run this code:
x = 1; y = 2; z = x + y; x = 4;
What's the value of z?
If you answered "3" you are in for a world of nasty surprises. If you confidently answered "maybe 3 and maybe 6" you're in for even worse surprises.
volatile int x = 0;
volatile int y = 0;
volatile int z = 0;
void mythread() {
for(;;) {
x = 1;
y = 2;
z = x + y;
x = 4;
}
}
void main() {
unordered_map<int, bool> printedSoFar;
volatile thread threads[] = {
thread(mythread),
thread(mythread)
};
for(;;) {
int currentZ = z;
auto &printed = printedSoFar[currentZ];
if(!printed) {
cout << currentZ << endl;
printed = true;
}
}
}
Posted 17 May 2012 - 03:53 AM
When I tested it, it printed 3 99.99% of the time, but not 100%, which just means that the race condition is very unlikely in my test circumstances... which doesn't mean anything; there's still a race condition present.The only thing it ever prints is 3.
Edited by Hodgman, 17 May 2012 - 04:03 AM.
Posted 17 May 2012 - 05:13 AM
I would guess 3 or 6, but seems I cannot reproduce that in artificial situation:
volatile int x = 0; volatile int y = 0; volatile int z = 0; void mythread() { for(;;) { x = 1; y = 2; z = x + y; x = 4; } } void main() { unordered_map<int, bool> printedSoFar; volatile thread threads[] = { thread(mythread), thread(mythread) }; for(;;) { int currentZ = z; auto &printed = printedSoFar[currentZ]; if(!printed) { cout << currentZ << endl; printed = true; } } }
The only thing it ever prints is 3. Using volatile because release build destroys loop in mythread(). Debug mode without volatile prints only 3 as well.
Posted 17 May 2012 - 07:39 AM
What's the value of z?
Posted 17 May 2012 - 10:37 AM
Posted 17 May 2012 - 11:42 AM
Posted 17 May 2012 - 12:01 PM
The game I'm working on tops at 125fps on target machines using 80% of a core. On lower spec machines, it usually runs about 60 fps with ease. The game is simple. It will be a while before I'll need multithreading.
I've not used multithreading before but I think it's time I start.
There's no such thing as "I think": there's the need for performance. Or not. Until you need the performance, just practice it outside of main code base.
Posted 18 May 2012 - 07:24 PM
Edited by dublindan, 19 May 2012 - 01:24 PM.
Posted 19 May 2012 - 11:53 AM
The only thing it ever prints is 3. Using volatile because release build destroys loop in mythread(). Debug mode without volatile prints only 3 as well.
Posted 24 May 2012 - 07:11 PM
Posted 24 May 2012 - 07:53 PM
Dude no offense but I have read your few recent posts and you don't seem to know what you are talking about (as well as having a relatively spammy behaviour). Threads don't run each other, they run in parallel and (optionally) share data (which may require synchronization, but also may not - if the data is read-only no synchronization is needed for instance). As for the time spent context-switching threads (is this what you meant by threads taking cpu time?), it is largely negligible unless you have hundreds of threads running in parallel in your own process (which is a bad idea anyhow).carefull!
Implement multithreading only if it benefits you.
Threads take CPU time. There are 45 processes on windows that take 0cpu time.
Your thread takes 50 or 100.
If your thread shares memory with thread that runs it, you must lock the threads and bring serial run between them.
Powerfull but edgy