linux下多线程编程

软件和网站开发以及相关技术探讨
回复
weihua2008
帖子: 448
注册时间: 2008-07-10 15:08

linux下多线程编程

#1

帖子 weihua2008 » 2008-08-29 17:43

linux下多线程编程似乎很简单
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>

void *thread_fun(void *argc)
{
printf(" I'm the child thread !\n");
return (void*)0;
}
int main()
{
pthread_t thread_id;
int err;
err=pthread_create(&thread_id,NULL,(void*)thread_fun,NULL);
if(err<0){
perror("创建子线程失败");
exit(1);
}
printf("I'm the parent thread!\n");
sleep(1);
return 0;
}
上面的函数用gcc -Wall thread.c -lpthread -o thread
可以生成可执行函数,并且可以运行,只是没有显示包含sleep的头文件,有知道的可以留言,谢谢!
但是在linix下编程又不时太简单如下:
#ifndef THREAD_H_
#define THREAD_H_
class Thread{
public:
void *thread_fun(void* arc);
void start();

};
#endif
源文件:
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include"thread.h"
void *Thread::thread_fun(void *argc)
{
printf(" I'm the child thread !\n");
return (void*)0;
}
void Thread::start()
{
pthread_t thread_id;
int err;
err=pthread_create(&thread_id,NULL,(void*)thread_fun,NULL);
if(err<0){
perror("创建子线程失败");
exit(1);
sleep(1);
}
}
因为涉及到类要用该g++.结果就出错了
:错误: invalid use of member (did you forget the ‘&’ ?)
将红色行改为
err=pthread_create(&thread_id,NULL,(void*)&thread_fun,NULL);
又出错了:
初始化实参 3,属于 ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
再把 线程函数改为静态的也是过不去;问题酒精出现在哪?
请高手指教,吾将不胜感激!
weihua2008
帖子: 448
注册时间: 2008-07-10 15:08

#2

帖子 weihua2008 » 2008-08-29 17:53

我刚查到sleep()函数的头文件是#include<unistd.h>
分享快乐!
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#3

帖子 BigSnake.NET » 2008-08-29 18:02

C++ 成员函数的调用不同的吧。。
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#4

帖子 BigSnake.NET » 2008-08-29 18:28

^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
poet
帖子: 2841
注册时间: 2006-09-11 22:47

#5

帖子 poet » 2008-08-29 19:38

c++下面解决pthread_create的问题确实略为麻烦。

但其实在Linux下绝大多数情况是不需要使用线程的。使用进程足以。

有人从windows的经验过来认为进程开销比线程大,不过我相信它一定没有实地测试过,在Linux下线程和进程的效率差异对于大多数普通应用来说是不存在的,效率上没有区别。

那么对于那些少数的,线程和进程效率差异会很大的程序如何处理?其实我的回答就是:别用c++。
weihua2008
帖子: 448
注册时间: 2008-07-10 15:08

#6

帖子 weihua2008 » 2008-08-30 11:56

BigSnake.NET
谢谢,
weihua2008
帖子: 448
注册时间: 2008-07-10 15:08

#7

帖子 weihua2008 » 2008-08-30 14:55

BigSnake.NET
她的方法我试过了,不好使,也不知道是到底多笨,你再给说说
lew0001
帖子: 31
注册时间: 2008-06-06 21:45

#8

帖子 lew0001 » 2008-08-30 23:09

#include <pthread.h>
class Thread{
public:
void start();

private:
pthread_t thread_id;
static void *thread_fun(void* arc);

};
#include <unistd.h>
#include<stdio.h>
#include<stdlib.h>
void *Thread::thread_fun(void *argc)
{
Thread *thread = (Thread *) argc;
printf(" I'm the child thread !\n");
return (void*)0;
}
void Thread::start()
{
int err;
err=pthread_create(&thread_id,NULL,thread_fun,this);
if(err<0){
perror("创建子线程失败\n");
exit(1);
sleep(1);
}
}

int main(void)
{
Thread thread;
thread.start();
}
上次由 lew0001 在 2008-08-31 12:46,总共编辑 1 次。
头像
yangcheng
帖子: 498
注册时间: 2005-09-27 18:24
来自: 杭州

#9

帖子 yangcheng » 2008-08-31 10:35

poet 写了:c++下面解决pthread_create的问题确实略为麻烦。

但其实在Linux下绝大多数情况是不需要使用线程的。使用进程足以。

有人从windows的经验过来认为进程开销比线程大,不过我相信它一定没有实地测试过,在Linux下线程和进程的效率差异对于大多数普通应用来说是不存在的,效率上没有区别。

那么对于那些少数的,线程和进程效率差异会很大的程序如何处理?其实我的回答就是:别用c++。
恩..phread库本来就是软实现的,,
weihua2008
帖子: 448
注册时间: 2008-07-10 15:08

#10

帖子 weihua2008 » 2008-09-01 11:40

lew0001
谢谢你,我试过了行得通,
头像
openware
帖子: 173
注册时间: 2009-02-22 14:15
联系:

Re: linux下多线程编程

#11

帖子 openware » 2009-09-04 11:41

呵呵,经典。。。
Desire Freedom, Love Beauty, Keep Hacking。
回复