2017-02-09 83 views
1

我目前正在尝试从其他线程进行线程。 当我的线程试图加入,我得到一个segfaultpthread_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. 

每当我在ubuntu机器上运行它时,我会得到一个分段重刑故障。 使用gdb,在pthread_join中放置一个断点并逐步完成,最终给了我一个段错误。

运行它在Mac上,我得到以下的输出:

./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

长的细丝=(长)*线应不使用只是“(long)thread。我指的是threadMain函数的第一行

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

在threadBody中,我正在更新c,r和tix中所有内部线程应该有权访问的值。 – BabblingMonkey