求救:CreateMutex函数生成的句柄在子线程一生成就清为NULL。(100分)

  • 主题发起人 主题发起人 dyb
  • 开始时间 开始时间
D

dyb

Unregistered / Unconfirmed
GUEST, unregistred user!
为解决线程互斥共享资源,我设置了三个全局变量,初始化如下:
HANDLE CaptureDataedMutex =CreateMutex(NULL,FALSE,NULL);
HANDLE CompressOutEmptyMutex =CreateMutex(NULL,FALSE,NULL);
HANDLE CompressOutDataedMutex =CreateMutex(NULL,FALSE,NULL);
但子线程一生成,CaptureDataedMutex 、CompressOutEmptyMutex 、CompressOutDataedMutex 全变为NULL,为什么?
 
看看这段解释:
The state of a mutex object is signaled when it is not owned by any thread. The creating thread can use the bInitialOwner flag to request immediate ownership of the mutex. Otherwise, a thread must use one of the wait functions to request ownership. When the mutex's state is signaled, one waiting thread is granted ownership, the mutex's state changes to nonsignaled, and the wait function returns. Only one thread can own a mutex at any given time. The owning thread uses the ReleaseMutex function to release its ownership.
也许因为你某一个线程用了waitfor..指令从而指定了该mutex的owner而没有释放, 这时其他线程
当然无法访问该mutex了
 
将你的source贴上来看看?
 
刚看到一资料说创建Mutexhandle 的process 一结束, MutexHandle自动释放,因此我把CaptureDataedMutex 、CompressOutEmptyMutex 、CompressOutDataedMutex放在一暂时不会停止的线程中创建,但一旦程序转到其它线程中运行,CaptureDataedMutex 、CompressOutEmptyMutex 、CompressOutDataedMutex又自动释放,怎么回事?还没有执行waitforsingleobject().
我现改用 TCriticalSection。但仍感到同步有错, 我使用方法如下:
TCriticalSection lock;//Global declare
lock->Acquire();
protect visit code
lock->Release();
上述用法有错吗?
CB++太次,居然连 TCriticalSection也不认识,搞了半天才知道须加#include <vcl/syncobjs.hpp>才能编译通过,却没有HELP提到。
 
多人接受答案了。
 

Similar threads

后退
顶部