互斥量没有正确使用?继续过去的问题

Mutex not correctly used? Continuation of past questions

我有第二个问题是此线程的延续:

void ThreadTest(LPTSTR* Pointer, mutex* MutexPtr)
{
    MutexPtr->lock();   
    wcout << *Pointer << endl;
    MutexPtr->unlock();
    return;
}
void FakeMain()
{
    mutex Mut;
    mutex* MutPtr = &Mut;
    LPTSTR Image = L"First String";
    LPTSTR* StrPointer = &Image;
    thread Test(ThreadTest, StrPointer, MutPtr);
    MutPtr->lock();
    if (true)
    {

        NewStringFunction(Image);
        MutPtr-unlock() // Added with the comment of Mike
        Test.join();        
        LPTSTR* StrPointer = &Image;        
        thread Test(ThreadTest, StrPointer, MutPtr);                
    }

    MutPtr->lock();
    Test.join();
    MutPtr->unlock();
    return;
};
void NewStringFunction(LPTSTR& Image)
{
    LPTSTR TempString = L"New String for new Thread";
    Image = TempString;
    return;
};

上面的代码模拟了我遇到问题的代码。 流程应该如下:

我有一个 "if" 语句,因为真实代码在 if 语句中创建了一个线程,我想确保内存分配代表了这一点。 字符串在单独的函数中更改,因为这是实际代码执行此操作的方式。 我销毁了第一个线程,因为在实际代码中,线程显示 window,它改变了大小,因此需要一个新线程。

从编写这段代码开始,我觉得我的互斥量没有正确实现。这段代码没有给出字符串错误,但我认为互斥量被线程覆盖了(我认为)。在程序运行时观察内存,第一个线程实例可能会覆盖互斥锁,我不确定。主线程中的最后一个 mutex.lock() 抛出错误。我是否正确使用了互斥体?我应该 locking/stopping 个线程有更简洁的方法吗?

不要使用互斥锁,也不要共享内存。

蛮力方法:

发件人需要做的就是

Message msg = new Message(<important information>);
// send msg

并且在接收码中,

// use msg  
delete msg;

编写和使用起来非常简单,但存在内存泄漏:如果一条消息从未得到服务,它永远不会归还 RAM。

更好的版本会变得非常复杂。这是一个简单的大纲:

Create a pool of Messages
Assign all messages to a FreeList
Create a mutex to protect FreeList

FreeList 是当前未使用的消息列表。当需要发送 Message 时,第一个 Message 从 FreeList 中移除并交给调用者。来电者在Message中填写要发送的信息,然后发送Message。

接收方负责将 Message 返回给 FreeList。

添加一些功能来管理 FreeList

GetMsg <changed to prevent collision with Windows GetMessage function>
    Message = NULL
    acquire mutex
    if FreeList not empty
        remove Message from FreeList
    release mutex
    return message            

ReturnMsg
    acquire mutex
    Add Message to FreeList
    release mutex

发件人有点复杂

Message msg = GetMessage()
If msg not NULL
    msg.set(<important information>);
    std::thread temp(ThreadTest, msg);
    temp.detatch
else
    // log and handle <- can be simple like abort or complicated like recover lost Messages

接收器还是很简单的

// use msg  
ReturnMessage(msg);

这对内存泄漏有一个上限,可以很容易地跟踪哪些消息从未返回(在池中,但不在 FreeList 中),因此您可以尝试跟踪哪些作业被阻止或炸毁。修锁或炸毁是另一个问题。

OP 有一个好主意,我认为我的想法是对的。这是基本线程函数的内容。它在后台挂起 运行 直到发送终止消息。它阻塞等待消息,使用消息,然后将其放回 FreeList 并阻塞,直到另一条消息到达。

void threadfunc()
{
    MSG winmsg;
    BOOL rval;

    while (GetMessage(&winmsg, (HWND__ *) -1, 0, 0) != -1)
    {
        Message * msg = (Message *)winmsg.wParam;
        // do stuff with msg
        ReturnMsg(msg);
    }
    // GetMessage failed. Find out why and try to recover or die gracefully
}

需要类似

的支持
bool PostMsg(<Important Information>,
             DWORD & BackgroundThreadId)
{
    Message * msg = GetMsg();
    if (msg != NULL)
    {
         msg.set(<Important Information>)
        if (PostThreadMessage(BackgroundThreadId,
                              WM_USER,
                              (WPARAM) msg,
                              0); != 0)
        {
            return true;
        }
        else
        {
            // failed. Find out why and try to recover or die gracefully
            ReturnMsg(msg);
            return false;
        }
    }
    else
    {
        // out of free messages. Try to find out why
    }
}

PostMsg 将消息提供给后台线程的消息队列。消息来自 FreeList,只有 FreeList 需要互斥保护。可以使用多个线程。 我不认为这可以用 std::thread 完成,因为 std::thread 不允许访问底层线程句柄 (刚刚检查过,可以完成)或线程的消息队列。无论如何,这都是基于 Windows 调用,所以忘记可移植性吧。

if 语句之前的锁定调用没有匹配的解锁。 如果主线程在测试 thead 之前获得互斥量,那么您也有潜在的死锁。此外,您的最后一次加入正在等待错误的线程。据我所知,没有理由锁定互斥锁来进行连接。只需加入即可。