当前位置: 技术问答>linux和unix
100分求解:将这个C语言写的线程程序改写成C++程序
来源: 互联网 发布时间:2015-01-27
本文导语: 源C程序 #include #include #include #include void *thread_function(void *arg); char message[]="Hello World"; int main() { int res; pthread_t a_thread; void *thread_result; res=pthread_create(&a_thread,NULL,threa...
源C程序
#include
#include
#include
#include
void *thread_function(void *arg);
char message[]="Hello World";
int main()
{
int res;
pthread_t a_thread;
void *thread_result;
res=pthread_create(&a_thread,NULL,thread_function,(void *)message);
if(res!=0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...n");
res=pthread_join(a_thread,&thread_result);
if(res!=0)
{
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined,it returned %sn",(char *)thread_result);
printf("Message is now %sn",message);
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
printf("thread_function is running.Argument was %sn",arg);
sleep(3);
strcpy(message,"bye!");
pthread_exit("Thank you for the CPU time");
}
我改写的程序,但编译不通过。
test.h:
#ifndef __TEST_H__
#define __TEST_H__
#include
#include
#include
#include
#include
class test
{
public:
int res;
pthread_t a_thread;
void *thread_result;
char message[10];
void *thread_function(void *arg);
int start();
};
#endif
test.cpp:
#include "Serial1.h"
main()
{
test main;
main.start();
}
int test::start()
{
char a[]="Hello World";
strcpy(message,a);
res=pthread_create(&a_thread,NULL,thread_function,(void *)message);
if(res!=0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...n");
res=pthread_join(a_thread,&thread_result);
if(res!=0)
{
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined,it returned %sn",(char *)thread_result);
printf("Message is now %sn",(char *)message);
exit(EXIT_SUCCESS);
}
void * test::thread_function(void *arg)
{
printf("thread_function is running.Argument was %sn",arg);
sleep(3);
strcpy((char *)arg,"bye!");
pthread_exit(arg);
}
请大家帮忙修改,指点!!
#include
#include
#include
#include
void *thread_function(void *arg);
char message[]="Hello World";
int main()
{
int res;
pthread_t a_thread;
void *thread_result;
res=pthread_create(&a_thread,NULL,thread_function,(void *)message);
if(res!=0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...n");
res=pthread_join(a_thread,&thread_result);
if(res!=0)
{
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined,it returned %sn",(char *)thread_result);
printf("Message is now %sn",message);
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
printf("thread_function is running.Argument was %sn",arg);
sleep(3);
strcpy(message,"bye!");
pthread_exit("Thank you for the CPU time");
}
我改写的程序,但编译不通过。
test.h:
#ifndef __TEST_H__
#define __TEST_H__
#include
#include
#include
#include
#include
class test
{
public:
int res;
pthread_t a_thread;
void *thread_result;
char message[10];
void *thread_function(void *arg);
int start();
};
#endif
test.cpp:
#include "Serial1.h"
main()
{
test main;
main.start();
}
int test::start()
{
char a[]="Hello World";
strcpy(message,a);
res=pthread_create(&a_thread,NULL,thread_function,(void *)message);
if(res!=0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...n");
res=pthread_join(a_thread,&thread_result);
if(res!=0)
{
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined,it returned %sn",(char *)thread_result);
printf("Message is now %sn",(char *)message);
exit(EXIT_SUCCESS);
}
void * test::thread_function(void *arg)
{
printf("thread_function is running.Argument was %sn",arg);
sleep(3);
strcpy((char *)arg,"bye!");
pthread_exit(arg);
}
请大家帮忙修改,指点!!
|
因为pthread_create()不是class test的成员函数也不是友元函数。当将class test的成员函数thread_function()以传址方式传递给pthread_create()时,因为pthread_create()无法直接访问class test成员函数,将会出现访问权限错误。加上static后,因为static以面向对象来理解,在某个class中以static修饰的成员都将作为类直接成员,即不再只是类实例(instance)成员处理,这时的类成员跟结构体(struct)中的成员非常相似,就可以以“类名::成员”的方式,以类似全局作用域的方式进行访问。因此就不会出现访问权限错误。
|
在test的申明中, 把void *thread_function(void *arg); 改为
static void *thread_function(void *arg); 就可以了
static void *thread_function(void *arg); 就可以了