pthread:我想将main中的struct指针传递给pthread调用的函数

时间:2013-10-21 01:06:36

标签: c++ linux pointers pthreads semaphore

我要做的是启动两个线程,每个线程都运行crit_area函数。我需要将ptrBank从main()传递给crit_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)都无济于事。我也花了几个小时在线搜索类似的问题。有人可以帮忙吗?是的......这是家庭作业的一部分,我的实验室合作伙伴和我完成了除最后一部分以外的所有工作。 [请不要判断我们的新代码]

#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);
}

1 个答案:

答案 0 :(得分:4)

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

void * crit_area(void* voidA) {
    BANK *a = (BANK*)voidA;
    ... // The rest of your function
}