Advertisement

04.30.2008 at 06:02PM PDT, ID: 23367376
[x]
Attachment Details

Synchronization question

Asked by CyrexCore2k in C Programming Language, C++ Programming Language

I've been trying to figure out when and where a context switch can occur. I thought I understood it but a program I wrote seems to be proving me wrong.

Here's the setup. I've declared a global unsigned int val and in my main method I give it the value 2097152. I also created a global volatile char called hold which I've set to false in the main method. I then create 10 threads and have them run the following function:

void * thread_work(void * parameters)
{
   while(hold) {}
   val = val / 2;
   return NULL;
}

As soon as all the threads are created main will set hold to some value that evaluates to false. (I did this because I didn't want any threads executing while I'm still creating the others.)

Here's my question, why am I consistently getting the result 2048? Shouldn't I be getting random power of 2 values between that and 1048576? I thought the code might execute something like this:

thread1 moves memory value into register
thread1 divides value (lets say it's the first one so 1048576)
*context switch*
thread2 moves memory value into register (still 2097152)
thread2 divides value (lets say it's the first one so 1048576)
thread2 moves register value back into memory
*context switch*
thread1 moves register value back into memory

Which would make the result 4096 at least... so why isn't this happening? What am I misunderstanding here?Start Free Trial
[+][-]04.30.2008 at 06:09PM PDT, ID: 21475810

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.30.2008 at 07:45PM PDT, ID: 21476240

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.30.2008 at 07:49PM PDT, ID: 21476263

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.30.2008 at 07:54PM PDT, ID: 21476273

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.30.2008 at 08:07PM PDT, ID: 21476302

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.30.2008 at 08:56PM PDT, ID: 21476430

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.01.2008 at 01:18AM PDT, ID: 21477090

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.01.2008 at 02:41AM PDT, ID: 21477355

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.01.2008 at 03:51AM PDT, ID: 21477574

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.01.2008 at 09:03AM PDT, ID: 21479904

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C Programming Language, C++ Programming Language
Sign Up Now!
Solution Provided By: billtouch
Participating Experts: 4
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906