C ++ - Should the data transferred to the stream be unstable?

In Microsoft Visual C ++, I can call CreateThread () to create a thread by running a function with one parameter void *. I pass a pointer to the structure as this parameter, and I see that many others are doing this.

My question is: if I pass a pointer to my structure, how do I know if structure elements were actually written to memory before calling CreateThread ()? Is there any guarantee that they will not be simply cached? For instance:

struct bigapple { string color; int count; } apple;
apple.count = 1;
apple.color = "red";
hThread = CreateThread( NULL, 0, myfunction, &apple, 0, NULL );

DWORD WINAPI myfunction( void *param )
{
    struct bigapple *myapple = (struct bigapple *)param;

    // how do I know that apple struct was actually written to memory before CreateThread?
    cout << "Apple count: " << myapple->count << endl; 
}

, , Windows - , , , , , - . , ++ , , " ", , - . , , & apple CreateThread(), , apple.

+5
5

. Win32 . CreateThread . , CreateThread.

volatile . .

+5

, volatile. . Intel/ARM/etc.

, . . , .

, , , , ​​ . .

volatile . , ( ..).

+2

-, , . CreateThread() - , binidng .

-, . .

+1

...

  • , CreateThread: , , CreateThread. , , , .
  • , : new, , delete ( : !)
  • , ( !). , .
  • API, CreateThread, , . C-runtime _beginthreadex. CreateThread ++, . C ( ++) , . Unsing CreateThread malloc , delete .

bnehavior

// create the data
// create the other thread
// // perform othe task
// wait for the oter thread to terminate
// destroy the data

API win32 , HANDLE - . ,

WaitForSingleObject(hthread,INFINITE);

, :

{
    data* pdata = new data;
    HANDLE hthread = (HANDLE)_beginthreadex(0,0,yourprocedure, pdata,0,0);
    WaitForSingleObject(htread,INFINITE);
    delete pdata;
}

{
    data d;
    HANDLE hthread = (HANDLE)_beginthreadex(0,0,yourprocedure, &d,0,0);
    WaitForSingleObject(htread,INFINITE);
}
0

, . , ( ).

, , ( ), . , .

Edit: I think the examples on the wiki page are a good explanation of http://en.wikipedia.org/wiki/Volatile_variable

0
source

All Articles