Click here to Skip to main content
16,016,168 members
Home / Discussions / C#
   

C#

 
Questionusing MDF file without SQL Server Pin
Jassim Rahma18-Sep-09 4:40
Jassim Rahma18-Sep-09 4:40 
AnswerRe: using MDF file without SQL Server Pin
Dave Kreskowiak18-Sep-09 5:19
mveDave Kreskowiak18-Sep-09 5:19 
Questionpopup calendar when right clicked on Crystal Report Viewer Pin
Jassim Rahma18-Sep-09 4:37
Jassim Rahma18-Sep-09 4:37 
QuestionCrystal Reports with SQL Server step by step Pin
Jassim Rahma18-Sep-09 4:35
Jassim Rahma18-Sep-09 4:35 
QuestionCrossing Threads Pin
FJJCENTU18-Sep-09 4:30
FJJCENTU18-Sep-09 4:30 
AnswerRe: Crossing Threads Pin
Luc Pattyn18-Sep-09 4:44
sitebuilderLuc Pattyn18-Sep-09 4:44 
GeneralRe: Crossing Threads Pin
FJJCENTU18-Sep-09 5:19
FJJCENTU18-Sep-09 5:19 
GeneralRe: Crossing Threads Pin
Luc Pattyn18-Sep-09 5:44
sitebuilderLuc Pattyn18-Sep-09 5:44 
FJJCENTU wrote:
is needed that Method1 be Synchronized ?


Methods don't need to be synchronized, accesses to shared data should.
As I said: all data that is going to be read/written by several threads at once must be protected

Here is a simple example of how things CAN and eventually WILL go wrong:
List<int> power1=new List<int>();
List<int> power2=new List<int>();

public void Add(int x) {
    power1.Add(x);
    // point A
    power2.Add(x*x);
}


Then two threads are created that generate random numbers x and call Add, hoping to obtain a list of numbers, and in parallel a list of their squares. Everything will run fine; suddenly the operating system will decide to temporarily halt thread 1 (which was at point A), and resume thread 2.

So the lists will contain
power1  a    b    c    x1     x2   ...
power2  a*a  b*b  c*c  x2*x2  ...

and it is only when thread1 becomes active again that x1*x1 gets added to list power2.

The solution could be like this:
object powerLock=new object();

public void Add(int x) {
    lock(powerLock) {
        power1.Add(x);
        // point A
        power2.Add(x*x);
    }
}

so whenever some thread is inside the critical region protected by powerLock, no other thread needing the data protected by powerLock can but in.


FJJCENTU wrote:
a third Method is needed...


as I said, it is not methods that get protected, it is data. And the example should make that clear.

Smile | :)

Luc Pattyn

Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

Local announcement (Antwerp region): Lange Wapper? Neen!


Questionquestions about an array? Pin
JollyMansArt18-Sep-09 4:14
JollyMansArt18-Sep-09 4:14 
AnswerRe: questions about an array? Pin
harold aptroot18-Sep-09 4:20
harold aptroot18-Sep-09 4:20 
GeneralRe: questions about an array? Pin
JollyMansArt18-Sep-09 4:33
JollyMansArt18-Sep-09 4:33 
GeneralRe: questions about an array? Pin
Ian Shlasko18-Sep-09 4:37
Ian Shlasko18-Sep-09 4:37 
GeneralRe: questions about an array? Pin
harold aptroot18-Sep-09 4:38
harold aptroot18-Sep-09 4:38 
GeneralRe: questions about an array? Pin
Keith Barrow18-Sep-09 4:38
professionalKeith Barrow18-Sep-09 4:38 
GeneralRe: questions about an array? Pin
JollyMansArt18-Sep-09 4:44
JollyMansArt18-Sep-09 4:44 
GeneralRe: questions about an array? Pin
harold aptroot18-Sep-09 4:46
harold aptroot18-Sep-09 4:46 
GeneralRe: questions about an array? Pin
Keith Barrow18-Sep-09 4:53
professionalKeith Barrow18-Sep-09 4:53 
GeneralRe: questions about an array? Pin
harold aptroot18-Sep-09 4:57
harold aptroot18-Sep-09 4:57 
GeneralRe: questions about an array? Pin
Richard MacCutchan18-Sep-09 5:03
mveRichard MacCutchan18-Sep-09 5:03 
GeneralRe: questions about an array? Pin
harold aptroot18-Sep-09 5:05
harold aptroot18-Sep-09 5:05 
GeneralRe: questions about an array? Pin
Keith Barrow18-Sep-09 5:15
professionalKeith Barrow18-Sep-09 5:15 
GeneralRe: questions about an array? Pin
harold aptroot18-Sep-09 5:26
harold aptroot18-Sep-09 5:26 
GeneralTerinary commands then what is wrong with this example? As I am learning to use them... Pin
JollyMansArt18-Sep-09 5:41
JollyMansArt18-Sep-09 5:41 
GeneralRe: Terinary commands then what is wrong with this example? As I am learning to use them... Pin
harold aptroot18-Sep-09 5:47
harold aptroot18-Sep-09 5:47 
GeneralRe: Terinary commands then what is wrong with this example? As I am learning to use them... Pin
JollyMansArt18-Sep-09 5:55
JollyMansArt18-Sep-09 5:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.