Multithreading Synchronisation
<p>I'm trying to write a multithreaded graphics manipulation program using Borland's C++ Builder 6 on WinXP SP3, but have run into (I think) a synchronisation issue, and can't figure out why.</p>

<p>Main Form (Form1) has a TPicture loaded from file. A copy of this is acquired by the thread via a Synchronize() call, and works fine. The thread does some work on the image, and in theory, it periodically updates the main Form image. The main Form also controls a machine, and is a 'First Resort' emergency stop, so blocking isn't an option. Everything is fine until the main Form gets hold of the working copy, or a copy of the working copy (sorry, but it's got to that) at which point the program hangs, and is only responsive to a 'program reset' from the IDE. A poor solution is to copy the working image to the Clipboard, and then, from the main Form, copy from the Clipboard to the main Form's image.</p>

<pre><code>        //Synchronization routines:
//----------------------------------------------------------------
`void __fastcall ImageRout::update()
{
Form1-&gt;Image9-&gt;Picture-&gt;Bitmap-&gt;Assign(Imgcopy);
//never returns
}
//----------------------------------------------------------------
void __fastcall ImageRout::getimage()
{
    Imgcopy-&gt;Assign(Form1-&gt;Image9-&gt;Picture);
}
//----------------------------------------------------------------

//do the initialisation things... Then,
//(data is a struct, loaded with image data via a Synchronize() call)
Imgcopy=new Graphics::TBitmap;
Imgcopy-&gt;Width=data.width;
Imgcopy-&gt;Height=data.height;    //size the bitmap
while(Imgcopy-&gt;Canvas-&gt;LockCount!=1)
{
    Imgcopy-&gt;Canvas-&gt;TryLock();
}  //have to Lock() the image or it gets lost... Somewhere
Synchronize(getimage);  //works fine

//do some work on Imgcopy

//"By the book"- attempt 1
//(rate (=15) is a 'brake' to stop every alteration being displayed)
update_count++;
if(update_count&gt;rate)   //after a few iterations, update
{           //user interface
     Synchronize(update);  //fails: never returns from Synchronize call
     update_count=0;
}           
</code></pre>

<p>After a lot of failed attempts, I came up with this.</p>

<pre><code>//in the thread...

update_count++;
if(update_count&gt;rate)
{
     EnterCriticalSection(&amp;Form1-&gt;mylock1);
     Form1-&gt;tempimage-&gt;Assign(Imgcopy);        //tempimage is another bitmap, 
     InterlockedExchange(&amp;Form1-&gt;imageready,1);//declared in the main Form
     LeaveCriticalSection(&amp;Form1-&gt;mylock1);    //and is only ever accessed
     update_count=0;                           //inside a critical section
}

//...and in the main Form....

if(imageready==1)
{
     EnterCriticalSection(&amp;mylock1);
     Image9-&gt;Picture-&gt;Bitmap-&gt;Assign(tempimage);     //Fails here
     InterlockedExchange(&amp;gotimage,1);
     InterlockedExchange(&amp;imageready,0);
     LeaveCriticalSection(&amp;mylock1);
}
</code></pre>

<p>So, in desperation.</p>

<pre><code>//in the thread...
update_count++;
if(update_count&gt;rate)
{
     Synchronize(update);
     EnterCriticalSection(&amp;Form1-&gt;mylock1);
     Form1-&gt;tempimage-&gt;Assign(Imgcopy);
     Clipboard()-&gt;Assign(Imgcopy);
     InterlockedExchange(&amp;Form1-&gt;imageready,1);
     LeaveCriticalSection(&amp;Form1-&gt;mylock1);  */
     update_count=0;
}

//and in the main Form...
if(imageready==1)
{
     EnterCriticalSection(&amp;mylock1);
     if (Clipboard()-&gt;HasFormat(CF_BITMAP))
     {
          Image9-&gt;Picture-&gt;Bitmap-&gt;Assign(Clipboard());
     }
     InterlockedExchange(&amp;gotimage,1);
     InterlockedExchange(&amp;imageready,0);
     LeaveCriticalSection(&amp;mylock1);
}
</code></pre>

<p>This last attempt works, albeit relatively slowly, because of the Clipboard overhead, and it's a poor crutch, at best. I suspect the Clipboard is enforcing an otherwise failed synchronisation effort, but, as I said earlier, I can't fathom why. What can be the issue? </p>