注册 登录
编程论坛 C++教室

原子操作函数的问题,求大神指教

宋立鹏 发布于 2013-10-18 22:32, 849 次点击
程序代码:
#include <iostream>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
using namespace std;


volatile long g_nLoginCount;
unsigned int _stdcall Fun(void *pPM);
const int THREAD_NUM=50;
unsigned int _stdcall Fun(void *pPM)
{
    Sleep(1000);
    //g_nLoginCount++;
    InterLockedIncrement((LPLONG)&g_nLoginCount);
    Sleep(50);
    return 0;
}
int main()
{
    int num=20;
    while(num--)
    {
        g_nLoginCount=0;
    HANDLE handle[THREAD_NUM];
    for(int i=0;i<THREAD_NUM;i++)
    {
        handle[i]=(HANDLE)_beginthreadex(NULL,0,Fun,NULL,0,NULL);
    }
        WaitForMultipleObjects(THREAD_NUM,handle,TRUE,INFINITE);
        //printf("有%d个用户登录后记录结果是%dn",THREAD_NUM,g_nLoginCount);
        cout<<""<<THREAD_NUM<<"个用户登录后记录结果是"<<g_nLoginCount<<endl;
    }
        return 0;
   
}

 
运行结果显示error C3861: “InterLockedIncrement”: 找不到标识符,求大神指教
5 回复
#2
blueskiner2013-10-18 23:04
这不明摆着的吗?那个函数没定义。
#3
yuccn2013-10-19 00:59
找下那函数是那个头文件的,包含尽量,
查询下msdn吧
#4
宋立鹏2013-10-19 08:32
回复 3楼 yuccn
我查了一下MSDN,应该包含的是"windows.h",但是我头文件上也包含的有"windows.h"啊,还是不行
#5
rjsp2013-10-19 08:36
程序代码:
#include <windows.h>
#include <process.h>
#include <iostream>
using namespace std;

volatile long g_nLoginCount = 0;

unsigned int __stdcall fun( void* )
{
    //Sleep(1000);
    InterlockedIncrement( &g_nLoginCount );
    //Sleep(50);

    return 0;
}

int main()
{
    for( size_t i=0; i<20; ++i )
    {
        g_nLoginCount = 0;

        HANDLE handles[50];
        for( size_t i=0; i<_countof(handles); ++i )
            handles[i] = (HANDLE)_beginthreadex(NULL,0,&fun,NULL,0,NULL);

        WaitForMultipleObjects(_countof(handles),handles,TRUE,INFINITE);

        for( size_t i=0; i<_countof(handles); ++i )
            CloseHandle( handles[i] );

        cout<<""<<_countof(handles)<<"个用户登录后记录结果是"<<g_nLoginCount<<endl;
    }

    return 0;
}
#6
宋立鹏2013-10-19 15:03
回复 5楼 rjsp
谢谢rjsp指点,我已经找到错误所在了,函数名字有一个字母写成大写了,汗颜
1