Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C WaitOnAddress在值匹配或值不同时返回吗?_C_Winapi - Fatal编程技术网

C WaitOnAddress在值匹配或值不同时返回吗?

C WaitOnAddress在值匹配或值不同时返回吗?,c,winapi,C,Winapi,我正在尝试使用WaitOnAddress()函数来实现读写同步 ,WaitOnAddress()具有以下声明: BOOL WaitOnAddress( 易失性空*地址, PVOID比较裙, 大小\u T地址大小, 德沃德 ); 以及以下参数定义: 比较服装 指向先前观察到的值在地址处的位置的指针。当地址处的值与比较地址处的值不同时,函数返回 根据定义,我应该存储并传递监视地址的当前值,这样当监视值更改时,WaitOnAddress()将返回。但是这个函数没有像我预期的那样工作,所以我在Visu

我正在尝试使用
WaitOnAddress()
函数来实现读写同步

WaitOnAddress()
具有以下声明:

BOOL WaitOnAddress(
易失性空*地址,
PVOID比较裙,
大小\u T地址大小,
德沃德
);
以及以下参数定义:

比较服装

指向先前观察到的值在
地址
处的位置的指针。当
地址
处的值与
比较地址
处的值不同时,函数返回

根据定义,我应该存储并传递监视地址的当前值,这样当监视值更改时,
WaitOnAddress()
将返回。但是这个函数没有像我预期的那样工作,所以我在Visual Studio中为
WaitOnAddress()
编写了以下测试代码(还包括链接器库
Synchronization.lib
):

#包括
#包括//atoi
#包括
//在包含winsock2.h之后包含这两个文件
#包括
#包括
#包括
void*线程(void*num){
int*number=(int*)num;
对于(int i=0;i<10;i++){
printf(“编号=%d\n”,*编号);
}
int catch=*number;//将作为CompareAddress传递
WaitOnAddress((int*)num,&catch,sizeof(int),无穷大);
对于(int i=0;i<10;i++){
printf(“number2=%d,%d\n”,catch,*(int*)num);
}
返回NULL;
}
int main(){
国际清单[10];
对于(int i=0;i<10;i++){
int_列表[i]=i;
_beginthread(threads,0,(void*)&int_list[i]);
}
printf(“hello\n”);
睡眠(1000);
对于(int i=0;i<10;i++){
国际清单[i]=10-i;
}
printf(“已更改\n”);
睡眠(1000);
返回0;
}
在上面的代码中,
WaitOnAddress()
永远不会返回,即使它监视的值发生了更改。但是,如果我改变

int catch=*number;
致:

int catch=10-*个;
然后
WaitOnAddress()

但是,我希望使用所描述的行为,以便在监视的变量更改时释放线程。

根据:

参数

地址

要等待的地址。如果地址处的值与 值,则函数立即返回。如果 如果值相同,则函数在另一个线程之前不会返回 在同一过程中,发出信号表明地址处的值已更改 调用WakeByAddressSingle或WakeByAddressAll或超时 以先到者为准

尽管在第二个循环中修改了原始数组的值,但在线程中调用
WaitOnAddress
时,
catch
num
始终相同,因此
WaitOnAddress
函数不会返回

您可以尝试将其修改为:

#include <stdio.h> 
#include <stdlib.h>  // atoi 
#include <string.h> 

// include these two files after including winsock2.h
#include <process.h>
#include <Windows.h>

void* threads(void* num) {
    int* number = (int*)num;
    printf("number = %d\n", *number);
    int catch = *number; // will be passed as CompareAddress
    WaitOnAddress((int*)num, &catch, sizeof(int), INFINITE);
    printf("number2 = %d, %d\n", catch, *(int*)num);
    return NULL;
}

int main() {
    int int_list[10];
    for (int i = 0; i < 10; i++) {
        int_list[i] = i;
        _beginthread(threads, 0, (void*)&int_list[i]);
    }
    printf("hello\n");
    Sleep(1000);
    printf("changed\n");
    for (int i = 0; i < 10; i++) {
        int_list[i] = 10 - i;
        WakeByAddressSingle(&int_list[i]);
    }
    Sleep(1000);
    return 0;
}
#包括
#包括//atoi
#包括
//在包含winsock2.h之后包含这两个文件
#包括
#包括
void*线程(void*num){
int*number=(int*)num;
printf(“编号=%d\n”,*编号);
int catch=*number;//将作为CompareAddress传递
WaitOnAddress((int*)num,&catch,sizeof(int),无穷大);
printf(“number2=%d,%d\n”,catch,*(int*)num);
返回NULL;
}
int main(){
国际清单[10];
对于(int i=0;i<10;i++){
int_列表[i]=i;
_beginthread(threads,0,(void*)&int_list[i]);
}
printf(“hello\n”);
睡眠(1000);
printf(“已更改\n”);
对于(int i=0;i<10;i++){
国际清单[i]=10-i;
WakeByAddressSingle(&int_list[i]);
}
睡眠(1000);
返回0;
}
这对我很有用:

根据:

参数

地址

要等待的地址。如果地址处的值与 值,则函数立即返回。如果 如果值相同,则函数在另一个线程之前不会返回 在同一过程中,发出信号表明地址处的值已更改 调用WakeByAddressSingle或WakeByAddressAll或超时 以先到者为准

尽管在第二个循环中修改了原始数组的值,但在线程中调用
WaitOnAddress
时,
catch
num
始终相同,因此
WaitOnAddress
函数不会返回

您可以尝试将其修改为:

#include <stdio.h> 
#include <stdlib.h>  // atoi 
#include <string.h> 

// include these two files after including winsock2.h
#include <process.h>
#include <Windows.h>

void* threads(void* num) {
    int* number = (int*)num;
    printf("number = %d\n", *number);
    int catch = *number; // will be passed as CompareAddress
    WaitOnAddress((int*)num, &catch, sizeof(int), INFINITE);
    printf("number2 = %d, %d\n", catch, *(int*)num);
    return NULL;
}

int main() {
    int int_list[10];
    for (int i = 0; i < 10; i++) {
        int_list[i] = i;
        _beginthread(threads, 0, (void*)&int_list[i]);
    }
    printf("hello\n");
    Sleep(1000);
    printf("changed\n");
    for (int i = 0; i < 10; i++) {
        int_list[i] = 10 - i;
        WakeByAddressSingle(&int_list[i]);
    }
    Sleep(1000);
    return 0;
}
#包括
#包括//atoi
#包括
//在包含winsock2.h之后包含这两个文件
#包括
#包括
void*线程(void*num){
int*number=(int*)num;
printf(“编号=%d\n”,*编号);
int catch=*number;//将作为CompareAddress传递
WaitOnAddress((int*)num,&catch,sizeof(int),无穷大);
printf(“number2=%d,%d\n”,catch,*(int*)num);
返回NULL;
}
int main(){
国际清单[10];
对于(int i=0;i<10;i++){
int_列表[i]=i;
_beginthread(threads,0,(void*)&int_list[i]);
}
printf(“hello\n”);
睡眠(1000);
printf(“已更改\n”);
对于(int i=0;i<10;i++){
国际清单[i]=10-i;
WakeByAddressSingle(&int_list[i]);
}
睡眠(1000);
返回0;
}
这对我很有用:


在父线程更改所有值之前,如何确保线程已到达
WaitOnAddress
?您可以显示输出日志吗?从您链接到的文档中可以看到:“函数不会返回,直到同一进程中的另一个线程通过调用WakeByAddressSingle或WakeByAddressAll发出地址值已更改的信号,或者超时时间已过,以先到者为准”@kaylum谢谢提醒我。向上的