2017-02-09 1 views
1

현재 다른 스레드에서 스레드를 만들려고합니다. 내 스레드에 가입하려고 할 때 , 나는이 내 주요 기능입니다스레드에서 스레드를 만드는 중 pthread_join 오류가 발생했습니다.

세그먼트 폴트를 얻을 :

int main(int argc, char *argv[]) { 
    std::cout<< "start" << std::endl; 
    init(); 
    std::cout<<"finished init" << std::endl; 
    t1=clock(); 
    pthread_t threads[THREAD_COUNT]; 

    for (int i = 0; i < THREAD_COUNT; i++) { 
     pthread_create(&threads[i], NULL, &threadMain, (void*)((long)i)); 
    } 

    for (int i = 0; i < THREAD_COUNT; i++) { 
     printf("joining %d \n" , i); 
     pthread_join(threads[i], NULL); 
    } 
    timeEnd(); 


    return(0); 
} 

및 주요 내 스레드 :

void *threadMain(void *arg) { 
long thread = (long) arg; 

volatile int *tix; 
tix = (volatile int *)malloc(sizeof(volatile int) * INNER_THREADS); 
volatile int *c; 
c = (volatile int *)malloc(sizeof(volatile int) * INNER_THREADS); 
volatile int r; 

memset((void*)tix, 0, sizeof(tix)); 
memset((void*)c, 0, sizeof(c)); 
r = 0; 


pthread_t threads[INNER_THREADS]; 


for (int i = 0; i < INNER_THREADS; ++i) { 
    vec[i+thread*2] = new desc(); 
    vec[i+thread*2]->outterThread = thread; 
    vec[i+thread*2]->innerThread = i; 
    vec[i+thread*2]->tix = tix; 
    vec[i+thread*2]->c = c; 
    vec[i+thread*2]->r = &r; 
    pthread_create(&threads[i], NULL, &threadBody, (void*) vec[i+thread*2]); 


} 

for (int i = 0; i < INNER_THREADS; ++i) { 
    pthread_join(threads[i], NULL); 
} 


return 0; 
} 

Valgrind의를 실행하면 나에게 오류를 제공합니다

==820== Thread 288: 
==820== Invalid read of size 4 
==820== at 0x387AA08213: pthread_join (in /lib64/libpthread-2.12.so) 
==820== by 0x4019F1: threadMain(void*) (t.c:146) 
==820== by 0x387AA07AA0: start_thread (in /lib64/libpthread-2.12.so) 
==820== by 0x387A2E8AAC: clone (in /lib64/libc-2.12.so) 
==820== Address 0xc29c79d0 is not stack'd, malloc'd or (recently) free'd 
==820== 
==820== 
==820== Process terminating with default action of signal 11 (SIGSEGV) 
==820== Access not within mapped region at address 0xC29C79D0 
==820== at 0x387AA08213: pthread_join (in /lib64/libpthread-2.12.so) 
==820== by 0x4019F1: threadMain(void*) (t.c:146) 
==820== by 0x387AA07AA0: start_thread (in /lib64/libpthread-2.12.so) 
==820== by 0x387A2E8AAC: clone (in /lib64/libc-2.12.so) 
==820== If you believe this happened as a result of a stack 
==820== overflow in your program's main thread (unlikely but 
==820== possible), you can try to increase the size of the 
==820== main thread stack using the --main-stacksize= flag. 
==820== The main thread stack size used in this run was 10485760. 

우분투 컴퓨터에서 실행할 때마다 나는 세그먼트를 얻습니다. 오류. gdb를 사용하면 pthread_join에 중단 점을 넣고 단계별로 실행하면 결과적으로 세그 폴트가 생깁니다.

맥에 실행, 나는 다음과 같은 출력을 얻을 :

./a.out

start

finished init

joining 0

a.out(3473,0x10c61e000) malloc: * error for object 0x7f97fa5016f0: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug

Abort trap: 6

편집

일부 정의 :

#define INNER_THREADS 2 
#define THREAD_COUNT 10 

struct desc{ 
    long outterThread; 
    long innerThread; 
    volatile int* tix; 
    volatile int* c; 
    volatile int* r; 
}; 

struct desc* vec[THREAD_COUNT*2]; 
+0

어디에서'free()'가 있습니까? 이것은 나쁜 것처럼 보입니다 :'vec [i + thread * 2] -> tix = tix;': 동적으로 할당 된 메모리 주소의 사본을 여러 개 만들고 있습니다. 나는 당신을 여러 번 자유롭게 해줄 것이라고 확신한다. – YSC

+0

@YSC 나는 그들을 자유롭게하지 않았다. 스레드가 tix, c 및 r에 대한 자체 메모리를 갖기 때문에 내부 스레드가 함께 작동합니다. – BabblingMonkey

+0

'memset ((void *) tix, 0, sizeof (tix));'와 비슷하지만 관련이 없다. – YSC

답변

-2

긴 스레드를 = (긴) * 스레드하지 사용해야합니다 그냥 "(긴) 스레드. 스레드의 첫 번째 줄을 참조하고 있습니다. 메인 함수

0

제발 threadBody(), init(), clock() & timeEnd() 등의 누락 된 세부 정보를 추가하여 프로그램을 완료하거나 문제와 관련이없는 경우 제거하십시오.

좀 누락 된 세부 사항을 가정/삭제하여 코드를 준비 할 수 있습니다 : http://pastebin.com/j5a9whdR

그것은 잘 작동합니다.

$ ./a.out | sort 
joining outer thread 0 
joining outer thread 1 
joining outer thread 2 
joining outer thread 3 
joining outer thread 4 
joining outer thread 5 
joining outer thread 6 
joining outer thread 7 
joining outer thread 8 
joining outer thread 9 
Outer thread 0 Inner thread 0 
Outer thread 0 Inner thread 1 
Outer thread 1 Inner thread 0 
Outer thread 1 Inner thread 1 
Outer thread 2 Inner thread 0 
Outer thread 2 Inner thread 1 
Outer thread 3 Inner thread 0 
Outer thread 3 Inner thread 1 
Outer thread 4 Inner thread 0 
Outer thread 4 Inner thread 1 
Outer thread 5 Inner thread 0 
Outer thread 5 Inner thread 1 
Outer thread 6 Inner thread 0 
Outer thread 6 Inner thread 1 
Outer thread 7 Inner thread 0 
Outer thread 7 Inner thread 1 
Outer thread 8 Inner thread 0 
Outer thread 8 Inner thread 1 
Outer thread 9 Inner thread 0 
Outer thread 9 Inner thread 1 
$ 

일부 메모리 누수를 제외하고는 valgrind도 만족합니다.

'2'(vec [i + thread * 2] 및 vec [THREAD_COUNT * 2])의 사용법은 INNER_THREADS로 대체해야합니다.

주된 질문은 threadBody() 함수에있는 것입니다.

+0

in threadBody, 나는 모든 내부 스레드가 액세스해야하는 c, r 및 tix 값을 업데이트 중입니다. – BabblingMonkey

관련 문제