我想做的是启动两个线程,每个线程运行 crit_area
功能。我需要从 main()
传递 ptrBank至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);
}
最佳答案
Pthreads 期望函数的签名采用 void*
,而不是 BANK*
。您应该按如下方式更改 crit_area
函数:
void * crit_area(void* voidA) {
BANK *a = (BANK*)voidA;
... // The rest of your function
}
关于c++ - 线程: I want to pass a struct pointer in main to a function called by pthread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19484809/