[英]C++ class member variable initialization with thread
I am implementing a threading example with classes and passing argument to thread as a structure. 我正在实现一个带有类的线程示例,并将参数作为结构传递给线程。 I initialize class-members for each of the four threads but somehow one class-member is not getting initialized and it is getting initiated to garbage value. 我为四个线程中的每一个初始化类成员,但是不知何故一个类成员没有被初始化,而是被初始化为垃圾值。 I tried reducing number of threads to 3, however the same class-member is getting initialed to garbage value. 我尝试将线程数减少到3,但是同一类成员正在初始化为垃圾值。
In the below Dining Philosopher example attempt, Philosopher(thread)1 fork2 is getting initialized to garbage value. 在下面的Dining Philosopher示例尝试中,Philosopher(thread)1 fork2被初始化为垃圾值。
#include <iostream>
#include <stdio.h>
#include "PhilOne.h"
#include <pthread.h>
using namespace std;
#define NUM_PHIL 4
struct phil_data {
int thread_id;
int fork1;
int fork2;
};
void *StartPhil(void *threadarg){
struct phil_data *my_data;
my_data = (struct phil_data *) threadarg;
int tid = my_data->thread_id;
int my_fork1 = my_data->fork1;
int my_fork2 = my_data->fork2;
cout << "Hello World! I am Philosopher number, " << tid << endl;
cout << "My fork1 is:" << my_fork1 << endl;
cout << "My fork2 is:" << my_fork2 << endl;
PhilOne phil[tid];
phil[tid].waitForForks();
phil[tid].eat();
phil[tid].releaseForks();
phil[tid].think();
std::cout<<std::endl;
pthread_exit(NULL);
}
int main(){
pthread_t threads[NUM_PHIL];
struct phil_data td[NUM_PHIL];
int rc;
int i;
for (i=0;i<NUM_PHIL;i++){
//pthread_mutex_lock(&lock);
cout << "main(): creating Philosopher," << i << endl;
td[i].thread_id = i;
switch (i){
case 0:
{
td[i].fork1=0;
td[i].fork2=1;
break;
}
case 1:
{
td[i].fork1=1;
td[i].fork2=5;
break;
}
case 2:
{
td[i].fork1=2;
td[i].fork2=3;
break;
}
case 3:
{
td[i].fork1=3;
td[i].fork2=0;
break;
}
default:
break;
}
rc = pthread_create(&threads[i],NULL,StartPhil,(void *)&td[i]);
if (rc){
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
pthread_exit(NULL);
}
Below is PhilOne.cpp 以下是PhilOne.cpp
#include "PhilOne.h"
#include <iostream>
using namespace std;
PhilOne::PhilOne(){
//ctor
}
PhilOne::~PhilOne(){
//dtor
}
int PhilOne::eat(){
cout << "Eating ...." << endl;
sleep(2);
return 0;
}
int PhilOne::releaseForks()
{
std::cout << "Released forks ...." <<std::endl;
return 0;
}
int PhilOne::think()
{
std::cout << "Thinking...." <<std::endl;
sleep(1);
return 0;
}
int PhilOne::waitForForks()
{
std::cout << "Waiting for forks...." <<std::endl;
sleep(3);
return 0;
}
I am not able to reproduce your result but there appears to be some Undefined Behavior in the program. 我无法重现您的结果,但是程序中似乎存在一些未定义的行为。
PhilOne phil[tid];
phil[tid].waitForForks();
What is the purpose of declaring an array of PhilOne objects? 声明PhilOne对象数组的目的是什么? The second line will access the object that is one -after- the array. 第二行将访问数组之后的对象。 I think you just need 我想你只需要
PhilOne phil;
phil.waitForForks();
Try fixing that up and it may correct your issue. 尝试解决此问题,它可能会解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.