爱问知识人 爱问教育 医院库

C 多线程问题

首页

C++ 多线程问题

如何在C++中实现多线程?比如说在一个线程在屏幕上打印1到100的每个数字,第2个线程在屏幕上打印从200到300的每个数字,最好有完整的程序,谢谢啦:)

提交回答
好评回答
  • 2006-05-04 11:59:42
      class CThreadParam
    {
    private:
            int                m_iCount ;
            BOOL        m_bRun ;
            CRITICAL_SECTION m_csCriticalSection ;
            CMainFrame * m_pFrame ;
    public:
            CThreadParam() ;
            virtual ~CThreadParam() ;
            int StepIt() ;
            BOOL IsRun() ;
            int Start(CMainFrame *pFrame) ;
            int Finished () ;
    private:
            int Enter() ;
            int Leave() ;
    };
    // CMainFrame 消息处理程序
    void CMainFrame::OnThreadBegin()
    {
            if ( !m_tpGather。
      IsRun() ) { m_tpGather。Start(this) ; } AfxBeginThread(GatherThread, &m_tpGather) ; } void CMainFrame::OnThreadEnd() { m_tpGather。
      Finished() ; } CThreadParam::CThreadParam() { InitializeCriticalSection(&m_csCriticalSection) ; m_bRun = FALSE ; m_iCount = 0 ; } CThreadParam::~CThreadParam() { DeleteCriticalSection(&m_csCriticalSection) ; } int CThreadParam::StepIt() { int iPrevCount = m_iCount ; Enter() ; m_iCount++ ; Leave() ; // 这部分代码也应该和前面的思路是一样的,由视图来提供方法进行更新,但这样做就太过复杂了 // 我这不是实际工程,所以在这里为了节省时间对于视图的算法不做演示了 ((Ctest3View*)m_pFrame->GetActiveView())->m_iCount = iPrevCount ; ((Ctest3View*)m_pFrame->GetActiveView())->Invalidate() ; return iPrevCount ; } int CThreadParam::Enter() { EnterCriticalSection(&m_csCriticalSection); return 0 ; } int CThreadParam::Leave() { LeaveCriticalSection(&m_csCriticalSection); return 0 ; } BOOL CThreadParam::IsRun() { return m_bRun ; } int CThreadParam::Start(CMainFrame *pFrame) { m_bRun = TRUE ; m_iCount = 0 ; m_pFrame = pFrame ; return 0 ; } int CThreadParam::Finished() { m_bRun = FALSE ; return 0 ; } UINT GatherThread( LPVOID pParam ) { CThreadParam * tp = (CThreadParam *)pParam ; while ( tp->IsRun() ) { tp->StepIt() ; ::Sleep(2000) ; } return 0 ; } public: int m_iCount; 这是我在天极网的VC论坛回贴的一个关于线程的问题 里面正好也是一个最基本的线程控制代码。
      其中程序是基于SDI的,但给出的代码都是和线程有关的。其中主要是一些同步和互斥的东西。第一位朋友给如的代码并没有在printf时进行互斥,所以这样的多线程一定会出现问题,上面给出的代码是最基本的进出临界区的框架,写得不好,但大致就是那个意思。
      如果没有这部分代码而像第一位朋友那样写,多线程没有同步,就必然要运行中会出现问题。另外也可能是第一位朋友的疏忽,他在进行线程前malloc,但出去后没有free这也会导致明显的内存泄露。这一点儿也稍微有点不妥。

    d***

    2006-05-04 11:59:42

其他答案

    2006-04-28 19:56:56
  • 多线程的问题比较复杂,如果你是在Windows下编程,可以参考一本不错的书《Win32多线程程序设计》 侯捷译的。
    对于你所举的例子,两个线程交互输入的问题,楼上的程序其实是会有问题的。
    因为是两个程序交替打印,对于输出设备(这里是显示器)会存在一个资源的互斥问题。
    所以以上程序最好加一个metex进行同步操作,使得在输出每一行结果时对屏幕独享占用。不过在Linux或Unix下,不会出现同行两个线程混排的情况(与OS的输出设备设计有关)。
    其实这些问题在我一开始介绍的书里已经有很详尽的说明了。
    希望对你有用。
    

    1***

    2006-04-28 19:56:56

  • 2006-04-27 22:10:31
  •   连接的时候用libcmt。
      lib #define _MT #include #include #include #include #include void print(void * pStart) { int start = 0; memcpy(&start, pStart, sizeof(int)); for(int i = start; i < start + 101; i++) printf("%d\t", i); //next i return; }//end print int main(void) { int * a = (int *)malloc(sizeof(int)); *a = 1; int * b = (int *)malloc(sizeof(int)); *b = 200; _beginthread(&print, 0, a); _beginthread(&print, 0, b); getch(); return 0; }。

    黑***

    2006-04-27 22:10:31

类似问题

换一换
  • C/C++ 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...
最新资料 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 175-194
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):