0

也许这听起来很愚蠢,但我想在运行时动态创建一个向量/SRWLocks 数组。下面的代码编译两种方式,但数组的东西不起作用。可能问题应该是,我可以使用 new 运算符创建一个 SRWLock 对象吗?

如果这不是它的完成方式,那么创建 SRWLock 对象数组的正确方法是什么?

#include "stdafx.h"
#include <ppl.h>
#include <Windows.h>
#include <iostream>

using namespace std;

#define NOT_WORKING

int _tmain(int argc, _TCHAR* argv[])
{


#ifdef NOT_WORKING

// The following doesn't work
       SRWLOCK *lock = new SRWLOCK[2];

       int a = 10;

       AcquireSRWLockExclusive(&lock[0]);
       cout<<"Exclusive Lock Acquired";
       a++;
       ReleaseSRWLockExclusive(&lock[0]);


       // The following doesn't work either

       /*
       SRWLOCK *lock_n = new SRWLOCK;


       AcquireSRWLockExclusive(lock_n);
       cout<<"Exclusive Lock Acquired";
       a++;
       ReleaseSRWLockExclusive(lock_n);

       */

       #else
// This works
       SRWLOCK lock;

       int a = 10;
       AcquireSRWLockExclusive(&lock);
       cout<<"Exclusive Lock Acquired";
       a++;
       ReleaseSRWLockExclusive(&lock);

#endif
       cout<<"Value of a:"<<a;
       cin.get();

       return 0;
}
4

1 回答 1

0

User1577008 自己的回答:知道了!动态创建锁对象时需要使用 InitializeSRWLock() 函数。有人粘贴这个答案,并帮助我结束这个问题。

于 2012-08-05T07:44:18.967 回答