Why sometimes a statement after Thread.Start() is not executed?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I've found that sometime a statement after Thread.Start() is not executed.
My code are as follows:-

Sub Start()
' initialize threading value
...
_thrd.Start()

' new instance
_obj = new MyClass
End Sub

From code above, I've found that sometimes a statement "_obj = new MyClass"
is not executed. It's value is Nothing, when I've used it in a threading
module.

What I'm wrong to do this?

Thanks,
Thana N.
 
Thana N. said:
Hi,

I've found that sometime a statement after Thread.Start() is not executed.
My code are as follows:-

Sub Start()
' initialize threading value
...
_thrd.Start()

' new instance
_obj = new MyClass
End Sub

From code above, I've found that sometimes a statement "_obj = new
MyClass"
is not executed. It's value is Nothing, when I've used it in a threading
module.

If you mean that it's nothing when you're trying to access it from the other
thread, then it probably just hasn't been created yet. You could just move
the creation statement to before the second thread is started. If the
constructor is just a default constructor, then it's also possible that the
JIT has just optimized the statement out, since it would effectively do
nothing.
 
It is not possible for what you describe to occur. If you are trying to
manipulate _obj from within your thread, it needs to be passed in thru the
constructor. If the new myClass line fails, the run-time will throw an
exception.

--
Regards,
Alvin Bruney

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
 
Sean Hederman said:
If you mean that it's nothing when you're trying to access it from the other
thread, then it probably just hasn't been created yet.
Agreed.

You could just move
the creation statement to before the second thread is started. If the
constructor is just a default constructor, then it's also possible that the
JIT has just optimized the statement out, since it would effectively do
nothing.

No - invoking a default constructor is *not* the same as doing nothing.
Setting the value of a variable to be a reference to a new object is
very different from leaving the value of the variable as it was before.
 
Jon Skeet said:
No - invoking a default constructor is *not* the same as doing nothing.
Setting the value of a variable to be a reference to a new object is
very different from leaving the value of the variable as it was before.

Sorry, I should have said "an empty default constructor". Would the JIT not
optimise away a constructor call that did nothing for a variable never used
again? Obviously it couldn't do this if there was any "real" work being done
in the call chain of the constructor, but if it was just, say initializing
an int variable, this *could* be optimized away, couldn't it?
 
Sean Hederman said:
Sorry, I should have said "an empty default constructor". Would the JIT not
optimise away a constructor call that did nothing for a variable never used
again?

I'm not sure - but I don't see any reason to think this variable is
never used again.
Obviously it couldn't do this if there was any "real" work being done
in the call chain of the constructor, but if it was just, say initializing
an int variable, this *could* be optimized away, couldn't it?

Only if the variable isn't going to be used - and seeing as the OP has
talked about the value of the variable being Nothing, that suggests
that the variable *is* used.
 
Jon Skeet said:
I'm not sure - but I don't see any reason to think this variable is
never used again.

Right you are. I just re-read the OP, and see that I have been mistaken
about the function. I had got it into my head that the function was the
Main() entry point.

Wrong!
 
*BUT* what you guys said does happen to my code. OK I'd like to explain what
my code do to you all. I write a TCP server to receive and send data. It will
create a thread when client has connected (1 thread/client). In a module that
start a thread, I also create an object to read/write data via TCP (which
pass NetworkStream to it) as you saw in my first message. When I use client
connect, sometimes it fails to connect, because server close connection. When
I check at the server I've found that sometime an instant does not create
(value is Nothing).

You're right I can move the code (new object) at the beginning of module.
*BUT* I'd like to know why it happens this way. And what happen if my code is
to start 3 threads, can I predict it behavior?

Thanks for your help.
Thana N.
 
Thana N. said:
*BUT* what you guys said does happen to my code. OK I'd like to explain what
my code do to you all. I write a TCP server to receive and send data. It will
create a thread when client has connected (1 thread/client). In a module that
start a thread, I also create an object to read/write data via TCP (which
pass NetworkStream to it) as you saw in my first message. When I use client
connect, sometimes it fails to connect, because server close connection. When
I check at the server I've found that sometime an instant does not create
(value is Nothing).

You're right I can move the code (new object) at the beginning of module.
*BUT* I'd like to know why it happens this way. And what happen if my code is
to start 3 threads, can I predict it behavior?

You can't predict whether or not some code in your other thread will
execute before the code in the rest of your Start method, unless you
use a lock. If you lock on something during Start, and at the start of
each of your threads you lock on the same thing, then the other threads
won't be able to do anything until the Start method releases the lock -
but in that case, you might as well just not start the other threads
until the end of the Start method.

If you want to effectively pass parameters to the new thread, have a
look at
http://www.pobox.com/~skeet/csharp/threads/parameters.shtml
 
Back
Top