如何检查内存是否可用于 read/write 操作?
How to check if memory is available for read/write operation?
我正在尝试将字符值写入我使用 malloc()
定义的内存中,同时从中读取一个字符值。为此,我全局定义内存,然后启动一个线程。在线程中,我在内存中写入字符值,在 main() 中,我从中读取值。这是我的代码:-
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;
}
现在,当我 运行 这段代码时,我得到了这个错误:-
看来我不能同时读写。所以,我的问题是,如何检查内存是否可用于读取和写入值?
LOOP:do
{
str[c] = 'a';
c++;
goto LOOP;
}while( a < 2 );
这是一个无限循环,每次迭代都会调用 goto LOOP;
,没有机会检查 while(a < 2)
没有 "can not read and write simultaneously" 这样的东西。不同线程分别执行。即使在 2 核上,对内存的访问也由控制器处理,"simultaneous" 访问没有问题。但是正如 Alter Mann 所说,第二个线程中没有退出循环。
我正在尝试将字符值写入我使用 malloc()
定义的内存中,同时从中读取一个字符值。为此,我全局定义内存,然后启动一个线程。在线程中,我在内存中写入字符值,在 main() 中,我从中读取值。这是我的代码:-
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;
}
现在,当我 运行 这段代码时,我得到了这个错误:-
LOOP:do
{
str[c] = 'a';
c++;
goto LOOP;
}while( a < 2 );
这是一个无限循环,每次迭代都会调用 goto LOOP;
,没有机会检查 while(a < 2)
没有 "can not read and write simultaneously" 这样的东西。不同线程分别执行。即使在 2 核上,对内存的访问也由控制器处理,"simultaneous" 访问没有问题。但是正如 Alter Mann 所说,第二个线程中没有退出循环。