2009-04-10 81 views
1

pthread_create返回值251而不创建线程。有谁知道问题是什么?请帮忙。该机器是HP-UX。pthread返回251

我是新来的多线程。

#include <stdio.h> 
    #include <stdlib.h> 
    #include <pthread.h> 

    void *print_message_function(void *ptr); 

    main() 
    { 
     pthread_t thread1, thread2; 
     char *message1 = "Thread 1"; 
     char *message2 = "Thread 2"; 
     int iret1, iret2; 
     /* Create independent threads each of which will 
     * execute function */ 

     iret1 = pthread_create(&thread1, NULL, print_message_function, (void*) message1); 
     iret2 = pthread_create(&thread2, NULL, print_message_function, (void*) message2); 

     /* Wait till threads are complete before 
     * main continues. Unless we */ 
     /* wait we run the risk of executing an 
     * exit which will terminate */ 
     /* the process and all threads before the 
     * threads have completed. */ 

     pthread_join(thread1, NULL); 
     pthread_join(thread2, NULL); 
     printf("Thread 1 returns: %d\n",iret1); 
     printf("Thread 2 returns: %d\n",iret2); 
     exit(0); 
    } 

    void *print_message_function(void *ptr) 
    { 
     char *message; 
     message = (char *) ptr; 
     printf("%s \n", message); 
    } 

回答

4

编辑:在HP-UX11上。 pthread_create失败,错误251:函数不可用。

检查链接顺序中是否存在-lc之前的-lpthread。 如果是这种情况,那么该调用将解析为存储在C库 中,并可能导致此错误。


你链接到-lpthread吗?

您应该使用errno.h中看到251是您的系统上有什么错误,或者这应该给你一个更的相关详细信息:

printf("%s\n", strerror(errno)); 

此外,使用并行线程的时候,你应该检查返回值几乎每次调用并行线程*(参见各功能的man检查返回的可能的错误)

对于在pthread_create,你至少有2个可能出现的错误(取决于您的系统和并行线程的实现上):

[EAGAIN该系统缺乏必要的资源来创建 另一个线程,或系统强加的限制上的螺纹的 总数在工艺 [PTHREAD_THREADS_MAX]将超过:如果10在pthread_create()将失败。

[EINVAL] attr指定的值无效。

0

这编译和运行在我的Linux机器,结果如下:

Thread 1 
Thread 2 
Thread 1 returns: 0 
Thread 2 returns: 0 

所以看起来这个问题是不是在你的代码,但在一些环境。我有10多年没有使用过HP-UX,所以我无法在那帮助你。

+0

是的,看起来它跟环境有关。 – Shree 2009-04-10 09:15:03

+0

似乎它可能是链接顺序,请检查我编辑的答案。 – claf 2009-04-10 09:16:08