c 多线程进程

发布于 2024-11-06 09:53:07 字数 1263 浏览 1 评论 0原文

我想用c语言写一个多线程程序。我使用 posix 线程库。

我编写了以下代码:

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

void *put (int *arg)
{
    int i;
    //int * p;
    // p=(int*)arg;
    for(i=0;i<5;i++)
    { 
        printf("\n%d",arg[i]);
    }
    pthread_exit(NULL);
}

int main()
{
    int a[5]={10,20,30,40,50};
    pthread_t s;
    pthread_create(&s,NULL,put,a);
    printf("what is this\n");
    return 0;
}

我只想让我的线程仅显示数组中的项目。程序编译时出现以下警告:

tm.c:19: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(int *)’

当我运行程序时,我得到了主线程的输出,但没有得到存储在数组中的值。

现在谁能告诉我我做错了什么? 如何将数组作为线程函数中的参数发送?

如果我只是稍微更改了代码,编译时警告将解决更改后的代码如下:

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


void *put (void *arg)
{
    int i;
    int * p;
    p=(int*)arg;
    for(i=0;i<5;i++)
    { 
        printf("\n%d",p[i]);
    }
    pthread_exit(NULL);
}

int main()
{
    int a[5]={10,20,30,40,50};
    pthread_t s;
    pthread_create(&s,NULL,put,a);
    printf("what is this\n");
    return 0;
}

但输出不会更改。谁能告诉我我做错了什么?将数组发送到线程函数(在本例中放置)的正确方法是什么?

I want to write a multi threaded program in c language. I use posix thread library.

I write the following code:

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

void *put (int *arg)
{
    int i;
    //int * p;
    // p=(int*)arg;
    for(i=0;i<5;i++)
    { 
        printf("\n%d",arg[i]);
    }
    pthread_exit(NULL);
}

int main()
{
    int a[5]={10,20,30,40,50};
    pthread_t s;
    pthread_create(&s,NULL,put,a);
    printf("what is this\n");
    return 0;
}

I just want my thread just show the items in the array. The program compiled with following warning:

tm.c:19: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
/usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(int *)’

When I run the program I got the out put of main thread but not the value stored in array.

Now can anyone tell me what I'm doing wrong?
How to send the array as an argument in the thread function?

If I just changed the code little bit the compile time warning resolved the changed code is following:

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


void *put (void *arg)
{
    int i;
    int * p;
    p=(int*)arg;
    for(i=0;i<5;i++)
    { 
        printf("\n%d",p[i]);
    }
    pthread_exit(NULL);
}

int main()
{
    int a[5]={10,20,30,40,50};
    pthread_t s;
    pthread_create(&s,NULL,put,a);
    printf("what is this\n");
    return 0;
}

But the output does not change. Can any one tell me what i did wrong? What is the proper way to send array to a thread function (put in this case)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Oo萌小芽oO 2024-11-13 09:53:07

您的代码创建线程,然后进程通过到达 main 末尾而退出。您必须通过调用 pthread_join 来等待线程有机会执行,或者休眠一会儿。

Your code creates the thread and then the process exits by reaching the end of main. You have to wait for the thread to have a chance to execute, by calling pthread_join, or sleeping for a bit.

一场信仰旅途 2024-11-13 09:53:07

首先,您需要等待线程完成才能从 main() 返回。

此代码的另一个问题是数组 a 分配在 main() 例程的堆栈上,因此在不同线程的上下文中可能无效。您应该通过调用 malloc() 来堆分配 a

如果您等待线程在 main() 中完成,则 a 可能有效,因为 main() 的堆栈帧仍然存在。但是,任何未来的重构都可能会给您带来痛苦,因此请改用 malloc()

First of all, you need to wait for the thread to finish before returning from main().

Another problem with this code is that the array a is allocated on the stack of the main() routine and is thus potentially invalid in the context of a different thread. You should heap allocate a with a call to malloc().

If you wait for the thread to finish in main() then a is probably valid since the stack frame for main() will still exist. However, any future refactorings are liable to cause you grief so please switch to using malloc().

独自←快乐 2024-11-13 09:53:07

尝试等待线程执行,

pthread_join(s, NULL);

return 0; 之前添加

Try to wait for your thread to execute, add

pthread_join(s, NULL);

before your return 0;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文