我目前正在尝试从其他线程创建线程。 当我的线程试图加入时,我得到一个段错误
这是我的主要功能:
from itertools import groupby
data = (k.rstrip().split("=Cluster=") for k in open("f_input.txt", 'r'))
final = list(k for k,_ in groupby(list(data)))
with open("new_file.txt", 'a') as f:
for k in final:
if k == ['','']:
f.write("=Cluster=\n")
elif k == ['']:
# write '\n\n' in Windows and '\n' in Linux (tested only in Windows!)
f.write("\n\n")
else:
f.write("{}\n".join(k))
和我的帖子主要:
new_file.txt
运行valgrind会给我错误:
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);
}
每当我在ubuntu机器上运行它时,我都会遇到分段错误。 使用gdb,在pthread_join中放置一个断点并单步执行最终会给我一个段错误。
在Mac上运行它,我得到以下输出:
./ a.out
开始
完成初始化
加入0
a.out(3473,0x10c61e000)malloc: *对象0x7f97fa5016f0的错误:未释放指针被释放 * 在malloc_error_break中设置断点以进行调试
中止陷阱:6
修改
一些定义:
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;
}
答案 0 :(得分:0)
请添加缺少的详细信息来完成您的程序:threadBody(),init(),clock()&amp; 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()函数是什么?
答案 1 :(得分:-2)
长线程=(长)*线程不应该只用“(长)线程。我指的是你的threadMain函数的第一行