-1

以下功能似乎是问题的原因”

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: 错误: 候选是: 模板void* server_work(void*)

4

1 回答 1

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);
}
于 2013-09-29T11:40:42.933 回答