473,799 Members | 2,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Ne wThreadWork);
Subscription.Cr eate //This relies on the New Thread running

Thanks
Oct 7 '08 #1
3 2003
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(Ne wThreadWork);
Subscription.Cr eate //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
2788
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, you've >provided next to no useful (IMHO) information to >let anyone help you more than this This is about 5% of the code. Uses no locks.
13
5097
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 only 1 thread per line Trunk_Thread.ApartmentState = ApartmentState.STA
37
8911
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 is code used for measuring:
6
5817
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 thread and run them will the local variables in functions be shared or will they be thread safe?
5
3807
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 mysql connections open so I end up with quite a few at the end. Is there a different way that I should do this? class Program { static string categories = { "emulation" , "audio" , "console" , "anime" , "xxx" , "tv" , "pictures" , "video" };
5
2807
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 stop at the middle of the something ?
2
1964
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 regularly to inform the main thread of the progress (which is then displayed to the user). Since the event is raised from another thread, i've used me.Invoke() to update the ui properly. However, my problem is in cancelling the process. I need to...
4
6221
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 Function */ template < class FunType, class ArgType> Thread makeThread(Funtype fun, ArgType arg)
0
2697
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 problem might be the way I'm passing the value. can someone please take a look at this code and maybe give me some guidance on what I'm doing wrong. Please note there are 3 separate files with the corresponding classes, I separated it with ===>...
0
1531
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 threading import Thread import thread class testThread(Thread): def __init__ (self, id): Thread.__init__(self) self.id = id def run(self):
0
9686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9540
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10475
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10222
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10026
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.