提供建议,阅读很多关于这个主题的内容,并且没有找到对此行为的解释pthread_detach ..
帮助的结构代码:
#define QUEUE_SIZE 50
//...
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_t tid[QUEUE_SIZE];
//....
int main(int argc, char *argv[])
{
pthread_attr_t thattr;
//...
if (pthread_mutex_init(&mutex, NULL) != 0) { ... exit; }
for(;;) {
if ( tn == QUEUE_SIZE ) { tn = 0; }
while (1) {
if(pthread_mutex_trylock(&mutex) == 0) {
//....
memset or other for data carg[]
//....
pthread_mutex_unlock(&mutex);
break;
}}
pthread_attr_init(&(thattr));
if (pthread_attr_setdetachstate(&thattr,PTHREAD_CREATE_DETACHED) != 0) { .. error print.. }
ret = pthread_create(&(tid[tn]), &(thattr), th_logger, &carg[tn]);
if ( ret != 0 ) { ... continue; } else {
if ( pthread_detach(tid[tn]) != 0 ) { ...perror etc } // This is seg fault permanent
if ( pthread_attr_destroy(&thattr) != 0 ) { ...perror etc }
tn++;
}
//...
} // end for(;;;)
void* th_logger(void* arg) {
//....
while (1) {
if(pthread_mutex_trylock(&mutex) == 0) {
//... read write pointer to structure data arg
pthread_mutex_unlock(&mutex);
break;
}}
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
//.... job and other ..
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
while (1) {
if(pthread_mutex_trylock(&mutex) == 0) {
tid[a->mutexid] = (pthread_t)NULL;
pthread_mutex_unlock(&mutex);
break;
}}
pthread_exit(0);
}
valgrind写道:
==3673== Invalid read of size 4
==3673== at 0x7BF41B: pthread_detach (in /lib/libpthread-2.5.so)
==3673== by 0x8051152: main (in /usr/sbin/sensord-ng)
==3673== Address 0x48 is not stack'd, malloc'd or (recently) free'd
==3673==
==3673== Process terminating with default action of signal 11 (SIGSEGV)
==3673== Access not within mapped region at address 0x48
==3673== at 0x7BF41B: pthread_detach (in /lib/libpthread-2.5.so)
==3673== by 0x8051152: main (in /usr/sbin/sensord-ng)
==3673==
==3673== ERROR SUMMARY: 5 errors from 3 contexts (suppressed: 19 from 1)
==3673==
==3673== 1 errors in context 1 of 3:
==3673== Invalid read of size 4
==3673== at 0x7BF41B: pthread_detach (in /lib/libpthread-2.5.so)
==3673== by 0x8051152: main (in /usr/sbin/sensord-ng)
==3673== Address 0x48 is not stack'd, malloc'd or (recently) free'd
也许我错过了一些重要的内容,但我没有在代码中看到任何基本错误。