急问,如何控制多个线程访问同一资源的问题?

zwhappy3 2006-07-31 03:54:51
创建了多个线程,但是他们都对同一资源进行访问,如何控制他们的互斥访问这一资源。
最好是给出一些代码,以及需要的头文件和库文件。谢谢!
...全文
295 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hdqqq 2006-08-02
  • 打赏
  • 举报
回复
模板写的一个线程间同步的适配器
http://blog.csdn.net/hdqqq/archive/2006/01/14/579680.aspx
一剑 2006-08-01
  • 打赏
  • 举报
回复
我也提供一个:)

/******************************************************************************
Module: Interlocked.h
Notices: Copyright (c) 2000 Jeffrey Richter
******************************************************************************/


#pragma once


///////////////////////////////////////////////////////////////////////////////


// Instances of this class will be accessed by multiple threads. So,
// all members of this class (except the constructor and destructor)
// must be thread-safe.
class CResGuard {
public:
CResGuard() { m_lGrdCnt = 0; InitializeCriticalSection(&m_cs); }
~CResGuard() { DeleteCriticalSection(&m_cs); }

// IsGuarded is used for debugging
BOOL IsGuarded() const { return(m_lGrdCnt > 0); }

public:
class CGuard {
public:
CGuard(CResGuard& rg) : m_rg(rg) { m_rg.Guard(); };
~CGuard() { m_rg.Unguard(); }

private:
CResGuard& m_rg;
};

private:
void Guard() { EnterCriticalSection(&m_cs); m_lGrdCnt++; }
void Unguard() { m_lGrdCnt--; LeaveCriticalSection(&m_cs); }

// Guard/Unguard can only be accessed by the nested CGuard class.
friend class CResGuard::CGuard;

private:
CRITICAL_SECTION m_cs;
long m_lGrdCnt; // # of EnterCriticalSection calls
};
CW_Wei 2006-08-01
  • 打赏
  • 举报
回复
up
思危 2006-08-01
  • 打赏
  • 举报
回复
最简单的是临界区
//Definition of critical section class
class CMyClass
{
static CString _strShared; //shared resource
static CCriticalSection _critSect;
public:
CMyClass(void);
~CMyClass(void);
void MyMethod(void); //locks, modifies, and unlocks shared resource
};

//Declaration of static members and SomeMethod
CString CMyClass::_strShared;
CCriticalSection CMyClass::_critSect;

void CMyClass::SomeMethod()
{
_critSect.Lock();
if (_strShared == "")
_strShared = "<text>";
_critSect.Unlock();
}
leon7909 2006-07-31
  • 打赏
  • 举报
回复
一般用临界区比较简单。给你个类用:
class Mutex {
public:
Mutex() {
InitializeCriticalSection(&crit);
}
~Mutex() {
DeleteCriticalSection(&crit);
}
friend class Lock;
protected:
void enter() {EnterCriticalSection(&crit);}
void exit() {LeaveCriticalSection(&crit);}
CRITICAL_SECTION crit;
};

class Lock {
public:
Lock(Mutex& m) : mutex(m) {m.enter();}
~Lock() {mutex.exit();}
protected:
Mutex& mutex;
};
使用时定议个全局的Mutex 实例:
Mutex g_mutex;
在要互斥的地方如下使用:
void DoSomething()
{
Lock l(g_mutex);
......
}
这样在l的生命期内就是互斥的内容。
DentistryDoctor 2006-07-31
  • 打赏
  • 举报
回复
简单的讲就是同步。
在进程内可以用临界区来同步。
折腾_苏州 2006-07-31
  • 打赏
  • 举报
回复
线程同步--使用信号量:
http://www.vckbase.com/document/viewdoc/?id=762

线程同步--使用互斥:
http://www.vckbase.com/document/viewdoc/?id=758
折腾_苏州 2006-07-31
  • 打赏
  • 举报
回复
http://www.vckbase.com/document/viewdoc/?id=758
zwhappy3 2006-07-31
  • 打赏
  • 举报
回复
各位大侠,请不吝赐教,谢谢先!

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧