473,496 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

threads question


assuming code:

int i=0;//global variable

//process 1 //process 2
int num=0; int num=0;
while(i<5){ while(i<5){
num=num+1; num=num+1;
i=i+1; i=i+1;
} }

what is the range of values for num?
it is supposed to be from 0-15, but i dont know why- any thoughts?

Jul 23 '05 #1
3 1339
<ja************@gmail.com> wrote...
[...]


Threads are off-topic here since C++ does not define anything thread-
related in its Standard. Try comp.programming.threads.
Jul 23 '05 #2
ja************@gmail.com wrote:
assuming code:

int i=0;//global variable

//process 1 //process 2
int num=0; int num=0;
while(i<5){ while(i<5){
num=num+1; num=num+1;
i=i+1; i=i+1;
} }

what is the range of values for num?
it is supposed to be from 0-15, but i dont know why- any thoughts?


Mr. Bazarov is correct as usual, threads are not part of C++ and are
therefore off-topic in a strict sense. However, I'll answer just to
illustrate why threads are tricky in C++ (heck, any language) because
there are no threading primitives...

First, the easy case, suppose process 1 runs to completion (exits the
loop) before process 2 ever gets started. When process 2 finally
starts, i is already equal to 5 so the while loop never executes and num
remains 0.

Now, how do we get a num equal to 15?

Suppose process 1 runs first and gets to the point mid way through
executing the i = i+1 statement. It has read the value (0) of i in to a
register and is about to add 1 to it when its time slice is up and it is
interrupted.

Now process 2 runs for a while, and lets say it loops 5 times, with i
having values:

0, 1, 2, 3, 4

Then just before it can execute the i = i+1 statement, or even after it,
but before it can perform the relational test in the while loop... Its
time slice is up and process 1 runs again.

Process 1 still has the value 0 in the register, its context is
restored, and it resets i to 1 now (not 4 or 5 as process 2 had left it).

Now, in an evenly scheduled round-robin (for same priority levels)
system you'd assume that process 1 would now run to completion. But
lets assume a really pathalogical case. Maybe process 1 gets a tiny
time slice before being interrupted by a higher priority process/thread,
just enough to loop exactly once back to where it was, then the round
robin scheduler moves back to process 2 since 1 had at least some time...

Now process 2 gets to run again. It loops with i values:

1, 2, 3, 4

Before once again giving up to process 1. Now process 1 runs again,
with 1 held in the register... It has the ill fortune to loop exactly
once more... Then process 2 runs again...

2, 3, 4

Then 1, and 2...

3, 4

Then finally 1 and 2 once more...

4

So, how many iterations has process 2 actually made through its loop? 15...

Admittedly, this is an extremely pathalogical case - one that you would
probably never be able to induce during testing, but which would almost
certainly occur in the first week of client/customer use. ;-)

It also illustrates just the tip of the iceberg when it comes to the
difficulties and pitfalls associated with multithreaded programming (or
multiprocess programming with shared memory or other resources). And as
Mr. Bazarov points out, C++ has no help for you. Even programming
languages such as Java with built in threading and synchronization
primitives won't think/design for you.
Jul 23 '05 #3
Phil Staite wrote:
Mr. Bazarov is correct as usual, threads are not part of C++ and are
therefore off-topic in a strict sense. However, I'll answer just to
illustrate why threads are tricky in C++ (heck, any language) because
there are no threading primitives...


Just a touch of theory: Threads are tricky in any language - even those that
claim to support them robustly - when they violate encapsulation. One thread
would prefer to ignore and be ignored by other threads. Instead, a thread
must often use semaphores that couple its private control flow details to
other threads' details.

(BTW - question for Phil: Did you submit a Bug++ of the Month last
millenium? I thought you did, but it's Google-proof.)

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 23 '05 #4

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

Similar topics

5
30657
by: Tzach | last post by:
I'm developing a simple Java client that runs over a CORBA server. The main client thread is waiting for notification from this server. On each notification, The client creates a new thread...
2
1452
by: grahamo | last post by:
Hi, I realise that c++ knows nothing about threads however my question is related to an (excellent) article I was reading about threads and C++. For all intents and purposes we can forget the...
10
1657
by: [Yosi] | last post by:
I would like to know how threads behavior in .NET . When an application create 4 threads for example start all of them, the OS task manager will execute all 4 thread in deterministic order manes,...
1
1712
by: Raoul Minder | last post by:
Hi all I am new to threading! I am developping a newsletter tool including a dispatch manager that should schedule sendings. The Web layer kicks a remote object hosted by a windows service...
14
3237
by: Lior Amar | last post by:
Quick question about threads and delegates. I have the following scenario Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a callback Thread B Invokes a DelegateB...
10
1490
by: cj | last post by:
I'm writing a TCP/IP server app that will have many simultaneous connections. The main thread listens for new connections and starts a thread to handle each requested connection. These are short...
7
3087
by: Michael | last post by:
I'm writing an application that decodes a file containing binary records. Each record is a particular event type. Each record is translated into ASCII and then written to a file. Each file contains...
1
1384
by: | last post by:
I'm trying to think something through and am wondering if may have some suggestions. I am building a Windows service (VStudio 2005, C#) that uses a COM component to answer a telephone call(s). ...
4
3993
by: gsimmons | last post by:
I've been researching multi-threaded WinForms apps and thread synchronization stuff for a couple days since I'm working on refactoring a multi-threaded GUI app at work and want to be sure it's...
1
4117
by: chunghorng_lung | last post by:
Hi All, I have a question on the scope of variables for threads. The program has a main thread which creates a few worker threads. The main thread can access another class stored in another...
0
7120
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
7196
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...
1
6878
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...
0
7373
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...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4583
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...
0
1405
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 ...
0
286
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...

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.