Click here to Skip to main content
15,922,696 members
Home / Discussions / C#
   

C#

 
GeneralRe: Statistical Functions in C# Pin
leppie6-Oct-03 12:31
leppie6-Oct-03 12:31 
GeneralRe: Statistical Functions in C# Pin
Donald_a8-Oct-03 3:40
Donald_a8-Oct-03 3:40 
Generalabout lock Pin
yyf6-Oct-03 5:52
yyf6-Oct-03 5:52 
GeneralRe: about lock Pin
jparsons6-Oct-03 6:21
jparsons6-Oct-03 6:21 
GeneralRe: about lock Pin
Daniel Turini6-Oct-03 6:30
Daniel Turini6-Oct-03 6:30 
GeneralRe: about lock Pin
jparsons6-Oct-03 6:36
jparsons6-Oct-03 6:36 
GeneralRe: about lock Pin
yyf6-Oct-03 7:39
yyf6-Oct-03 7:39 
GeneralRe: about lock Pin
jparsons6-Oct-03 13:02
jparsons6-Oct-03 13:02 
yyf wrote:
1: status = new object(0);
2: lock ( status )
3: {
4: status = status1;
5: }


I've added line numbers for clarification

This function will not work as you expect it to. For example assume I have two instances of MyClass m1 and m2. Each are created in a seperate thread. m1 executes lines 1-4 and then there is a thread switch. The problem is now the lock is on the previous object status pointed to and there is virtually no way to retrieve that object. Now m2 comes along and executes line 1 and 2. It is locking a completely new object so even though the thread containing m1 is currently inside the lock, the thread containing m2 will proceed into the code block. Now event though status1 will be set correctly in your case, the code is misleading and incorrect.

The easier way to do this is through a property and a locking object.

<br />
public class MyClass<br />
{<br />
  private static object Sync = new object();<br />
  public static object _status = null;<br />
<br />
  public static object status<br />
  {<br />
    get <br />
    {  <br />
      lock ( Sync )<br />
      { <br />
        if ( _status == null ) <br />
        { <br />
          _status = new object();<br />
        }<br />
      }<br />
      return _status;<br />
    }<br />
   }<br />
   /* Rest of Class */<br />
}<br />


Or you can just use a static constructor.

Jared
jparsons@jparsons.org
www.prism.gatech.edu/~gte477n
GeneralRe: about lock Pin
yyf7-Oct-03 6:03
yyf7-Oct-03 6:03 
QuestionA datatype/some way for 256 bit values? Pin
Fang@Illusion6-Oct-03 5:42
Fang@Illusion6-Oct-03 5:42 
AnswerRe: A datatype/some way for 256 bit values? Pin
Daniel Turini6-Oct-03 6:37
Daniel Turini6-Oct-03 6:37 
GeneralRe: A datatype/some way for 256 bit values? Pin
Fang@Illusion6-Oct-03 7:55
Fang@Illusion6-Oct-03 7:55 
AnswerRe: A datatype/some way for 256 bit values? Pin
leppie6-Oct-03 7:06
leppie6-Oct-03 7:06 
GeneralRe: A datatype/some way for 256 bit values? Pin
jparsons6-Oct-03 5:42
jparsons6-Oct-03 5:42 
GeneralRe: A datatype/some way for 256 bit values? Pin
Fang@Illusion8-Oct-03 9:01
Fang@Illusion8-Oct-03 9:01 
GeneralDirectoryTreeView with CheckBoxes Pin
j-hannemann6-Oct-03 4:30
j-hannemann6-Oct-03 4:30 
GeneralGetting CheckBox control in DataGrid Pin
brian41906-Oct-03 3:51
brian41906-Oct-03 3:51 
GeneralContext Menu Pin
.gonad6-Oct-03 3:31
.gonad6-Oct-03 3:31 
GeneralRe: Context Menu Pin
mistery226-Oct-03 10:13
mistery226-Oct-03 10:13 
GeneralRe: Context Menu Pin
.gonad6-Oct-03 12:43
.gonad6-Oct-03 12:43 
GeneralIcons in a c# gui Pin
Big Trev6-Oct-03 2:14
Big Trev6-Oct-03 2:14 
GeneralRe: Icons in a c# gui Pin
netclectic6-Oct-03 3:42
netclectic6-Oct-03 3:42 
GeneralRe: Icons in a c# gui Pin
mistery226-Oct-03 10:19
mistery226-Oct-03 10:19 
GeneralcomboBox.SelectedIndex in Win2000 vs. XP Pin
misiek6-Oct-03 2:08
misiek6-Oct-03 2:08 
GeneralConfiguration files Pin
Configuration programmer6-Oct-03 1:36
sussConfiguration programmer6-Oct-03 1:36 

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.