473,394 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Thread.Start

If I call Thread.Start am I guaranteed that thread will be running before the
call from Thread.Start returns?

i.e.

//Doing Something on main thread
Thread.Start(NewThreadWork);
Subscription.Create //This relies on the New Thread running

Thanks
Oct 7 '08 #1
3 1985
I'm pretty sure the tread will be active and scheduled but it might not have
had enough processor time to have gotten any serious work done. The whole
idea is that it will be scheduled independently of your thread that started
it.
If you want to synchronise them, look at using a WaitHandle

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"JT" wrote:
If I call Thread.Start am I guaranteed that thread will be running before the
call from Thread.Start returns?

i.e.

//Doing Something on main thread
Thread.Start(NewThreadWork);
Subscription.Create //This relies on the New Thread running

Thanks
Oct 7 '08 #2
On Tue, 07 Oct 2008 02:33:00 -0700, JT <JT@discussions.microsoft.com>
wrote:
If I call Thread.Start am I guaranteed that thread will be running
before the
call from Thread.Start returns?
No, and in fact it would be unusual for it to do so. The started thread
will be runnable, but it won't get any CPU time until its turn in the
round-robin scheduling.

The thread that created the new thread will, unless it has exhausted its
own quantum in the act of creating the new thread (a statistical
improbability, though of course not impossible), continue running until it
yields or uses up its quantum. And of course, assuming it didn't exhaust
its own quantum while creating the new thread, neither of those conditions
will happen before the call from Thread.Start() returns. In most cases,
the existing thread will have plenty of time to do other things before the
newly created thread ever gets to run.

As Ciaran says, if the existing thread is in some way dependent on the
newly created thread, you can use thread synchronization objects such as a
WaitHandle implementation to control execution of each thread. That said,
it's unusual to need that sort of thing immediately after creating the new
thread. Normally, if there's something that the existing thread depends
on, you might as well do that thing in the existing thread rather than
relying and waiting on the newly created thread to do it.

In other words, if you find yourself in a situation where this is
important, you may have a design problem that needs fixing. Unnecessary
thread synchronization tends to hurt performance and as a general rule
negates any benefits that might have been had by using a multi-threaded
design in the first place.

Pete
Oct 7 '08 #3
Thats kinda what I mean't, just put a whole load more articulately.

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Peter Duniho" wrote:
On Tue, 07 Oct 2008 02:33:00 -0700, JT <JT@discussions.microsoft.com>
wrote:
If I call Thread.Start am I guaranteed that thread will be running
before the
call from Thread.Start returns?

No, and in fact it would be unusual for it to do so. The started thread
will be runnable, but it won't get any CPU time until its turn in the
round-robin scheduling.

The thread that created the new thread will, unless it has exhausted its
own quantum in the act of creating the new thread (a statistical
improbability, though of course not impossible), continue running until it
yields or uses up its quantum. And of course, assuming it didn't exhaust
its own quantum while creating the new thread, neither of those conditions
will happen before the call from Thread.Start() returns. In most cases,
the existing thread will have plenty of time to do other things before the
newly created thread ever gets to run.

As Ciaran says, if the existing thread is in some way dependent on the
newly created thread, you can use thread synchronization objects such as a
WaitHandle implementation to control execution of each thread. That said,
it's unusual to need that sort of thing immediately after creating the new
thread. Normally, if there's something that the existing thread depends
on, you might as well do that thing in the existing thread rather than
relying and waiting on the newly created thread to do it.

In other words, if you find yourself in a situation where this is
important, you may have a design problem that needs fixing. Unnecessary
thread synchronization tends to hurt performance and as a general rule
negates any benefits that might have been had by using a multi-threaded
design in the first place.

Pete
Oct 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: phil | last post by:
And sorry I got ticked, frustrating week >And I could help more, being fairly experienced with >threading issues and race conditions and such, but >as I tried to indicate in the first place,...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
37
by: ales | last post by:
Hello, I have a problem with creation of new thread. The method .Start() of newly created thread delays current thread for 0 - 1 second. Cpu while delay occurs is about 5%. Any idea? Here...
6
by: Extremest | last post by:
I am new to threading and trying to figure some things out. Are all variables in a thread set to only that thread? Meaning if I create 2 instances of a class and then put each one in a different...
5
by: admin | last post by:
ok This is my main. Pretty much it goes through each category and starts up 4 worker threads that then ask for groups to gether from. My problem is that when the thread gets done it keeps the...
5
by: Alan T | last post by:
I will do several things in my thread: Copy a file to a location Update database record Read the file content Write the content to a log file If I call Thread.Abort(), it may be possible to...
2
by: Don | last post by:
How to stop a process which is running in a separate thread!!! I've got a class which performs some lengthy process in a background (separate) thread. And this lengthy process raises events...
4
by: jayesah | last post by:
Hi All, I am writting a Thread class with using pthread library. I have some problem in saving thread function type and argument type. How to make Thread class generic ? /* This is my global...
0
by: Buckaroo Banzai | last post by:
Hello, newbie here... I'm writing this program but when I click the start button which should initiate either the Hare or the Tortoise, it does not, this is the first time I use threads, so the...
0
by: Yue Fei | last post by:
I have a multi thread python code, threads can start immediately if I run on command line, but I can get them started right the way if I call the same code from C/C++. test code like this: from...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.