linux编译多线程命令 linux多线程程序设计

Linux多线程编程

程序代码test.c共两个线程,一个主线程,一个读缓存区的线程卖腔:

创新互联建站坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站建设、成都网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的五华网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

#include pthread.h

#include stdio.h

#include stdlib.h

#include string.h

#include unistd.h

char globe_buffer[100];

void *read_buffer_thread(void *arg); //这里先声明一下读缓存的线中拆衫程,具体实现写在后面了

int main()

{

int res,i;

pthread_t read_thread;

for(i=0;i20;i++)

globe_buffer[i]=i;

printf("\nTest thread : write buffer finish\n");

sleep(3);\\这里的3秒是多余,可以不要。

res = pthread_create(read_thread, NULL, read_buffer_thread, NULL);

if (res != 0)

{

printf("Read Thread creat Error!");

exit(0);

}

sleep(1);

printf("waiting for read thread to finish...\n");

res = pthread_join(read_thread, NULL);

if (res != 0)

{

printf("read thread join failed!\n");

exit(0);

}

printf("read thread test OK, have fun!! exit ByeBye\n");

return 0;

}

void *read_buffer_thread(void *arg)

{

int i,x;

printf("Read buffer thread read data : \n");

for(i=0;i20;i++)

{

x=globe_buffer[i];

printf("%d ",x);

globe_buffer[i]=0;//清空

}

printf("\nread over\n");

}

---------------------------------------------------------------------------------

以上程御毕序编译:

gcc -D_REENTRANT test.c -o test.o –lpthread

运行这个程序:

$ ./test.o:

关于linux下多线程编程

pthread_join 线程停止等待函数没有调用

pthread_create 线程生成后,没有等子线程停止,败信主线程就先停止了。

主线程停止悉桐后,整个程序停止,子线程在没有printf的时候就被结束了。

结论:不是你没有看到结果,而是在子线程printf("..................\n");之前整个程序就已经停止了。

#include stdio.h

#include stdlib.h察陆轮

#include sys/types.h

#include string.h

#include unistd.h

#include pthread.h

#define FALSE -1

#define TRUE 0

void *shuchu( void *dumy )

{

int j;

printf("..................\n");

}

int main()

{

int i = 0;

int rc = 0;

int ret1;

pthread_t p_thread1;

if(0!=(ret1 = pthread_create(p_thread1, NULL, shuchu, NULL)))printf("sfdfsdfi\n");

printf("[%d]\n",p_thread1);

pthread_join(p_thread1, NULL);

return TRUE;

}

LINUX下多线程编译问题

你编余李译的时候有加多线程连接选项吗? 要加厅毁慧上 -lpthread 或者 -pthread (尽量选后者)

例如 gcc -pthread -o test main.cpp

另外你的线程创建的不对,函扮答数指针不能强转类型(这里也不用转)

pthread_create(producter_t,NULL,(void*)producter_f,NULL);

pthread_create(consumer_t,NULL,(void*)consumer_f,NULL);

应该是

pthread_create(producter_t,NULL,producter_f,NULL);

pthread_create(consumer_t,NULL,consumer_f,NULL);

linux下c、c++下多线程编译?

C语言要求除main函数外 所有的函数必须先声明才能使用 你可以在函数定义的时候一起声明这个函数 但是在这个函数定义之前不能使用这个函瞎没数

下面用通俗点的语言讲: 你在main函数中调用了thread函数, 但是如果你把void *thread(void *vargp);删掉了, 那么编译器就找不到这个函数了(因为编译器是从前往后编译这个程序的) 因此编译器是通不过的

有两种方法, 一种就是像你之前写的 先声明这个函数 第二种是把void *thread(void *vargp){...}放到main函数的前面.

有些返回值和参数类磨乎纳型为int型的函数顷嫌也可以不用声明, 编译器也能通过, 不过这是不建议的 也是不符合标准的

标准建议每个函数都应该给出它们的显式的声明

希望你懂了 嘿嘿

当前名称:linux编译多线程命令 linux多线程程序设计
浏览路径:https://www.cdcxhl.com/article12/ddpopgc.html

成都网站建设公司_创新互联,为您提供微信公众号网站导航网站收录手机网站建设网站设计面包屑导航

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站网页设计