奇怪的多线程链接问题,记录一下

来源:互联网 发布:c语言判断字符为数字 编辑:程序博客网 时间:2024/04/29 00:51

代码很简单,如下;

 

 

  1. #include <unistd.h>
  2. #include <pthread.h>
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. void* pfn(void* pParam)
  7. {
  8.  const char* psz = (char*)pParam;
  9.  cout << psz << endl;
  10.  pthread_exit(0);
  11. }
  12. void* create(const char* psz)
  13. {
  14.  pthread_t tid = 0;
  15.  pthread_attr_t attr;
  16.  pthread_attr_init(&attr);
  17.  pthread_create(&tid, &attr, pfn, (void*)psz);
  18. }
  19. int main(int argc, char* argv[])
  20. {
  21.  const char* psz = "Hello, thread!";
  22.  create(psz);
  23.  sleep(1000);
  24.  return 0;
  25. }

编译时居然不用-lpthread选项能通过,平台是SUSE10.0企业版。

运行时死活到pthread_create时出现段错误,原因是函数的地址为零。

化了好长时间,下载了OpenSUSE10.3装了起来,终于没有问题了,当然链接时要指定-lpthread

原创粉丝点击