简体   繁体   English

如何检查内存是否可用于读/写操作?

[英]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. 我正在尝试在使用malloc()定义的内存中写入字符值,并同时从中读取字符值。 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. 在线程中,我正在将字符值写入内存,而在main()中,我正在从中读取值。 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; 这是一个无限循环, goto LOOP; is called on each iteration with no chance to check while(a < 2) 每次迭代都被调用而没有机会检查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. 即使在2个内核上,对内存的访问也由控制器处理,并且“同时”访问没有问题。 But as said Alter Mann, there is no exit from the loop in 2nd thread. 但是正如Alter Mann所说,第二线程没有从循环中退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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