| 发表于:2007-06-14 11:45:56 楼主 |
问题描述如下,在线程a中接收数据,写入buff后,使用pthread_cond_signal发送一个信号,线程b中使用while(1)循环接收信号,搜索buff,找到buff中的数据,写入文件。 现在问题是调用signal后函数不能返回, while(1)的循环执行完一次循环,不能开始新的循环………… 另我很是费解啊。 大家帮忙看一下。 代码片段如下: //发送signal部分 if( pthread_mutex_trylock( &g_pfile[1].packetlist[ data-> nid - 1].mutex) == 0) { #ifndef ndebug printf( "-lock- "); #endif //packet i not recv and move into buff if( !g_pfile[1].packetlist[ data-> nid - 1].data && !g_pfile[1].packetlist[ data-> nid - 1].npacketsize) { //malloc mem spc and copy it g_pfile[1].packetlist[ data-> nid - 1].data = (char*)malloc( data-> npacketsize); if(g_pfile[1].packetlist[ data-> nid - 1].data) { memcpy( g_pfile[1].packetlist[ data-> nid - 1].data, data-> data, data-> npacketsize); #ifndef ndebug printf( "-packet %d '- " , data-> nid); #endif g_pfile[1].packetlist[ data-> nid - 1].npacketsize = data-> npacketsize; #ifndef ndebug printf( "-sig- "); #endif pthread_cond_signal( &g_pfile[1].cond); #ifndef ndebug printf( "-nal- "); #endif } else { pthread_mutex_unlock( &g_pfile[1].packetlist[ data-> nid - 1].mutex); #ifndef ndebug printf( "-unlock- "); #endif err err; err.opc = 7; err.nerrorcode = 10; //send message if(sendto(sockfd, &err, sizeof( err), 0, toaddr, sizeof( struct sockaddr)) <= 0) { #ifndef ndebug perror( "send err "); #endif } else { #ifndef ndebug printf( "--send err --\n "); #endif } break; } } pthread_mutex_unlock( &g_pfile[1].packetlist[ data-> nid - 1].mutex); #ifndef ndebug printf( "-unlock- "); #endif } //接收 处理部分 while(1) { #ifndef ndebug printf( "-in loop- "); #endif pthread_mutex_unlock( &g_pfile[1].mutex); #ifndef ndebug printf( "-unlock file 1- "); #endif pthread_testcancel(); pthread_mutex_lock( &g_pfile[1].mutex); #ifndef ndebug printf( "-lock file 1- "); #endif //lock current update file info and wait a data singal maketimeout( &timeout, 1); int timeoutflag = -1; static int timeoutcount = 0; #ifndef ndebug printf( "-wait- "); #endif if( (timeoutflag = pthread_cond_timedwait( &g_pfile[1].cond, &g_pfile[1].mutex, &timeout) )== 0 ¦ ¦ g_pfile[1].writedcount < g_pfile[1].rrq.npacketcount ) { #ifndef ndebug printf( "-if- "); #endif //check time out count if( timeoutflag && timeoutcount < 20) { timeoutcount++; } else if( timeoutcount > = 20) { int i = 0; for( ; i < g_pfile[1].rrq.npacketcount; i++) { pthread_mutex_destroy( &g_pfile[1].packetlist[i].mutex); } free( g_pfile[1].packetlist); g_pfile[1].packetlist = null; pthread_mutex_unlock( &g_pfile[1].mutex); fclose(fp); fp = null; psmsghandle-> dataupdate( localpath, g_pfile[1].rrq.sfilename, -1); break; } else { timeoutcount = 0; } //a signal arrive // i is count int i = 0; // j is count, mean file has writed packet count befor i packet int j = 0; #ifndef ndebug printf( "-for-\n "); #endif for( ; i < g_pfile[1].rrq.npacketcount; i++) { #ifndef ndebug printf( "--%d- ", i+1); #endif if( pthread_mutex_trylock( &g_pfile[1].packetlist[i].mutex) == 0) { #ifndef ndebug printf( "-lock %d- ", i+1); #endif // packet i has writed if( !g_pfile[1].packetlist[i].data && g_pfile[1].packetlist[i].npacketsize) { j++; } // packet i not write to file and packet have data else if( g_pfile[1].packetlist[i].data && g_pfile[1].packetlist[i].npacketsize) { //not file end, need insert if( j < g_pfile[1].writedcount) { //insert a packet to file int k = j; for( k; k < g_pfile[1].writedcount; k++ ) { if(fseek( fp, k * g_pfile[1].rrq.npacketsize, seek_set) == 0) { //read packetsize bytes to insert buff if( g_pfile[1].rrq.npacketsize = fread( insertbuff, 1, g_pfile[1].rrq.npacketsize, fp)) { break; } //write and free this packet data if(fwrite( g_pfile[1].packetlist[i].data, 1, g_pfile[1].packetlist[i].npacketsize, fp) == g_pfile[1].packetlist[i].npacketsize) { memcpy( g_pfile[1].packetlist[i].data, insertbuff, g_pfile[1].rrq.npacketsize); }//end if(fwrite) }//end if(fseek) } free( g_pfile[1].packetlist[i].data); #ifndef ndebug printf( "--insert file packet :%d\n ", i); #endif j++; g_pfile[1].writedcount++; } else { //add a packet to file end if(fseek( fp, 0, seek_end) == 0) { #ifndef ndebug fpos_t curpos; fgetpos( fp, &curpos); printf( "--current file pos :%d- ", curpos); #endif //write file and free this packet data if(fwrite( g_pfile[1].packetlist[i].data, 1, g_pfile[1].packetlist[i].npacketsize, fp) == g_pfile[1].packetlist[i].npacketsize) { //free this packet data free( g_pfile[1].packetlist[i].data); g_pfile[1].packetlist[i].data = null; //count this packet j++; g_pfile[1].writedcount++; #ifndef ndebug printf( "-write file packet :%d, size:%d- ", i+1, g_pfile[1].packetlist[i].npacketsize); #endif } } }//end else } pthread_mutex_unlock( &g_pfile[1].packetlist[i].mutex); #ifndef ndebug printf( "-unlock %d-\n ", i+1); #endif }//end if try lock }//end for #ifndef ndebug printf( "-end for, writed %d--\n ", g_pfile[1].writedcount); #endif } #ifndef ndebug printf( "-end if--\n "); #endif if( g_pfile[1].writedcount == g_pfile[1].rrq.npacketcount) { #ifndef ndebug printf( "-update file ok-\n "); #endif //update a file ok pthread_mutex_lock( &g_pfile[0].mutex); int i = 0; for( ; i < g_pfile[1].rrq.npacketcount; i++) { pthread_mutex_destroy( &g_pfile[1].packetlist[i].mutex); } free( g_pfile[1].packetlist); g_pfile[1].packetlist = null; memcpy( &(g_pfile[0].rrq), &(g_pfile[1].rrq), sizeof( rrq)); g_pfile[0].writedcount = g_pfile[1].writedcount; memset( &g_pfile[1].rrq, 0, sizeof( rrq)); g_pfile[1].writedcount = 0; pthread_mutex_unlock( &g_pfile[0].mutex); fclose(fp); fp = null; psmsghandle-> dataupdate( localpath, g_pfile[1].rrq.sfilename, 1); break; } } -in loop--unlock file 1--unlock file 1--wait--if--for- --1--lock 1--unlock 1- --2--lock 2--unlock 2- --3--lock 3--unlock 3- --4--lock 4--unlock 4- --5--lock 5--unlock 5- --6--lock 6--unlock 6- --7--lock 7--unlock 7- --8--lock 8--unlock 8- --9--lock 9--unlock 9- --10--lock 10--unlock 10- --11--lock 11--unlock 11- --12--lock 12--unlock 12- --13--lock 13--unlock 13- --14--lock 14--unlock 14- -end for, writed 0-- -end if-- -in loop--unlock file 1--unlock file 1--wait---send ack : 0 -data: 1,size:4096--lock--packet 1 '--sig--if--for- --1---2--lock 2--unlock 2- --3--lock 3--unlock 3- --4--lock 4--unlock 4- --5--lock 5--unlock 5- --6--lock 6--unlock 6- --7--lock 7--unlock 7- --8--lock 8--unlock 8- --9--lock 9--unlock 9- --10--lock 10--unlock 10- --11--lock 11--unlock 11- --12--lock 12--unlock 12- --13--lock 13--unlock 13- --14--lock 14--unlock 14- -end for, writed 0-- -end if-- 大家看下什么地方出问题了! |
|
|
|
|