Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti-thread use fstream to read different files but caused segment fault
    primarykey
    data
    text
    <p>First I use a python script to generate 600 files eg:one0 ...one199 two0...two199 five0...five199 Then I write a C++ program which is used fstream to read different files. My code:</p> <pre><code>int main(int argc,char* argv[]) { init();//init the data structure int createthread=0; signal(SIGINT,CtrlHandlerRoutine); pthread_t *workthread= new pthread_t[6]; ThreadParam *param=new ThreadParam[6]; for (int i = 0; i &lt; 6; ++i) { param[i].thread_index=i; } for (int index = 0; index &lt; 2; ++index) { /* code */ if(pthread_create(&amp;workthread[index],NULL,WorkerThreadFunction,&amp;param[index])==0) { createthread++; fflush(stdout); } } while(!gExitFlag) { usleep(100000); } gWorkThreadStopFlag=true; while(gStoppedThread&lt;createthread) { usleep(1); } delete[] workthread; for(int i=0; i&lt;DELAY_TYPE_NUM; i++) { printProcDelayResult(gProcDelay[i]); } fflush(stdout); return 0; } </code></pre> <p>Thread func:</p> <pre><code>void* WorkerThreadFunction(void* arg) { ThreadParam *param=(ThreadParam*) arg; int threadnum=param-&gt;thread_index; int max_index,min_index; int protype=0; string filetype=""; //memset(&amp;sessionProcDelay,0,21*sizeof(sessionProcDelay)); init the data structure //fstream fd;//define a handle of the file switch(threadnum) { case 0: max_index=99; min_index=0; protype=1; filetype="one"; break; case 1: max_index=199; min_index=100; protype=1; filetype="one"; break; case 2: max_index=99; min_index=0; protype=2; filetype="two"; break; case 3: max_index=199; min_index=100; protype=2; filetype="two"; break; case 4: max_index=99; min_index=0; protype=3; filetype="five"; break; case 5: max_index=199; min_index=100; protype=3; filetype="five"; break; } for (int i = min_index; i &lt;=max_index; ++i) { ostringstream oss; oss&lt;&lt;i; string filename=filetype+oss.str(); fstream fd(filename.c_str(),ios::in); if(!fd.is_open()) { printf("Can not open the file\n"); } int num=0; char text[6]; while(!fd.eof()) { num++; fd.getline(text,6); string line(text); int mvalue=atoi(line.c_str()); f_StatProcessDelay(sessionProcDelay[i*DELAY_TYPE_NUM],protype,mvalue); /*if(mvalue==0) { printf("filename:%s,line:%d content %s\n",filename.c_str(),num,line.c_str()); }*/ } fd.close(); } printf("OVER\n"); while(!gWorkThreadStopFlag) { usleep(1000); } pthread_mutex_lock(&amp;gDataLock); for (int j = 0; j &lt; 100; ++j) { for (int k = 0; k &lt; DELAY_TYPE_NUM; ++k) { /* code */ if(sessionProcDelay[j*DELAY_TYPE_NUM+k].procDelayMax&gt;0) // if process delay data is available, if not procDelayMax is -1 { int procTypeIndex = sessionProcDelay[j*DELAY_TYPE_NUM+k].procDelayType; updateTotalProcDelay(sessionProcDelay[j*DELAY_TYPE_NUM+k], gProcDelay[procTypeIndex-1]); } } } gStoppedThread++; pthread_mutex_unlock(&amp;gDataLock); } </code></pre> <p>And GDB log:</p> <pre><code>(gdb) bt #0 0x00007ffff7935d1d in std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;::getline(char*, long) () from /usr/lib64/libstdc++.so.6 #1 0x0000000000401db6 in WorkerThreadFunction (arg=0x604054) at mutithreadcal.cpp:147 #2 0x00007ffff7bc85f0 in start_thread () from /lib64/libpthread.so.0 #3 0x00007ffff71bf84d in clone () from /lib64/libc.so.6 #4 0x0000000000000000 in ?? () </code></pre> <p>What's the root cause for this segement fault?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload