| |||||||
| Programming and Assembly Language Please read this Topic's rules!! |
![]() |
| | LinkBack | Thread Tools | Rate Thread |
| ||||
| 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. |
| ||||
| 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! 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 ![]() ![]() ![]() |
| ||||
| 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. |
| ||||
| 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
}
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! 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 ![]() ![]() ![]() Last edited by Gizmo; 17th October, 2007 at 04:06 PM. |
| ||||
| 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 |
| ||||
| 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 |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Rate This Thread | |
| |
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 |