C++ pthread:我想在main中向pthread调用的函数传递一个结构指针
我试图做的是启动两个线程,每个线程运行C++ pthread:我想在main中向pthread调用的函数传递一个结构指针,c++,linux,pointers,pthreads,semaphore,C++,Linux,Pointers,Pthreads,Semaphore,我试图做的是启动两个线程,每个线程运行crit\u区域函数。我需要将ptrBank从main()传递到crit_area(),以便线程顺序更新银行结构balance[0]和balance[1]。为了实现这一点,我创建了自己的sem\u class-1.h信号量类。我已经解决了所有编译错误,但以下错误除外: race1d.cc:49:61: error: invalid conversion from ‘void* (*)(BANK*)’ to ‘void* (*)(void*)’ [-fperm
crit\u区域
函数。我需要将ptrBank从main()
传递到crit_area()
,以便线程顺序更新银行结构balance[0]
和balance[1]
。为了实现这一点,我创建了自己的sem\u 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\u create(&tid1,NULL,crit\u area,ptrBank)
或pthread\u create(&tid1,NULL,crit\u 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);
}
#包含/*符号常量*/
#包括/*基本系统数据类型*/
#包括/*系统错误号*/
#包括/*错误*/
#包括/*输入/输出*/
#包括/*一般公用设施*/
#包括/*POSIX线程*/
#include/*字符串处理*/
//#include/*信号量*/
//#包含打开sem_的参数所需的/**/
#包括“shm437.h”
#包括“sem_1.h级”
//银行结构,带整数变量“余额”
结构银行
{
国际收支平衡[2];
//BANK函数设置余额变量==0
银行()
{
余额[0]=余额[1]=0;
}
};
无效*临界区(银行*a);
//开始主程序
int main(int argc,字符**argv)
{
pthread_t tid1,tid2;
Shm437*pShmBank=new Shm437(1,sizeof(BANK));//设置指针
BANK*ptrBank=(BANK*)pShmBank->ShmAlloc();
ptrBank->余额[0]=ptrBank->余额[1]=100;
信号量(1);//初始化信号量类,传递信号
srandom(getpid());//设置种子
printf(“初始余额0:%d+1:%d==>%d!\n”,//打印初始值
ptrBank->余额[0],
ptrBank->余额[1],
ptrBank->余额[0]+ptrBank->余额[1]
);
if(pthread_create(&tid1,NULL,crit_区域,ptrBank))//p线程1
{
printf(“\n创建线程1时出错”);
出口(1);
}
if(pthread_create(&tid2,NULL,crit_区域,ptrBank))//p线程2
{
printf(“\n创建线程2时出错”);
出口(1);
}
if(pthread_join(tid1,NULL))//等待线程1完成
{
printf(“\n连接线程时出错”);
出口(1);
}
if(pthread_join(tid2,NULL))//等待线程2完成
{
printf(“\n连接线程时出错”);
出口(1);
}
//打印新值
printf(“让我们检查余额0:%d+1:%d=>%d?=200\n”,
ptrBank->余额[0],ptrBank->余额[1],
ptrBank->余额[0]+ptrBank->余额[1]);
pthread_exit(NULL);
信号量(1);
}
/*线程函数是我们将使用信号量的关键区域
并与时间延迟建立平衡*/
无效*临界面积(银行*a)
{
int tmp1、tmp2、rint;
双假人;
等待(0);
for(int i=0;i<100;i++)//循环99次
{
tmp1=a->balance[0];//tmp1变量等于balance[0]或100//第49行
tmp2=a->balance[1];//tmp2变量等于balance[1]或100
rint=(rand()%20)-10;//rint var==随机数(0到19)-10或-10到9
//使用种子数(PID)作为起始值
if((tmp1+rint)>=0&(tmp2-rint)>=0)//如果非负
{
a->balance[0]=tmp1+rint;//如果不是,将balance[0]更改为tmp1+rint//第55行
for(int j=0;jbalance[1]=tmp2-rint;//将balance[1]更改为tmp2 rint//第63行
}
}
信号(1);
}
Pthreads希望函数的签名为void*
,而不是BANK*
。您应该按如下方式更改crit_区域
功能:
void * crit_area(void* voidA) {
BANK *a = (BANK*)voidA;
... // The rest of your function
}
Pthreads期望函数的签名为
void*
,而不是BANK*
。您应该按如下方式更改crit_区域
功能:
void * crit_area(void* voidA) {
BANK *a = (BANK*)voidA;
... // The rest of your function
}
虽然我会使用C++的CAST(因为它是C++)。但是,OP在她的代码中使用了C CAST,代码主要是“C编写的C++编译器”,完整的引用了C标头,其中C++等价物存在,所以我用了C型铸造。DasBLink(LasBLink):我做了一些类似的Baskink光:你的建议是有效的…我做了一些类似的事情:代码>空隙* CRITZAL(空洞*A){StReToBank *PTR=(StReStub *)A;< /Cord>,允许我传递指针到函数,即使我对它的工作有点模糊。谢谢。如果不允许使用<代码> -f>“<代码> >,我该怎么办?虽然我会使用C++的CAST。(因为它是C++)。@洛基斯塔里是这样的。但是,OP在她的代码中使用了C Casp,代码很大程度上是“C++编译器的C”。完成了C++标题的引用,所以我使用了C的CAST。DasBLink NeLink:我做了一些类似的Baskink灯:你的建议是有效的…我做了类似的代码>空隙* CRITZORE(空洞*A){StReToB**PTR=(StReStub *)A;<代码>