Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ pthread_连接错误,从线程生成线程_C++_Multithreading_Pthreads_Pthread Join - Fatal编程技术网

C++ pthread_连接错误,从线程生成线程

C++ pthread_连接错误,从线程生成线程,c++,multithreading,pthreads,pthread-join,C++,Multithreading,Pthreads,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_C

我目前正在尝试从其他线程生成线程。 当我的线程试图加入时,我会遇到一个错误

这是我的主要职能:

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_连接中放置一个断点并单步执行最终会给我一个segfault

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

/a.out

开始

完成初始化

加入0

a、 out(3473,0x10c61e000)malloc:*对象0x7f97fa5016f0的错误:未分配要释放的指针 *在malloc\u error\u break中设置断点以进行调试

中止陷阱: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];

long thread=(long)*thread不应仅用于“(long)thread。我指的是threadMain函数的第一行

请通过添加缺少的详细信息来完成您的程序:threadBody()、init()、clock()和timeEnd(),如果它们与所面临的问题无关,请删除它们

我可以通过删除/假设缺少一些详细信息来准备代码:

它很好用

$ ./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\u COUNT*2]中)的用法应该替换为内部线程


主要的问题是threadBody()函数中有什么?

free()在哪里?这看起来很糟糕:
vec[i+thread*2]->tix=tix;
:您正在制作动态分配内存地址的多个副本;我打赌您是
free()
tix多次。@YSC我没有释放它们。我需要线程拥有自己的tix、c和r内存,这样它们的内部线程就可以使用它。这看起来很可疑:
memset((void*)tix,0,sizeof(tix));
,但未更新的行是
t.c:146
?@YSC line
t.c:146
pthread\u连接(线程[I],NULL)在threadBody中,我正在更新c、r和tix中的值,所有内线程都应该可以访问这些值。
#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];
$ ./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
$