C ++有PThreads问题,而不是编译

时间:2013-09-29 06:25:29

标签: c++ pthreads

以下功能似乎是导致问题的原因“

template <class Type>
void * server_work(void * arg)
{
    int ii;
    int cno;
    Server<Type> * server = (Server<Type> *) arg;
    Customer<Type> newcust;

    for(ii=0; ii<QUEUE_LENGTH; ii++)
    {
        size_t length = rand()%(MAX_RANGE-MIN_RANGE)+MIN_RANGE ; // Generate the number, assign to variable.
        pthread_mutex_lock(&MUTEX);
        cno=CUSTOMER_COUNT;
        CUSTOMER_COUNT++;
        pthread_mutex_unlock(&MUTEX);

        newcust=Customer<Type>(cno, cno,cno,length);

        if(CUSTOMER_COUNT<=QUEUE_LENGTH)
        {
            server->IncreaseNumOfCustomers();

            for(size_t i = 0; i < length; ++i)
            {
                newcust.getLinkedList().insertFirst(1000);
            }
            server->getCustomers()[ii]=newcust;
        }
        else
        {
            break;
        }
    }

    return NULL;
}

当编译器读取以下代码时会出现问题:

int main(int argc, char** argv)
{


    pthread_t threads[NUMBER_OF_SERVERS];
    int i,j;


    if(pthread_mutex_init(&MUTEX, NULL))
    {
        cout<<"Unable to initialize a MUTEX"<<endl;
        return -1;
    }

    Server<int> servs[NUMBER_OF_SERVERS];

    for(i = 0; i < NUMBER_OF_SERVERS; i++)
    {
        servs[i].setServerNum(i);
        pthread_create(threads+i, NULL, server_work, (void *)&servs[i]);//<<--compiler flags here
    }

    // Synchronization point
    for(i = 0; i < NUMBER_OF_SERVERS; i++)
    {
        pthread_join(*(threads+i), NULL);
    }

    cout<<"SERVER-NO\tCUSTOMER-NO\tARRIVAL-TIME\tWAITING-TIME\tTRANSACTION-TIME"<<endl;
    for(i = 0; i < NUMBER_OF_SERVERS; i++)
    {
        for(j=0; j<servs[i].getCustomersServed(); j++)
        {
            cout<<i<<"\t\t"<<servs[i].getCustomers()[j].getCustomerNumber()<<"\t\t"<<servs[i].getCustomers()[j].getArrivalTime()<<"\t\t"<<servs[i].getCustomers()[j].getWaitingTime()<<"\t\t"<<servs[i].getCustomers()[j].getTransactionTime()<<endl;

        }

    }
    cout<<endl;
    cout<<endl;

我从编译器中收到以下错误:

  

main.cpp:84:71:错误:没有匹配将函数'server_work'转换为'void *()(void )'       main.cpp:26:8:错误:候选者是:template void * server_work(void *)

1 个答案:

答案 0 :(得分:1)

你的原型错误:

template <class Type>
void * server_work(void * arg)

虽然pthread期待这样的事情

void * server_work(void * arg)

然而,解决这个问题并不困难,例如:

void* CoolWrapper(void* arg)
{
     return server_work<desired_type>(arg);
}