AOA AOA AOA Folding For Team 45 AOA Files Home Front Page Become an AOA Subscriber! UserCP Calendar Memberlist FAQ Search Forum Home


Go Back   AOA > Software > Programming and Assembly Language

Programming and Assembly Language Please read this Topic's rules!!

Reply
 
LinkBack Thread Tools Rate Thread
  #1 (permalink)  
Old 9th October, 2007, 02:59 AM
MrSeanKon's Avatar
OcBible Creator
 
Join Date: June 2005
Location: Where the streets have no name
Posts: 766

Global variables: Do you prefer them?

What's your opinion gizmo?
As you know (OcBible internals) I use them.
__________________
GRAB overclocking -->OcBible v1.55 Guidemania v1.21
CARDGAMES-->Jack v1.17 Deck v1.13 Preference v1.20

Last edited by MrSeanKon; 9th October, 2007 at 03:00 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 9th October, 2007, 10:15 AM
Gizmo's Avatar
Chief BBS Administrator
BassTeroids Champion, Global Player Champion, Aim & Fire Champion, Puzzle Maniax Champion, Othello Champion, Canyon Glider Champion, UFO Shoot Out Champion, Unicycle Challenge Champion, Zed Champion
 
Join Date: May 2003
Location: Webb City, Mo
Posts: 14,946
Send a message via ICQ to Gizmo Send a message via AIM to Gizmo Send a message via MSN to Gizmo Send a message via Yahoo to Gizmo Send a message via Skype™ to Gizmo

As a general rule, I tend to avoid globals. It's awfully easy to end up with things changing globals unexpectedly, particularly when you are writing multi-threaded code.
__________________
Avatar and sig graphic by Pitch. Subscribers!
Ask about a custom graphic or avatar today!
 
Later,
Gizmo
Thermal Diode Mod and Direct-Die Water Block
8-Cheetah 18GiB U-2 SCSI
MegaRAID Enterprise 1500/128MiB
Samsung SyncMaster 955DF
TTGI/Superflower TTS-520 PSU
 

 
AOA Team filesAOA Team wcgAOA Team fah
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 13th October, 2007, 08:38 AM
Favu's Avatar
AOA's resident barman
 
Join Date: October 2005
Location: /Wales/Abergavenny
Posts: 4,004
Send a message via ICQ to Favu Send a message via AIM to Favu Send a message via MSN to Favu

I tend to avoid them also, except where I really need them

otherwise it is safer and avoids much confusion not to use them
__________________
AOA Team fah
 

Custom 8-bit Sharp Z80 @ 4.194304 MHz
Reflective LCD @ 160 × 144
8 kByte S-RAM






Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 15th October, 2007, 06:27 AM
Áedán's Avatar
Chief Systems Administrator
 
Join Date: September 2001
Location: Europe
Posts: 12,248

I avoid globals where I can. As Gizmo points out, it's very easy to accidently change a global without realising. It also takes a long time to track down why things don't work properly, just because a global was used.
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 17th October, 2007, 01:47 AM
MrSeanKon's Avatar
OcBible Creator
 
Join Date: June 2005
Location: Where the streets have no name
Posts: 766

Quote:
Originally Posted by Gizmo View Post
particularly when you are writing multi-threaded code.
Could you be more specific??
Upload some source code for example (prefer C).
Cos I am writing the new edition of Data Modeling and uses more global variables.
To clarify Data Modeling has one stop button which terminates all running threads (some subroutines which calculate the approximation functions).
GKR as you know C# do not allow to bypass parameters in threads.
Or am I wrong?
__________________

Last edited by MrSeanKon; 17th October, 2007 at 01:49 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 17th October, 2007, 04:01 PM
Gizmo's Avatar
Chief BBS Administrator
BassTeroids Champion, Global Player Champion, Aim & Fire Champion, Puzzle Maniax Champion, Othello Champion, Canyon Glider Champion, UFO Shoot Out Champion, Unicycle Challenge Champion, Zed Champion
 
Join Date: May 2003
Location: Webb City, Mo
Posts: 14,946
Send a message via ICQ to Gizmo Send a message via AIM to Gizmo Send a message via MSN to Gizmo Send a message via Yahoo to Gizmo Send a message via Skype™ to Gizmo

Code:
struct
{
    nUsageCount = 0; //just some silly var
    HANDLE hThreadStopEvent = INVALID_HANDLE_VALUE; handle for stopping thread
} Resources;

void main(argc, arcv)
{
    TCHAR char;

    Resources.hThreadStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL); //create our stop event
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Thread, NULL, 0, NULL);  //create the first of our two threads
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Thread, NULL, 0 , NULL); //create the second of our two threads

    do
    {
        Sleep(1000); //sleep for one second
        Resources.nUsageCount++; //increment our shared var
        char = _gettchar(); //get a character from the keyboard
     } while (char != 'x'); //keep going unless it is an 'x'

    SetEvent(Resources.hThreadStopEvent); //Signal our threads to stop
    Sleep(1000); //wait so that they can exit
    return (0);
}    

void Thread()
{
    while(WAIT_TIMEOUT == WaitForSingleObject(Resources.hThreadStopEvent, 1000)) //keep looping until event fires
    {
        Resources.nUsageCount++; //increment counter
    }
    return; //bail out
}
Now, if you look at this code, you'll see that main(), and both child threads are all incrementing nUsageCount as part of their operation.

But when? What happens if two threads access nUsageCount at the same time?

It's instructive to remember how the increment operation is performed; typically, the machine will read the memory address into an internal register, add 1 to the value, and then write the value back out to memory.

That is three separate operations, and it is entirely possible in a multitasking OS like Windows (even on a single-threaded, single-core, single-cpu, e.g. a 486) that the thread could be preempted before it finishes those operations.

Assume that thread1 reads the memory address into an internal register, adds 1 to the value, and then gets preempted before it can write the value back out. Thread2 wakes up, does it's thing, and then control is handed back to Thread1. Thread1 is now writing INVALID data, because the information stored in its registers is based on a machine state that no longer exists. The problem gets even worse if you consider that the main thread is also looping and incrementing that counter.

Now, you've got a situation where increments to the counter are being lost, but there's no obvious way of being able to tell that. Throw in multiple independent cores all accessing memory at the same time, and split that variable in the address space so that it takes two bus cycles to retrieve the data, and all KINDS of problems can occur.

The solution here is to either maintain separate counters, and pass them directly to the thread when you create it, or wrap all access to the counter with a thread synchronization object like InterlockedExchange(). This can quickly become cumbersome, and all it takes is to forget to do that just one time, and all bets are off.

The Windows CreateThread call allows you to pass one parameter to the thread (typically a pointer to a structure of some kind), and I'd be very surprised if C# doesn't have something similar. I'm far from expert in C#, though. It is not capable of doing the things that I often need to do unless I want to jump through all kinds of hoops to call unprotected code, so I don't do much programming in it.
__________________
Avatar and sig graphic by Pitch. Subscribers!
Ask about a custom graphic or avatar today!
 
Later,
Gizmo
Thermal Diode Mod and Direct-Die Water Block
8-Cheetah 18GiB U-2 SCSI
MegaRAID Enterprise 1500/128MiB
Samsung SyncMaster 955DF
TTGI/Superflower TTS-520 PSU
 

 
AOA Team filesAOA Team wcgAOA Team fah

Last edited by Gizmo; 17th October, 2007 at 04:06 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 22nd October, 2007, 05:29 AM
MrSeanKon's Avatar
OcBible Creator
 
Join Date: June 2005
Location: Where the streets have no name
Posts: 766

Thanks for your effort gizmo I will ask at other forums.
__________________

Last edited by MrSeanKon; 30th October, 2007 at 02:09 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 11th November, 2007, 01:11 PM
Wolf2000me's Avatar
Member
 
Join Date: September 2004
Location: Belgium
Posts: 1,238

In the case of multithreaded operations one might use this in C#

Synchronized method access in C# - B# .NET Blog

I sometimes use this at work in Java and I don't know much about C#

I would suggest to create a kind of generic language Class and have the other classes inherit from that class. Set the global variables as Class variables in the generic class and add some get and set methods that are synchronized.
__________________
Asus Maximus Formula (X38)
Intel Q6600 3720Mhz
TT Mozart Tx
2x WD Raptors 74gb R0
2x Maxtor 300gb
Asus Geforce EN8800GT
Windoors XP Pro sp2

Download here OcBible 1.55 & Guidemania v1.21
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 15th November, 2007, 06:33 AM
MrSeanKon's Avatar
OcBible Creator
 
Join Date: June 2005
Location: Where the streets have no name
Posts: 766

Bonjour!
I will keep the URL you suggested.
But I will rewrite some parts later (time for more rest)
__________________
GRAB overclocking -->OcBible v1.55 Guidemania v1.21
CARDGAMES-->Jack v1.17 Deck v1.13 Preference v1.20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What do you prefer in a P4? alexkerhead Intel Motherboards & CPUs 8 23rd October, 2004 03:58 PM
Windows 2000 server login script and how to set variables Con Mobile Devices and Networking 0 9th January, 2003 08:19 AM
Anyone prefer reg UT to the new UT2k3? ZigZagZeppelin GAMES! OH YEAH! 13 28th September, 2002 11:16 AM
SuSE and environmental variables Kaitain OS, Software, Firmware, and BIOS 1 25th March, 2002 09:16 AM


All times are GMT -5. The time now is 11:06 PM.


Copyright ©2001 - 2009, AOA Forums

Search Engine Friendly URLs by vBSEO 3.3.0