简体   繁体   中英

How to check if memory is available for read/write operation?

I am trying to write character values in a memory which I define using malloc() and simultaneously read a character value from it. For this I define the memory globally and then start a thread. In thread I am writing character value in memory and In main() I am reading value from it. Here is my code:-

char *str = (char *) malloc(90000);
DWORD WINAPI Thread_no_1( LPVOID lpParam ) 
{
    int a=1;
    int c=0;
 LOOP:do
   {
        str[c] = 'a';
            c++;
            goto LOOP;
   }while( a < 2 );

    return 0;
}

int main()
{
int b=0;
char value;

int Data_Of_Thread_1 = 1;
HANDLE Handle_Of_Thread_1 = 0;
Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1, &Data_Of_Thread_1, 0, NULL);  
if ( Handle_Of_Thread_1 == NULL)
ExitProcess(Data_Of_Thread_1);

    while(1);
    {
    value = str[b];
    printf("%c",value);;
    b++;
    }

    return 0;
}

Now, when I run this code I got this error:- 在此处输入图片说明 It seems that I can not read and write simultaneously. So, my question is , how can I check if memory is available for read and for write the values?

 LOOP:do
   {
        str[c] = 'a';
            c++;
            goto LOOP;
   }while( a < 2 );

This is an infinite loop, goto LOOP; is called on each iteration with no chance to check while(a < 2)

No such thing like "can not read and write simultaneously". Different threads are executed separately. Even on 2 cores, the access to memory is handled by controller and there is no problem with "simultaneous" access. But as said Alter Mann, there is no exit from the loop in 2nd thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM