linux下多线程编程
-
- 帖子: 448
- 注册时间: 2008-07-10 15:08
linux下多线程编程
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*)’
再把 线程函数改为静态的也是过不去;问题酒精出现在哪?
请高手指教,吾将不胜感激!
#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*)’
再把 线程函数改为静态的也是过不去;问题酒精出现在哪?
请高手指教,吾将不胜感激!
-
- 帖子: 448
- 注册时间: 2008-07-10 15:08
- BigSnake.NET
- 帖子: 12522
- 注册时间: 2006-07-02 11:16
- 来自: 廣州
- 联系:
- BigSnake.NET
- 帖子: 12522
- 注册时间: 2006-07-02 11:16
- 来自: 廣州
- 联系:
-
- 帖子: 2841
- 注册时间: 2006-09-11 22:47
-
- 帖子: 448
- 注册时间: 2008-07-10 15:08
-
- 帖子: 448
- 注册时间: 2008-07-10 15:08
-
- 帖子: 31
- 注册时间: 2008-06-06 21:45
#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();
}
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
- 来自: 杭州
-
- 帖子: 448
- 注册时间: 2008-07-10 15:08
- openware
- 帖子: 173
- 注册时间: 2009-02-22 14:15
- 联系: