Problem with Android NDK pthread_mutex_unlock problem

I had a problem with pthread_mutex_unlock and my NDK application for vigorous activity. I added log messages for each mutex initialization, lock, lock, and unlock. My program is at a standstill because unlocking mutexes tells me that the thread trying to unlock the mutex did not block it. My magazine says otherwise. Therefore, I am wondering if there is something that I am doing / not doing that can cause this. The number in () is mutex_t *, followed by the line number of the code and the thread identifier returned by pthread_self (). It seems to me that thread 1584064 has acquired a lock and is doing its processing. Then, thread 1597448 tries to lock and is properly suspended, waiting for a lock to be received. The problem is that when 1584064 exits and tries to release the lock, pthread_mutex_unlock returns an error (1).

09-21 13: 55: 27.098: WARN / native-activity (1333): try to block the mutex (2154220968) Line: 3041 Subject: 1584064

09-21 13: 55: 27.098: WARN / native-activity (1333): Success: blocked Mutex (2154220968) Line: 3041 Subject: 1584064

09-21 13: 55: 31.668: DEBUG / dalvikvm (352): GC_EXPLICIT released 8K, 4% free 8606K / 8963K, suspended 3 ms + 423 ms

09-21 13: 55: 31.898: WARN / native-activity (1333): try to block the mutex (2154220968) Line: 3041 Subject: 1597448

09-21 13: 55: 32.198: WARN / native-activity (1333): Error: 1 Unlock Mutex (2154220968) Line: 3045 Subject: 1584064

Initialization is a macro that looks like this:

#define InitializeCriticalSection(p, X) \ { \ LOGW("Initialize Mutex(%u)", p); \ *p = PTHREAD_MUTEX_INITIALIZER; \ pthread_mutexattr_t attr; \ pthread_mutexattr_init(&attr); \ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); \ int ii_p = pthread_mutex_init((pthread_mutex_t *)p, &attr); \ pthread_mutexattr_destroy(&attr); \ LOGW("Initialize Mutex(%u) Returned %d", (pthread_mutex_t *)p, ii_p); \ } 

One thread is a standard NDK thread, the other is my own timer thread, initialized as follows:

 pthread_t thread; pthread_attr_t pattr; int state; state = pthread_attr_init(&pattr); pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_DETACHED); if (!state) { LOGW("Creating Timers Thread %d MS", dwMsecs); int iRetTest = pthread_create(&thread, &pattr, TimersThread, (void *)pTimer); pthread_attr_destroy(&pattr); } 

etc. error processing

It doesn't seem to make any difference if I attachCurrentThread to vm or not in the timers thread. Mutexes seem to be working incorrectly. Offer any help you can provide in advance.

0
source share
1 answer

I get it. The problem was initialization. Although this works on several other platforms, I really did it wrong. If you are using custom init, you must add a mutex). The new init looks like this:

 #define InitializeCriticalSection(p) \ { \ p = new pthread_mutex_t; \ LOGW("Initialize Mutex(%u)", p); \ pthread_mutexattr_t attr; \ pthread_mutexattr_init(&attr); \ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); \ int ii_p = pthread_mutex_init(p, &attr); \ pthread_mutexattr_destroy(&attr); \ LOGW("Initialize Mutex(%u) Returned %d", p, ii_p); \ } 

Phew!

+1
source

All Articles