冲突原因
写CString的时候,可能内存不够,于是释放、重新分配内存。可能刚释放,没来来得及分配,就被挂起了,然后另外的线程访问此变量,就崩了。
测试说明
Button18测试了,没有防冲突,程序崩溃。Button19,测试了有防冲突处理,程序没崩溃。Button20,一个写锁若干个读锁,比全部写锁要快。SNMFC::CriticalReadWriteLock,自己整理的类库的类。
测试代码
class CTesNoLock { public: static UINT Write(LPVOID) { Sleep(1); s_str += _T("AB33333333333333333333"); return 0; } static UINT Read(LPVOID) { Sleep(1); bool b = s_str[0] == _T('A'); ASSERT(b); return 0; } protected: static CString s_str; }; CString CTesNoLock::s_str = "A"; class CTesReadWriteLock : public CTesNoLock { public: static UINT LockWrite(LPVOID) { s_lock.Lock(); Write(NULL); s_lock.UnLock(); return 0; } static UINT LockRead(LPVOID) { s_lock.LockRead(); Read(NULL); s_lock.UnLockRead(); return 0; } static UINT LockWrite100ms(LPVOID) { s_lock.Lock(); Sleep(100); s_lock.UnLock(); return 0; } static UINT LockRead100ms(LPVOID) { s_lock.LockRead(); Sleep(100); s_lock.UnLockRead(); return 0; } protected: static SNMFC::CriticalReadWriteLock s_lock; }; SNMFC::CriticalReadWriteLock CTesReadWriteLock::s_lock; void CSNMFCDlg::OnBnClickedButton18() { CTesNoLock::Write(NULL); for (int i = 0; i < 1000; i++) { AfxBeginThread(CTesNoLock::Write, NULL); AfxBeginThread(CTesNoLock::Read, NULL); } } void CSNMFCDlg::OnBnClickedButton19() { for (int i = 0; i < 10; i++) { AfxBeginThread(CTesReadWriteLock::LockWrite, NULL); for (int j = 0; j < 1000; j++) { AfxBeginThread(CTesReadWriteLock::LockRead, NULL); } } } void CSNMFCDlg::OnBnClickedButton20() { DWORD d1 = GetTickCount(); {//由于被锁,只能依次访问,用时约10秒 for (int i = 0; i < 99; i++) { AfxBeginThread(CTesReadWriteLock::LockWrite100ms, NULL); } } CTesReadWriteLock::LockWrite100ms(NULL); DWORD d2 = GetTickCount(); {//可以同时读,所以用时少的多 for (int i = 0; i < 99; i++) { AfxBeginThread(CTesReadWriteLock::LockRead100ms, NULL); } } CTesReadWriteLock::LockWrite100ms(NULL); DWORD d3 = GetTickCount(); ASSERT((d2 - d1) >(d3 - d2) * 2); }