【发布时间】:2014-10-19 06:20:23
【问题描述】:
我的代码有问题,也许你们中的某个人可以帮助我。 让我们试着做个短... 我有一个学校项目要做,它需要管道和线程(PL=C,SO=Debian)。程序运行良好,除了一个小错误让我无法入睡。
我把我的“线程”用于程序的文件访问,所以......它只需要打开文件,保存数据,并将文件部分的“报告”保存在字符串中,所以'shell' 可以将其显示给用户。
但问题是,如果我在线程内打印字符串(全局),字符串有数据,并且它被打印,但如果我让在“程序的外壳部分”中打印,则字符串为空,根本没有数据...下面是代码的关键部分,如果有人可以帮助我,我将非常感激。 顺便说一句,对不起我的英语,不是我的母语。
那是线程函数
void *acessaDisco(void *arg)
{
int op;
op = (int) arg;
switch(op)
{
case 1:
//salva no disco
//sem_wait(&semaforoControleTextoThread);
if(escrevePersonagemArquivo(personagemTempThread)==TRUE){
pthread_mutex_lock(&tranca);
sprintf(textoThread,"\n\nSeu personagem foi salvo corretamente!\n\n");
pthread_mutex_unlock(&tranca);
}
else{
pthread_mutex_lock(&tranca);
sprintf(textoThread,"\n\nOcorreu um erro ao tentar salvar seu personagem...\n\n");
pthread_mutex_unlock(&tranca);
}
//sem_post(&semaforoControleTextoThread);
break;
case 2:
//encontra personagem no arquivo
//sem_wait(&semaforoControleTextoThread);
if(encontraPersonagem(nomeBuscaPersonagem,&personagemTempThread)==FALSE){
pthread_mutex_lock(&tranca);
sprintf(textoThread, "\n\nInfelizmente, o personagem %s não foi encontrado... :(\n\n", nomeBuscaPersonagem);
pthread_mutex_unlock(&tranca);
}
else{
pthread_mutex_lock(&tranca);
imprimeFichaTemp(personagemTempThread, textoThread);
strcpy(auxiliarTeste, textoThread);
pthread_mutex_unlock(&tranca);
//printf("\n\n\nDepois bloquear mutex, string: %s", textoThread);
}
//sem_post(&semaforoControleTextoThread);
break;
}
return NULL;
}
这里是我调用它的地方...
case 300:
//procura o personagem
strcpy(nomeBuscaPersonagem, buff+4*sizeof(char));
pthread_create(&threadAcessaDisco, NULL, acessaDisco, (int)2);
break;
最后,我在哪里打印它
case 2:
fflush(stdout);
fflush(stdin);
printf("\n\nAntes Tela Visualizacao\n\n");
telaVisualizacao(writefd);
fflush(stdout);
fflush(stdin);
printf("\n\nAntesSemWaitClient\n\n");
//sem_wait(&semaforoControleTextoThread);
printf("\n\nDepoisSemWaitClient\n\n");
fflush(stdout);
fflush(stdin);
pthread_mutex_lock(&tranca);
printf("\n\nTexto Thread:%s Texto aux: %s\n\n", textoThread, auxiliarTeste);
pthread_mutex_unlock(&tranca);
fflush(stdout);
fflush(stdin);
//sem_post(&semaforoControleTextoThread);
getchar();
break;
如果需要任何其他信息,我很乐意提供!
提前致谢!!
【问题讨论】:
-
在
stdin上执行fflush在技术上是未定义的行为,它是某些库中的扩展,但是如果您想保持代码的可移植性,则不应这样做。此外,stdout默认是行缓冲的,所以stdout在打印换行符时会自动刷新,因此您不必这样做。最后,在那些支持在stdin上执行fflush的系统上,它只需要在您读取输入之前完成,而不是每次打印任何内容时完成。 -
这里有很多不清楚的地方。您在
case 300上创建线程,但在case 2上打印,显然没有创建线程。此外,您似乎没有等待线程准备好textoThread,然后再将其打印到任何地方。 -
另外,您对
nomeBuscaPersonagem的使用不受保护,因此如果您为一个线程设置它,但在线程有机会使用它之前更改它,该线程将使用更新后的字符串. -
显然你试图在你的代码中使用信号量(你注释掉)。这很重要,你需要它们。我只是没有看到
sem_init()被使用,这很重要。 -
好吧,让我们分部分进行
标签: c string thread-safety pthreads mutex