- 论坛徽章:
- 0
|
程序基本就是高程里面的内容了,如下:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pthread.h>
pthread_t pthid;
void printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid=getpid();
tid=pthread_self();
//tid=pthid;
printf("%s pid %u tid %u (0x%x)\n", s,pid, tid, tid);
}
void *thr_fn(void *arg)
{
printids("new thread: ");
//return(0);
pthread_exit((void *)2);
}
int main(void)
{
int err;
void *t_ret;
err=pthread_create(&pthid,NULL,thr_fn,NULL);
if(err!=0) printf("creat thread unsuccessful!\n");
printids("main thread: ");
err=pthread_join(pthid,&t_ret);
if(err!=0) printf("cannot join with phtid!\n");
printf("thread exit code: %d \n",t_ret);
//sleep(1);
exit(0);
}
运行环境:Solaris 9
编译和运行1:
# gcc myPthread.c
# a.out
creat thread unsuccessful!
main thread: pid 23984 tid 1 (0x1)
thread exit code: 4
#
编译和运行2:
# gcc myPthread.c -lpthread
# a.out
main thread: pid 23990 tid 1 (0x1)
new thread: pid 23990 tid 2 (0x2)
thread exit code: 2
# |
|