확장명이 .NEF 인 wile을 처리해야하는 다음 코드가 있습니다.내 스레드 프로그램에 어떤 문제가 있습니까?
#include <iostream>
#include <regex>
#include <pthread.h>
#include <dirent.h>
using namespace std;
void *workHorse(void*);
int main (int argc, char *argv[]){
pthread_t t1;
int rc, pos1;
DIR *dir;
struct dirent *ent;
regex e("(.*)(\\.)(NEF|nef)");
if ((dir = opendir (".")) != NULL) {
string fn1;
while ((ent = readdir (dir))!=NULL){
fn1.assign(ent->d_name);
if (regex_match (fn1, e)){
cout<<"F :"<<fn1.c_str()<<" "<<endl;
if (rc=pthread_create(&t1, NULL, &workHorse, (void*)&fn1)){
cout<<"Error creating threads "<<rc<<endl;
exit(-1);
}
}
}
}
return 0;
}
void *workHorse(void *fileName){
int ret;
cout<<"W :"<<((string*)fileName)->c_str()<<endl;
pthread_exit(NULL);
}
확장명이 .NEF 인 파일이 하나뿐입니다. 내 예상 출력은 -
F :DSC_0838.NEF
W :DSC_0838.NEF
그러나, 나는
F :DSC_0838.NEF
W :RGBbmp.bmp
RGBbmp.bmp
-
g++ tmp.cpp -pthread --std=c++11
* fn1' 문자열 개체가 하나뿐입니다 - 포인터가 전달됩니다. 그러나 메인 스레드의'cout'은 부작용 *을 즉시 평가합니다. – user2864740
또한 쓰레드에 안전하지 않은 여러 개의 스레드를 사용하여 cout으로 인쇄합니다. 귀하의 결과가 크게 혼란 스럽거나 혼합 될 수 있습니다. – Paladine