我采用临界区的线程同步方式进行了线程同步,方法如下:// 临界区结构对象
CRITICAL_SECTION g_cs;
//全局共享变量
list<CRecord> stList;//启动线程前进行初始化临界区
InitializeCriticalSection(&g_cs);在线程中:
CRecord stRec;
stRec.id = id;
// 进入临界区
EnterCriticalSection(&g_cs);
stList.push_back(stRec);
// 离开临界区
LeaveCriticalSection(&g_cs);在不用到共享变量的地方释放临界区:
DeleteCriticalSection( &g_cs);编译结果为:
函数InitializeCriticalSection错误为:
Unicode identifiers are not yet supported
undeclared identifier
error : missing ';' before identifier 'InitializeCriticalSection'函数EnterCriticalSection错误为:
Unicode identifiers are not yet supported
syntax error : missing ';' before identifier 'EnterCriticalSection'
identifier not found, even with argument-dependent lookup函数LeaveCriticalSection错误为:
Unicode identifiers are not yet supported
syntax error : missing ';' before identifier 'LeaveCriticalSection'
identifier not found, even with argument-dependent lookup函数DeleteCriticalSection编译通过。我使用的编译环境为vc.net2003!
CRITICAL_SECTION g_cs;
//全局共享变量
list<CRecord> stList;//启动线程前进行初始化临界区
InitializeCriticalSection(&g_cs);在线程中:
CRecord stRec;
stRec.id = id;
// 进入临界区
EnterCriticalSection(&g_cs);
stList.push_back(stRec);
// 离开临界区
LeaveCriticalSection(&g_cs);在不用到共享变量的地方释放临界区:
DeleteCriticalSection( &g_cs);编译结果为:
函数InitializeCriticalSection错误为:
Unicode identifiers are not yet supported
undeclared identifier
error : missing ';' before identifier 'InitializeCriticalSection'函数EnterCriticalSection错误为:
Unicode identifiers are not yet supported
syntax error : missing ';' before identifier 'EnterCriticalSection'
identifier not found, even with argument-dependent lookup函数LeaveCriticalSection错误为:
Unicode identifiers are not yet supported
syntax error : missing ';' before identifier 'LeaveCriticalSection'
identifier not found, even with argument-dependent lookup函数DeleteCriticalSection编译通过。我使用的编译环境为vc.net2003!
EnterCriticalSection和LeaveCriticalSection
就有问题了;
如果我在这个一般的函数中有对全局变量的操作怎么实现同步呢?
注明:这个一般的函数是在线程函数中调用的!