views:

388

answers:

3

I'm developing a DirectShow application. I encounter a deadlock problem, the problem seems caused by acquire lock in a callback function called from a thread. This is the quest I asked in MSDN forum:

http://social.msdn.microsoft.com/Forums/en-US/windowsdirectshowdevelopment/thread/f9430f17-6274-45fc-abd1-11ef14ef4c6a

Now I have to avoid to acquire lock in that thread. But the problem is, I have to output the audio to another thread, how can I put data to another thread without lock?

There's someone tell me that I can use PostMessage of win32 sdk to post data to another thread. But however, to get the message, I have to run a windows program. My program is a Python C++ extension module. That might be very difficult to add a loop to pull message. So I am think another way to pass data among threads without locking. (Actually... the producer thread can't be locked, but the consumer thread can do that. )

To lock or not to lock, that's the question.

So the question is how to do?

Thanks.

------EDIT------

I think I know why I got a deadlock, that might not be the problem of DirectShow.

The main thread is own by Python, it call stop, namely, it hold GIL. And the stop wait for callback of DirectShow in thread return. But callback acquire the GIL.

It looks like this

Main(Hold GIL) -> Stop(Wait callback) -> Callback(Wait GIL) -> GIL(Hold by Main thread)

Damn it! That's why I don't like multi-thread so much. No matter what, thanks your help.

+1  A: 

If you were doing this in pure Python, I'd use a Queue object; these buffer up data which is written but block on read until something is available, and do any necessary locking under the hood.

This is an extremely common datatype, and some equivalent should always be available, whatever your current language or toolchain; there's a STL Queue available in C++, for instance, but the standard doesn't specify thread-safety characteristics (so see your local implementation docs).

Charles Duffy
Yep, I can use STL queue to put and pull data, but the problem is I have to lock that queue before I put or pull something. I have to avoid acquire lock in producer thread.
Victor Lin
FYI: The deadlock is occurs when calling PyGILState_Ensure and DirectShow's contorl->stop(). Or even PyGILState_Ensure and seeking->Seek(...). So I think I can not acquire lock during the DirectShow callback.
Victor Lin
A: 

Well, theoretically locks can be avoided if both of your threads can work on duplicate copies of the same data. After reading your question in the MSDN forum...

"So to avoid deadlock, I should not acquire any lock in the graber callback function? How can I do if I want to output audio to another thread?"

I think that you should be able to deposit your audio data in a dequeue (an STL class) and then fetch this data from another thread. This other thread can then process your audio data.

I am glad that your problem has been resolved the reason I asked about your Os was that the documentation you referred to said that you should not wait on other threads because of some problem with win16Mutexes. There are no win16mutexes on windows XP (except when programs are running on ntvdm/wow16) so you should be able to use locks to synchronize these threads.

SDX2000
Is that thread safe to put data into dequeue, and pull from another thread?
Victor Lin
A: 

An alternative : Inline Asynchronous UI Coding using IDisposable in C# .NET

lsalamon