c 多线程进程
我想用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码创建线程,然后进程通过到达
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 callingpthread_join
, or sleeping for a bit.首先,您需要等待线程完成才能从
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 themain()
routine and is thus potentially invalid in the context of a different thread. You should heap allocatea
with a call tomalloc()
.If you wait for the thread to finish in
main()
thena
is probably valid since the stack frame formain()
will still exist. However, any future refactorings are liable to cause you grief so please switch to usingmalloc()
.尝试等待线程执行,
在
return 0;
之前添加Try to wait for your thread to execute, add
before your
return 0;