- 论坛徽章:
- 1
|
本帖最后由 AnkerLi 于 2015-03-05 23:21 编辑
进程里创建线程的函数原型如下:
int pthread_create ((pthread_t *thread, pthread_attr_t *attr,void *(*start_routine)(void *), void *arg))
以下是这一函数的运用(标红为该函数的引用):
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
/*线程一*/
void thread1(void)
{
int i=0;
for(i=0;i<6;i++){
printf("This is a pthread1.\n");
if(i==2)
pthread_exit(0);
sleep(1);
}
}
/*线程二*/
void thread2(void)
{
int i;
for(i=0;i<3;i++)
printf("This is a pthread2.\n");
pthread_exit(0);
}
int main(void)
{
pthread_t id1,id2;
int i,ret;
/*创建线程一*/
ret=pthread_create(&id1,NULL,(void *) thread1, NULL);
if(ret!=0){
printf ("Create pthread error!\n");
exit (1);
}
/*创建线程二*/
ret=pthread_create(&id2,NULL,(void *) thread2,NULL);
if(ret!=0){
printf ("Create pthread error!\n");
exit (1);
}
/*等待线程结束*/
pthread_join(id1,NULL);
pthread_join(id2,NULL);
exit (0);
}
本人对于标红中参数的供给,(void *) thread1,(void *) thread2,不是很理解,这里是给线程函数的起始地址,不是直接thread1,thread2这样就可以给出该函数的地址了吗?前面的(void *) 是什么意思?起到什么作用? |
|