시뮬레이터 (ns3)를 사용하고 있는데, 주 응용 프로그램에서 일부 파일을 다운로드하기 위해 Consumer라는 여러 응용 프로그램을 할당합니다. 이 클래스 (소비자)에서 파일 다운로드를 마치면 몇 가지 통계로 파일에 로그온하고 싶습니다. 따라서 아래에 나와있는이 함수를 스레드를 사용하여 구현했습니다. 주에 소수의 사용자가있는 경우 거기에 문제가 아니지만 매우 큰 번호와 스레드를 만드는 함수를 호출하면 로그 파일을 열 수 없습니다.스레드를 사용하여 파일에 로그온하는 방법
//consumer.cc
void
Consumer::OnData (Ptr<const Data> contentObject)
{
//do something
if (...)
{
int a = Consumer::create_thread();
std::string s = "foo";
Consumer::Log(s);
}
}
int Consumer::create_thread(){
int rc = pthread_create(&logger, NULL, &Consumer::runFunc, (void*) this); //logger is declared in consumer.h as pthread_t logger;
if(rc) printf("ERROR; return code from pthread_create() is %d\n", rc);
sem_init(&queueSem, 0, 0);
pthread_mutex_init(&mutex, NULL);
//isLogActive = true;
myfile.open ("ret.txt", std::ios::app);
if(!myfile.is_open())
std::cout << "Error opening the log file...";
return 1;
}
void Consumer::run_t(){
for(;;)
{
sem_wait(&queueSem);
//if(!isLogActive)
// break;
pthread_mutex_lock (&mutex);
myfile << queue.front();
queue.pop();
pthread_mutex_unlock (&mutex);
myfile.flush();
}
while(!queue.empty())
{
std::cout<<queue.front()<<std::endl;
myfile << queue.front();
queue.pop();
}
if(myfile.is_open())
{
myfile.close();
std::cout << "File Ret closed.\n";
}
else
std::cerr << "Calcolo error.\n";
pthread_exit(NULL);
}
void * Consumer::runFunc(void * self){
((Consumer*)self)->run_t();
return NULL;
}
void Consumer::Log(std::string toLog)
{
ss.str("");
ss.clear();
ss << toLog << "\n";
pthread_mutex_lock (&mutex);
queue.push(ss.str());
pthread_mutex_unlock (&mutex);
sem_post(&queueSem);
}
무엇이 문제인가요? 어떻게 해결할 수 있을까요?