为什么这段代码不一致崩溃?

时间:2012-02-22 22:42:16

标签: c++ debugging pthreads

我正在使用eratosthenes'为课堂写一个优质筛选程序。我有8个线程,每个线程负责一个数字范围的段[1,2 ^ 32]。出于某种原因,有时我的线程数组中的线程[0]不会使它成为线程在创建时进入的函数。其他人总是(似乎)这样做。请告诉我这段代码有什么问题。虽然是一个警告,我只是学习C ++,所以可能存在语法错误等,我认为这会导致错误。花了好几个小时把它缩小到线程[0]并不总是把它变成函数。我更改了#define的值,因此调试起来会更容易。 Bug无论哪种方式都会发对于我如何改进该计划,我对其他评论不太感兴趣。它很快到期,所以我只想让它按原样运行。非常感谢!

#include <iostream>
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <bitset>
#include <sys/time.h>
#include <sys/resource.h>
#include <sched.h>
#include <vector>
#include <math.h>

using namespace std;

#define NUM_OF_THREADS 8
#define TWO_TO_THIRTY_SECOND 1000000 //4294967296
#define SQRT_TWO_TO_THE_THIRTY_SECOND 1000 //65536
#define TWO_T0_THIRTY_SECOND_OVER_EIGHT 125000 //536870976

typedef struct {
    unsigned long composite_to_remove_index;
    int thread_index;
    unsigned long prime_number;
} thread_info_t;

bitset<TWO_T0_THIRTY_SECOND_OVER_EIGHT> bitmap[NUM_OF_THREADS];
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_t thread[NUM_OF_THREADS];


static void * threadFunc(void *arg)
{

    thread_info_t info = *(thread_info_t *)arg;

    bitmap[info.thread_index][info.composite_to_remove_index] = 1;
    info.composite_to_remove_index += info.prime_number;

    int index_to_remove = (int)info.composite_to_remove_index;

    for(; index_to_remove < TWO_T0_THIRTY_SECOND_OVER_EIGHT; index_to_remove += info.prime_number)
    {
        if(bitmap[info.thread_index][index_to_remove] == 0)
        {
            bitmap[info.thread_index][index_to_remove] = 1;
            if(info.thread_index == 0)
            {
                cout << "bit " << index_to_remove << ": " << bitmap[info.thread_index][index_to_remove] << "\n";
            }
        }
    }

    return NULL;
}

int main (int argc, char * argv[])
{   
    int thread_ret_val;
    vector<unsigned long long> prime_numbers;
    thread_info_t info; 

    for(unsigned long long i = 2; i < SQRT_TWO_TO_THE_THIRTY_SECOND; i++)
    {
        if(bitmap[0][i] == 0)
        {
            prime_numbers.push_back(i);
            info.prime_number = i;

            for(unsigned long j = 0; j < NUM_OF_THREADS; j++)
            {
                if(j == 0)
                    info.composite_to_remove_index = i*2;
                else
                    info.composite_to_remove_index = (((TWO_TO_THIRTY_SECOND/NUM_OF_THREADS)*j) % i);
                info.thread_index = (int)j;


                thread_ret_val = pthread_create(&thread[info.thread_index], NULL, threadFunc, (void*)&info);
                if(thread_ret_val != 0)
                {
                    cerr << "create thread error " << strerror(thread_ret_val) << "\n";
                }
            }

            for(int j = 0; j < NUM_OF_THREADS; j++)
            {
                pthread_join(thread[j], NULL);
            }
        }
    }

    return 1;
}

1 个答案:

答案 0 :(得分:1)

传递给所有线程的

info结构对于每个线程都是唯一的,但在这里无意中偶尔会在线程之间共享。问题是,在info的调用和pthread_create的开头之间,threadFunc不能保证不变。在调用pthread_create之后,您在下一次迭代中更改了info的内容,并且可能恰好前一个线程实际上在info的第一行中复制了threadFunc。 / p>

您应该拥有std::vector个信息结构,并且每次使用不同的pthread_create致电info