title: Multithreading with Strings in C
body: <p>I have a problem with my code, maybe one of you might help me. 
  Let's try to be short...
  I have a school project to do, and it requires pipes and threads(PL=C, SO=Debian).  The program is running fine, except for one little error that is robbing me of hours of sleep.</p>
  
  <p>I put my 'thread' to make the file access of the program, so... it just have to open the files, save the data, and save a 'report' of parts of the file in a string, so that the 'shell' can show it to the user.</p>
  
  <p>But the problem is that, if i print the string(global) inside the thread, the string has the data, and it's printed, but if i let to print in the 'shell part of the program' the string is empty, no data at all... Below are the key parts of the code, if anyone can help me, i'll be very grateful.
  Btw, sorry about my english, not my native language.</p>
  
  <p>That is the thread function</p>
  
  <pre><code>void *acessaDisco(void *arg)
  {
      int op;
      op = (int) arg;
  
      switch(op)
      {
          case 1:
              //salva no disco
              //sem_wait(&amp;semaforoControleTextoThread);
              if(escrevePersonagemArquivo(personagemTempThread)==TRUE){
                  pthread_mutex_lock(&amp;tranca);
                  sprintf(textoThread,"\n\nSeu personagem foi salvo corretamente!\n\n");
                  pthread_mutex_unlock(&amp;tranca);          
              }
              else{
                  pthread_mutex_lock(&amp;tranca);
                  sprintf(textoThread,"\n\nOcorreu um erro ao tentar salvar seu personagem...\n\n");
                  pthread_mutex_unlock(&amp;tranca);
              }
              //sem_post(&amp;semaforoControleTextoThread);
              break;
          case 2:
              //encontra personagem no arquivo
              //sem_wait(&amp;semaforoControleTextoThread);
              if(encontraPersonagem(nomeBuscaPersonagem,&amp;personagemTempThread)==FALSE){
                  pthread_mutex_lock(&amp;tranca);
                  sprintf(textoThread, "\n\nInfelizmente, o personagem %s não foi encontrado... :(\n\n", nomeBuscaPersonagem);
                  pthread_mutex_unlock(&amp;tranca);
              }
              else{
                  pthread_mutex_lock(&amp;tranca);
                  imprimeFichaTemp(personagemTempThread, textoThread);
                  strcpy(auxiliarTeste, textoThread);
                  pthread_mutex_unlock(&amp;tranca);
                  //printf("\n\n\nDepois bloquear mutex, string: %s", textoThread);
              }
              //sem_post(&amp;semaforoControleTextoThread);
              break;
      }
  
      return NULL;
  
  }
  </code></pre>
  
  <p>Here is where i call it from...</p>
  
  <pre><code>case 300:
          //procura o personagem
          strcpy(nomeBuscaPersonagem, buff+4*sizeof(char));
          pthread_create(&amp;threadAcessaDisco, NULL, acessaDisco, (int)2);
          break;
  </code></pre>
  
  <p>And finally, where i print it</p>
  
  <pre><code>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(&amp;semaforoControleTextoThread);
              printf("\n\nDepoisSemWaitClient\n\n");
              fflush(stdout);
              fflush(stdin);
              pthread_mutex_lock(&amp;tranca);
              printf("\n\nTexto Thread:%s Texto aux: %s\n\n", textoThread, auxiliarTeste);
              pthread_mutex_unlock(&amp;tranca);
              fflush(stdout);
              fflush(stdin);
              //sem_post(&amp;semaforoControleTextoThread);
              getchar();
              break;
  </code></pre>
  
  <p>If any additional information is needed, i'll be glad to provide!</p>
  
  <p>Thanks in advance!!</p>