pthread_join函数崩溃

时间:2016-02-17 14:06:01

标签: c gcc pthreads

内部发生了崩溃' pthread_join'当main函数调用它时,子线程已经终止。这是来自gdb的回溯:

Core was generated by `./bin/test'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0xb76fb530 in __call_tls_dtors@plt () from /lib/libpthread.so.0
(gdb) bt
#0  0xb76fb530 in __call_tls_dtors@plt () from /lib/libpthread.so.0
#1  0xb76fdd5a in start_thread (arg=0xb40fab40) at pthread_create.c:319
#2  0xb762f74e in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:129

pthread激活函数接收NULL参数并返回NULL参数。我无能为力,为什么我一直看到这次崩溃。

有人可以帮助儿童线程激活功能出错吗?我正在使用Fedora 20和gcc版本4.8.3 20140911(Red Hat 4.8.3-7)(GCC)

儿童激活功能的骨架在

之下
void* testControl(void* param)
{
   ...................
   return NULL;
}

由于我的代码很大,我在这里给出了代码片段,它解释了我如何创建子线程,它们的出口和终止。

unsigned long int rcThId1;  
unsigned long int rcThId2;
unsigned long int rcThId3;
unsigned long int rcThId4;
unsigned long int rcThId5;
unsigned long int rcThId6;

void* rcControl1(void* arg)
{
  bool th_loop = true;

  while(th_loop)
  {

     /*Listen and receive the message on message queue*/

     ...........
     ..........

     switch(message_type)
     {
        ............
        ............

        case EXIT:

          th_loop = false;
          break;

        default:
          break;
     }

   }

   return NULL;

}

/*Activation functions for rcControl2 rcControl3 rcControl4

rcControl5 rcControl6 similar to the defination of rcControl1*/

int main(void)
{
  pthread_create(&rcThId1,NULL,rcControl1,NULL);
  pthread_create(&rcThId2,NULL,rcControl2,NULL);
  pthread_create(&rcThId3,NULL,rcControl3,NULL);
  pthread_create(&rcThId4,NULL,rcControl4,NULL);
  pthread_create(&rcThId5,NULL,rcControl5,NULL);
  pthread_create(&rcThId6,NULL,rcControl6,NULL);

  ..............
  ..............
  /*Post EXIT event to Thread1*/
  /*Post EXIT event to Thread2*/
  /*Post EXIT event to Thread3*/
  /*Post EXIT event to Thread4*/
  /*Post EXIT event to Thread5*/
  /*Post EXIT event to Thread6*/
  /*By now all threads would have already exited */

  pthread_join(rcThId1, NULL);/*Inside this function crash is happening*/    
  pthread_join(rcThId2, NULL);
  pthread_join(rcThId3, NULL);
  pthread_join(rcThId4, NULL);
  pthread_join(rcThId5, NULL);
  pthread_join(rcThId6, NULL);

  return 0;
}

pthread_join(rcThId1, NULL);内部调用崩溃事件。

由于

1 个答案:

答案 0 :(得分:0)

在您发布的(伪)代码中,主要问题是线程标识符的类型:它们都应该是pthread_t类型。但是你有unsigned long int个。崩溃很可能是因为pthread_join()试图将rcThId1等人视为pthread_t,而不是rcThId1

rcThId6 .. pthread_t的类型更改为gcc -Wall -Wextra -pedantic-errors

你应该收到一些警告。如果没有编译器:

main()

除了:

您可能需要将线程ID作为全局线程。将它们移到var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); var _ = require('lodash'); var clients = {}; app.get('/', function(req, res){ res.send('<h1>Message Server</h1>'); }); io.on('connection', function(socket){ // store the socket with the username if a reply event was emitted socket.on('reply.username', function(username) { console.log('Got Username reply', username); clients[username] = socket; }); // handle direct messages socket.on('msg.private', function(obj) { console.log('routing private message'); if (obj.to && clients[obj.to]) { console.log('delivering to', obj.to); clients[obj.to].emit('msg.private', obj.message); } }); // emit a request to the client to send its username back socket.emit('send.username'); }); //start the server http.listen(3000, function() { console.log('Started server'); }); 内,除非你有充分的理由不这样做。