c++ - 这个函数是线程安全的吗?

标签 c++ multithreading mfc

我正在学习多线程,为了便于理解,我使用多线程编写了一个小函数......它工作正常。但我只想知道该线程是否可以安全使用,我是否遵循了正确的规则.

void CThreadingEx4Dlg::OnBnClickedOk()
{
    //in thread1 100 elements are copied to myShiftArray(which is a CStringArray)
    thread1 = AfxBeginThread((AFX_THREADPROC)MyThreadFunction1,this);
    WaitForSingleObject(thread1->m_hThread,INFINITE);
    //thread2 waits for thread1 to finish because thread2 is going to make use of myShiftArray(in which thread1 processes it first)
    thread2 = AfxBeginThread((AFX_THREADPROC)MyThreadFunction2,this);
    thread3 = AfxBeginThread((AFX_THREADPROC)MyThreadFunction3,this);

}

UINT MyThreadFunction1(LPARAM lparam)
{
    CThreadingEx4Dlg* pthis = (CThreadingEx4Dlg*)lparam;
    pthis->MyFunction(0,100);
    return 0;
}
UINT MyThreadFunction2(LPARAM lparam)
{
    CThreadingEx4Dlg* pthis = (CThreadingEx4Dlg*)lparam;
    pthis->MyCommonFunction(0,20);
    return 0;
}

UINT MyThreadFunction3(LPARAM lparam)
{
    CThreadingEx4Dlg* pthis = (CThreadingEx4Dlg*)lparam;
    WaitForSingleObject(pthis->thread3->m_hThread,INFINITE);
    //here thread3 waits for thread 2 to finish so that thread can continue
    pthis->MyCommonFunction(21,40);
    return 0;
}
void CThreadingEx4Dlg::MyFunction(int minCount,int maxCount)
{

    for(int i=minCount;i<maxCount;i++)
    {
        //assume myArray is a CStringArray and it has 100 elemnts added to it.
        //myShiftArray is a CStringArray -public to the class
        CString temp;
        temp = myArray.GetAt(i);
        myShiftArray.Add(temp);
    }

}

void CThreadingEx4Dlg::MyCommonFunction(int min,int max)
{
    for(int i = min;i < max;i++)
    {
        CSingleLock myLock(&myCS,TRUE);
        CString temp;
        temp = myShiftArray.GetAt(i);
        //threadArray is CStringArray-public to the class
        threadArray.Add(temp);
    }
    myEvent.PulseEvent();

}

最佳答案

您希望哪个函数是“线程安全的”?

我认为该术语应该应用于您的 CommonFunction。这是一个函数,您打算将其调用为多个(在第一种情况下为两个)线程。

我认为您的代码有一条规则:

Thread 2 do some work

meanwhile Thread 3 wait until Thread 2 finishes then you do some work

其实你的代码有

WaitForSingleObject(pthis->thread3->m_hThread,INFINITE);

也许等待错误的线程?

但回到线程安全。安全警务在哪里?它在你的线程的控制逻辑中。假设您有很多线程,您将如何扩展您编写的内容?你有很多这样的逻辑:

if thread a has finished and thread b has finished ...

真的很难正确和维护。相反,您需要使 CommonFunction 真正线程安全,即它需要容忍同时被多个线程调用。

在这种情况下,您可以通过在代码的关键部分周围放置某种互斥体来做到这一点,在这种情况下,这可能是整个函数——不清楚您是否打算将复制的项目放在一起,或者是否请注意这些值是否交错。

在后一种情况下,唯一的问题是对 myArray 和 myShiftArray 的访问是否是线程安全的集合

    temp = myArray.GetAt(i);
    myShiftArray.Add(temp);

所有其他变量都是本地的,位于当前线程所拥有的堆栈上 - 因此您只需要查阅这些集​​合的文档以确定它们是否可以被单独的线程安全地调用。

关于c++ - 这个函数是线程安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2771546/

相关文章:

c++ - CDaoDatabase断言错误

windows - 更改最小/最大/关闭按钮主题

c++ - 引用文献未给出预期结果

c++ - 有没有办法将 child *返回到基地*并评估哪个 child *已返回?

c# - 不可变列表不添加数据

java - Tomcat 线程数只会增加

.net - 从 ActiveX 控件使用 CWinFormControl

c# - 获取错误 CS0031 : Constant value `65535' cannot be converted to a `short'

c++ - 如何将参数传递给 draw() 方法 - FLTK

python - 非守护进程 python 线程在完成任务后会退出吗?