Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 通过pthread_create()函数设置errno(问题)_Multithreading_Thread Safety_Pthreads_Fork_Errno - Fatal编程技术网

Multithreading 通过pthread_create()函数设置errno(问题)

Multithreading 通过pthread_create()函数设置errno(问题),multithreading,thread-safety,pthreads,fork,errno,Multithreading,Thread Safety,Pthreads,Fork,Errno,我在ubuntu和嵌入式linux(我们的项目芯片)上运行以下代码。但产出是不同的。为什么线程errno在嵌入式linux上运行时为0?如何获得相同的输出?pthread_create()函数能否设置errno 此外,我使用了fork()函数而不是pthread_create()函数。在这个例子中,我得到了与ubuntu相同的输出。 例如: if (fork()) print_message_function("foo"); ubuntu: >>main errno: 2

我在ubuntu和嵌入式linux(我们的项目芯片)上运行以下代码。但产出是不同的。为什么线程errno在嵌入式linux上运行时为0?如何获得相同的输出?pthread_create()函数能否设置errno

此外,我使用了fork()函数而不是pthread_create()函数。在这个例子中,我得到了与ubuntu相同的输出。 例如:

if (fork())
    print_message_function("foo");
ubuntu:

>>main errno: 2
>>thread errno: 2
嵌入式linux:

>>main errno: 2
>>thread errno: 0
使用fork()函数时嵌入的linux:

>>main errno: 2
>>thread errno: 2

#include <unistd.h>     /* Symbolic Constants */
#include <sys/types.h>  /* Primitive System Data Types */
#include <errno.h>      /* Errors */
#include <stdio.h>      /* Input/Output */
#include <stdlib.h>     /* General Utilities */
#include <pthread.h>    /* POSIX Threads */
#include <string.h>     /* String handling */
#include <time.h>

typedef struct str_thdata
{
    int thread_no;
    char message[100];
} thdata;

int createError(char *str)
{
    int retVal = 0;

    if (NULL == fopen("YOK", "r"))
    {
        printf(">>%s errno: %d\n",str, errno);
        retVal = -errno;
    }
    return retVal;
}

void print_message_function ( void *ptr )
{
    int retVal;
    thdata *data;
    data = (thdata *) ptr;

    retVal = createError("thread");

    while(1)
        ;

    pthread_exit(0);
}

int main()
{
int retVal = 0;
    pthread_t thread1;
    thdata data1;

    data1.thread_no = 1;
    strcpy(data1.message, "Hello!");


    /*if (fork())
        print_message_function("foo");
    */
    pthread_create (&thread1, NULL, (void *) &print_message_function, (void *) &data1);

    retVal = createError("main");
    while(1)
    {

    }

    pthread_join(thread1, NULL);

    exit(0);
}
>主错误号:2
>>线程错误号:2
#包含/*符号常量*/
#包含/*基本系统数据类型*/
#包括/*错误*/
#包括/*输入/输出*/
#包括/*一般公用设施*/
#包括/*POSIX线程*/
#include/*字符串处理*/
#包括
typedef结构str_thdata
{
内螺纹号;
字符消息[100];
}thdata;
int createError(char*str)
{
int-retVal=0;
if(NULL==fopen(“YOK”,“r”))
{
printf(“>>%s错误号:%d\n”,str,errno);
retVal=-errno;
}
返回返回;
}
作废打印\消息\功能(作废*ptr)
{
内部检索;
thdata*数据;
数据=(thdata*)ptr;
retVal=createError(“线程”);
而(1)
;
pthread_退出(0);
}
int main()
{
int-retVal=0;
pthread_t thread1;
thdata数据1;
data1.thread_no=1;
strcpy(data1.message,“你好!”);
/*if(fork())
打印消息功能(“foo”);
*/
pthread_create(&thread1,NULL,(void*)和print_message_函数,(void*)和data1);
retVal=createError(“主”);
而(1)
{
}
pthread_join(thread1,NULL);
出口(0);
}

pthread函数返回错误值,但不设置
errno
。这可能是因为在设计pthread时,
errno
不是特定于线程的

用法示例:

int err = pthread_create(...);
if(err) {
    fprintf(stderr, "pthread_create: (%d)%s\n", err, strerror(err));
    exit(EXIT_FAILURE);
}

pthread函数返回错误值,但不设置
errno
。这可能是因为在设计pthread时,
errno
不是特定于线程的

用法示例:

int err = pthread_create(...);
if(err) {
    fprintf(stderr, "pthread_create: (%d)%s\n", err, strerror(err));
    exit(EXIT_FAILURE);
}

pthread函数返回错误值,但不设置
errno
。这可能是因为在设计pthread时,
errno
不是特定于线程的

用法示例:

int err = pthread_create(...);
if(err) {
    fprintf(stderr, "pthread_create: (%d)%s\n", err, strerror(err));
    exit(EXIT_FAILURE);
}

pthread函数返回错误值,但不设置
errno
。这可能是因为在设计pthread时,
errno
不是特定于线程的

用法示例:

int err = pthread_create(...);
if(err) {
    fprintf(stderr, "pthread_create: (%d)%s\n", err, strerror(err));
    exit(EXIT_FAILURE);
}

您的
print\u message\u函数
的返回类型错误,它应该返回
void*
,并且不要将传递给
pthread\u create
的函数强制转换为
void*
,它应该是
void*(*)(void*)
而不是
void*
,因此,如果您只是修复返回类型,则无需强制转换。您的
print_message_函数
的返回类型错误,它应该返回
void*
,而不要将传递给
pthread\u create
的函数强制转换为
void*
,它应该是
void*(*)(void*)
而不是
void*
,因此,如果您只是修复返回类型,则无需强制转换。您的
print_message_函数
的返回类型错误,它应该返回
void*
,而不要将传递给
pthread\u create
的函数强制转换为
void*
,它应该是
void*(*)(void*)
而不是
void*
,因此,如果您只是修复返回类型,则无需强制转换。您的
print_message_函数
的返回类型错误,它应该返回
void*
,而不要将传递给
pthread\u create
的函数强制转换为
void*
,它应该是
void*(*)(void*)
而不是
void*
,因此,如果您只是修复返回类型,则无需强制转换它。谢谢您的回答。但是我想在由线程\u create调用的print\u message\u函数中设置errno。@zafer victory我想在print\u message\u函数中设置errno,然后尽一切努力。谢谢你的回答。但是我想在由线程\u create调用的print\u message\u函数中设置errno。@zafer victory我想在print\u message\u函数中设置errno,然后尽一切努力。谢谢你的回答。但是我想在由线程\u create调用的print\u message\u函数中设置errno。@zafer victory我想在print\u message\u函数中设置errno,然后尽一切努力。谢谢你的回答。但是我想在由线程\u create调用的print\u message\u函数中设置errno。@zafer victory我想在print\u message\u函数中设置errno,然后无论如何都要这样做。