(1)线程创建pthread_create()
- #include <stdio.h>
- #include <pthread.h>
- void *myThread1(void)
- {
- int i;
- for (i=0; i<100; i++)
- {
- printf("This is the 1st pthread,created by zieckey.\n");
- sleep(1);//Let this thread to sleep 1 second,and then continue to run
- }
- }
- void *myThread2(void)
- {
- int i;
- for (i=0; i<100; i++)
- {
- printf("This is the 2st pthread,created by zieckey.\n");
- sleep(1);
- }
- }
- int main()
- {
- int i=0, ret=0;
- pthread_t id1,id2;
- /*创建线程1*/
- ret = pthread_create(&id1, NULL, (void*)myThread1, NULL);
- if (ret)
- {
- printf("Create pthread error!\n");
- return 1;
- }
- /*创建线程2*/
- ret = pthread_create(&id2, NULL, (void*)myThread2, NULL);
- if (ret)
- {
- printf("Create pthread error!\n");
- return 1;
- }
- pthread_join(id1, NULL);
- pthread_join(id2, NULL);
- return 0;
- }
(2)线程等待pthread_join()
- #include <pthread.h>
- #include <unistd.h>
- #include <stdio.h>
- void *thread(void *str)
- {
- int i;
- for (i = 0; i < 10; ++i)
- {
- sleep(2);
- printf( "This in the thread : %d\n" , i );
- }
- return NULL;
- }
- int main()
- {
- pthread_t pth;
- int i;
- int ret = pthread_create(&pth, NULL, thread, (void *)(i));
- pthread_join(pth, NULL);
- printf("123\n");
- for (i = 0; i < 10; ++i)
- {
- //sleep(1);
- printf( "This in the main : %d\n" , i );
- }
- return 0;
- }
(3)线程清除pthread_cleanup_push(),pthread_cleanup_pop()
- #include <stdio.h>
- #include <pthread.h>
- #include <unistd.h>
- void *clean(void *arg)
- {
- printf("cleanup :%s \n",(char *)arg);
- return (void *)0;
- }
- void *thr_fn1(void *arg)
- {
- printf("thread 1 start \n");
- pthread_cleanup_push( (void*)clean,"thread 1 first handler");
- pthread_cleanup_push( (void*)clean,"thread 1 second hadler");
- printf("thread 1 push complete \n");
- if(arg)
- {
- return((void *)1);
- }
- pthread_cleanup_pop(0);
- pthread_cleanup_pop(0);
- return (void *)1;
- }
- void *thr_fn2(void *arg)
- {
- printf("thread 2 start \n");
- pthread_cleanup_push( (void*)clean,"thread 2 first handler");
- pthread_cleanup_push( (void*)clean,"thread 2 second handler");
- printf("thread 2 push complete \n");
- if(arg)
- {
- pthread_exit((void *)2);
- }
- pthread_cleanup_pop(0);
- pthread_cleanup_pop(0);
- pthread_exit((void *)2);
- }
- int main(void)
- {
- int err;
- pthread_t tid1,tid2;
- void *tret;
- err=pthread_create(&tid1,NULL,thr_fn1,(void *)1);
- if(err!=0)
- {
- printf("error .... \n");
- return -1;
- }
- err=pthread_create(&tid2,NULL,thr_fn2,(void *)1);
- if(err!=0)
- {
- printf("error .... \n");
- return -1;
- }
- err=pthread_join(tid1,&tret);
- if(err!=0)
- {
- printf("error .... \n");
- return -1;
- }
- printf("thread 1 exit code %d \n",(int)tret);
- err=pthread_join(tid2,&tret);
- if(err!=0)
- {
- printf("error .... ");
- return -1;
- }
- printf("thread 2 exit code %d \n",(int)tret);
- return 1;
- }
(4)线程传参thread_int.c,thread_string.c,thread_struct.c
- #include <stdio.h>
- #include <pthread.h>
- #include <unistd.h>
- void *create(void *arg)
- {
- int *num;
- num=(int *)arg;
- printf("create parameter is %d \n",*num);
- return (void *)0;
- }
- int main(int argc ,char *argv[])
- {
- pthread_t tidp;
- int error;
- int test=4;
- int *attr=&test;
- error=pthread_create(&tidp,NULL,create,(void *)attr);
- if(error)
- {
- printf("pthread_create is created is not created ... \n");
- return -1;
- }
- sleep(1);
- printf("pthread_create is created ...\n");
- return 0;
- }
- #include <pthread.h>
- #include <stdio.h>
- #include <unistd.h>
- void *create(void *arg)
- {
- char *name;
- name=(char *)arg;
- printf("The parameter passed from main function is %s \n",name);
- return (void *)0;
- }
- int main(int argc, char *argv[])
- {
- char *a="zieckey";
- int error;
- pthread_t tidp;
- error=pthread_create(&tidp, NULL, create, (void *)a);
- if(error!=0)
- {
- printf("pthread is not created.\n");
- return -1;
- }
- sleep(1);
- printf("pthread is created... \n");
- return 0;
- }
- #include <stdio.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <stdlib.h>
- struct menber
- {
- int a;
- char *s;
- };
- void *create(void *arg)
- {
- struct menber *temp;
- temp=(struct menber *)arg;
- printf("menber->a = %d \n",temp->a);
- printf("menber->s = %s \n",temp->s);
- return (void *)0;
- }
- int main(int argc,char *argv[])
- {
- pthread_t tidp;
- int error;
- struct menber *b;
- b=(struct menber *)malloc( sizeof(struct menber) );
- b->a = 4;
- b->s = "zieckey";
- error = pthread_create(&tidp, NULL, create, (void *)b);
- if( error )
- {
- printf("phread is not created...\n");
- return -1;
- }
- sleep(1);
- printf("pthread is created...\n");
- return 0;
- }
(5)进程和线程共享数据thread_share.c
- #include <stdio.h>
- #include <pthread.h>
- #include <unistd.h>
- //static int a=4;
- int a = 1;
- void *create(void *arg)
- {
- printf("new pthread ... \n");
- printf("a=%d \n",a);
- return (void *)0;
- }
- int main(int argc,char *argv[])
- {
- pthread_t tidp;
- int error;
- int a=5;
- printf("a = %d\n",a);
- error=pthread_create(&tidp, NULL, create, NULL);
- if(error!=0)
- {
- printf("new thread is not create ... \n");
- return -1;
- }
- sleep(1);
- printf("new thread is created ... \n");
- return 0;
- }