#include <pthread.h>
#include <iostream>
#include <iomanip>
#define NOPER 4
struct operation
{
int val1;
int val2;
int op;
double result;
};
void *calculator(void *pos_void_ptr)
{
struct operation *pos_ptr = (struct operation *)pos_void_ptr;
switch(pos_ptr->op)
{
case 0: pos_ptr->result = pos_ptr->val1 + pos_ptr->val2;
break;
case 1: pos_ptr->result = pos_ptr->val1 - pos_ptr->val2;
break;
case 2: pos_ptr->result = pos_ptr->val1 * pos_ptr->val2;
break;
case 3: if (pos_ptr->val2 != 0)
pos_ptr->result = (double) pos_ptr->val1 / pos_ptr->val2;
else
pos_ptr->result = 0;
break;
}
return NULL;
}
int main()
{
static struct operation operations[NOPER];
pthread_t tid[NOPER];
for(int i=0;i<NOPER;i++)
{
operations[i].op = i;
std::cin >> operations[i].val1;
std::cin >> operations[i].val2;
if(pthread_create(&tid[i], NULL, calculator, &operations[i]))
{
fprintf(stderr, "Error creating thread\n");
return 1;
}
}
// Wait for the other threads to finish.
for (int i = 0; i < NOPER; i++)
pthread_join(tid[i], NULL);
for (int i = 0; i < NOPER; i++)
{
switch(operations[i].op)
{
case 0: std::cout << operations[i].val1 << " + " << operations[i].val2 << " = " << std::fixed << std::setprecision(2) << operations[i].result << std::endl;
break;
case 1: std::cout << operations[i].val1 << " - " << operations[i].val2 << " = " << std::fixed << std::setprecision(2) << operations[i].result << std::endl;
break;
case 2: std::cout << operations[i].val1 << " * " << operations[i].val2 << " = " << std::fixed << std::setprecision(2) << operations[i].result << std::endl;
break;
case 3: std::cout << operations[i].val1 << " / " << operations[i].val2 << " = " << std::fixed << std::setprecision(2) << operations[i].result << std::endl;
break;
}
}
return 0;
}
我正在尝试了解有关内部的基本概念
void *calculator(void *pos_void_ptr)
{
struct operation *pos_ptr = (struct operation *)pos_void_ptr;
switch(pos_ptr->op)
以及这些参数是什么
(pthread_create(&tid[i], NULL, calculator, &operations[i])
以及如何在任何程序中实现?该程序需要2次输入4次,然后将前2个元素相加,然后将后2个元素相乘,然后减去前2个元素,然后将第2个元素相除
这里定义了4个线程标识符,以后将传递给pthread_create
: