2

我想做的是启动两个线程,每个线程都运行该crit_area函数。我需要传递 ptrBank from main()tocrit_area()以便 BANK 结构balance[0]balance[1]由线程按顺序更新。为此,我创建了自己的信号量类sem_class-1.h。我已经解决了所有的编译错误,除了以下内容:

race1d.cc:49:61: error: invalid conversion from ‘void* (*)(BANK*)’ to ‘void* (*)(void*)’ [-fpermissive]
/usr/include/pthread.h:225:12: error:   initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’ [-fpermissive]
race1d.cc:54:61: error: invalid conversion from ‘void* (*)(BANK*)’ to ‘void* (*)(void*)’ [-fpermissive]
/usr/include/pthread.h:225:12: error:   initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’ [-fpermissive]
race1d.cc: In function ‘void* crit_area(BANK*)’:
race1d.cc:105:10: error: too few arguments to function ‘void (* signal(int, __sighandler_t))(int)’
/usr/include/signal.h:101:23: note: declared here

我对指针还不是很满意(其中一些代码是给我开始的),甚至不确定我是否可以通过 pthread 将指向结构的指针传递给函数。我尝试在 pthread 函数调用中以各种方式传递指针,即pthread_create(&tid1, NULL, crit_area, ptrBank)pthread_create(&tid1, NULL, crit_area, *ptrBank)全部无济于事。我还花了几个小时在网上搜索类似的问题。任何人都可以帮忙吗?是的……这是家庭作业的一部分,我和我的实验室伙伴已经完成了除了最后一部分之外的所有作业。[请不要评判我们的newb代码]

#include <unistd.h>     /* Symbolic Constants */
#include <sys/types.h>  /* Primitive System Data Types */ 
#include <sys/wait.h>   /* System error numbers */ 
#include <errno.h>      /* Errors */
#include <stdio.h>      /* Input/Output */
#include <stdlib.h>     /* General Utilities */
#include <pthread.h>    /* POSIX Threads */
#include <string.h>     /* String handling */
//#include <semaphore.h>  /* Semaphore */
//#include <fcntl.h>       /* Needed for arguments to sem_open */
#include "shm437.h"
#include "sem_class-1.h"

// BANK Structure, with integer variable 'balance' 
struct BANK 
{
    int balance[2];
        // BANK function sets balance variable == 0
        BANK() 
        {
            balance[0]=balance[1]=0;
        }
};

void * crit_area(BANK * a);

// Begin main program
int main(int argc, char **argv)
{ 
    pthread_t tid1, tid2;

    Shm437 *pShmBank = new Shm437(1,sizeof(BANK));     // set up pointers
    BANK *ptrBank = (BANK*) pShmBank->ShmAlloc();

    ptrBank->balance[0] = ptrBank->balance[1] = 100;

    Semaphore(1);                                         // initialize Semaphore class, pass sig
    srandom(getpid());                                    // set seed

    printf("Init balances 0:%d + 1:%d ==> %d!\n",         // print initial
                ptrBank->balance[0],
                ptrBank->balance[1],
                ptrBank->balance[0]+ptrBank->balance[1]
            );

    if(pthread_create(&tid1, NULL, crit_area, ptrBank))    // p thread 1
    {
      printf("\n ERROR creating thread 1");
      exit(1);
    }
    if(pthread_create(&tid2, NULL, crit_area, ptrBank))     // p thread 2
    {
      printf("\n ERROR creating thread 2");
      exit(1);
    }
    if(pthread_join(tid1, NULL))                //wait for the thread 1 to finish 
    {
      printf("\n ERROR joining thread");
      exit(1);
    }

    if(pthread_join(tid2, NULL))            // wait for the thread 2 to finish 
    {
      printf("\n ERROR joining thread");
      exit(1);
    }
        // print new values
        printf("Let's check the balances 0:%d + 1:%d ==> %d ?= 200\n",
                    ptrBank->balance[0],ptrBank->balance[1],
                    ptrBank->balance[0]+ptrBank->balance[1]);

    pthread_exit(NULL);
   Semaphore(1);
}


/*  Thread Function critical area where we will use semaphore
      and create balance with timing delay */
void * crit_area(BANK * a)  
{   
   int tmp1, tmp2, rint;    
    double dummy;   

    wait(0);
    for (int i=0; i < 100; i++)             // loop for 99 times
    {
        tmp1 = a->balance[0];                  // tmp1 var is equal to balance[0] or 100 //line 49
        tmp2 = a->balance[1];                  // tmp2 var is equal to balance[1] or 100
        rint = (rand()%20)-10;                // rint var == random number (0 thru 19)-10 or -10 thru 9 
                                              // using the seed number (PID) as the starting value
        if ((tmp1+rint)>=0 && (tmp2-rint)>=0) // if non-negative
        {
            a->balance[0] = tmp1 + rint;        // if not, change balance[0] to tmp1+rint //line 55
        for (int j=0; j < rint*100; j++)     // run this math problem for rint*100 times

         {dummy=2.345*8.765/1.234; }     
            usleep(1);                  

            a->balance[1] = tmp2 - rint;        // change balance[1] to tmp2-rint         //line 63
        }
    }
    signal(1);
}
4

1 回答 1

4

Pthreads 需要带有签名的函数void*,而不是BANK*. crit_area您应该按如下方式更改功能:

void * crit_area(void* voidA) {
    BANK *a = (BANK*)voidA;
    ... // The rest of your function
}
于 2013-10-21T01:11:13.670 回答