免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1384 | 回复: 3
打印 上一主题 下一主题

关于Linux C多线程的一个问题? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-05-11 11:51 |只看该作者 |倒序浏览
按照参考书写一个多线程并发服务器程序,以下是相关代码:

  1. .........

  2. /* Called by pthread_create() */
  3. void *start_routine(void *arg);
  4. struct ARG
  5. {
  6.         int connfd;
  7.         struct sockaddr_in client;
  8. };
  9. ......
  10. main()
  11. {
  12.    pthread_t thread;
  13.    struct ARG  *arg;
  14.    ......

  15.    arg = new ARG;
  16.                
  17.    arg->;connfd=connectfd;
  18.    memcpy((void *)&arg->;client,&client,sizeof(client));

  19.    if(pthread_create(&thread,NULL,start_routine,(void *)arg))
  20.   {
  21.      perror("Pthread_create() Error:");
  22.     exit(1);
  23.   }
  24.   ........
  25. }

  26. void* start_routine(void *arg)
  27. {
  28.         struct ARG *info;
  29.         info=(struct ARG *)arg;

  30.         /* the function handle client's requirement */
  31.         process_cli(info->;connfd,info->;client);

  32.         delete arg;
  33.                 pthread_exit(NULL);
  34.        
  35. }

复制代码

编译的时候说new和delete没有定义
后来我分别换成malloc和free,编译通过,运行时提示段错误
用gdb察看了一下,是程序中参数arg的free有问题
请问如何解决这个问题?谢谢

论坛徽章:
0
2 [报告]
发表于 2003-05-11 17:39 |只看该作者

关于Linux C多线程的一个问题?


  1. arg = new ARG;
  2.       
  3.    arg->;connfd=connectfd;
  4.    memcpy((void *)&arg->;client,&client,sizeof(client));

  5.    if(pthread_create(&thread,NULL,start_routine,(void *)arg))
  6.   {
  7.      perror("Pthread_create() Error:");
  8.     exit(1);
  9.   }
  10. 以后没有对arg再运行操作吧
复制代码



[/code]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
3 [报告]
发表于 2003-05-12 09:06 |只看该作者

关于Linux C多线程的一个问题?

malloc

arg = (void *)malloc(sizeof( int ) + sizeof( struct sockaddr_in ) );

free
free(arg );

论坛徽章:
0
4 [报告]
发表于 2003-05-12 16:26 |只看该作者

关于Linux C多线程的一个问题?

在程序中做了以下改变,编译运行测试均得到了预期的结果

  1. struct ARG
  2. {
  3.     int connfd;
  4.     struct sockaddr_in client;
  5. }
  6. ........
  7. arg=new ARG;
  8. ........
  9. delete arg;
  10. ........
复制代码

改为

  1. typedef struct Arg
  2. {
  3.         int connfd;
  4.         struct sockaddr_in client;
  5. }ARG;
  6. ........
  7. arg=(void *)malloc(struct(ARG));
  8. ........
  9. free(arg);
  10. .......
复制代码

最后谢谢各位的留言,让我想到的解决办法!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP