在Linux下用C++创建新线程

做图形界面,想用多线程做后台数据处理,只需可以创建一个新线程,执行一函数即可,下面是采用了一种C下的方法,在C++下编译总是出错,请问在C++下该怎么改

void thread(void)
{
printf ("The child process...\n");
}

int main(int argc, char *argv[])
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,(void *)thread(),NULL);
if(ret!=0)
{
printf ("Create pthread error!\n");
exit (1);
}

}

第1个回答  2010-06-14
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void* thread(void* arg)
{
printf ("The child process...\n");
}

int main(int argc, char *argv[])
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,(void *)thread,NULL);
if(ret!=0)
{
printf ("Create pthread error!\n");
exit (1);
}

}
程序如上就可以编译。
它属于linux下C编程中多线程编程的范围。
用命令
gcc -lpthread 1.c -o 1
./1
就可以出结果。
多线程编程的基础可以参考
http://hi.baidu.com/huifeng00/blog/item/ed13ddc0d6c59c170ff47715.html本回答被提问者采纳
第2个回答  2010-06-14
thread的函数的返回值改成 void *
相似回答