1、线程的创建
#include <stdio.h>
#include <pthread.h>
void Mythread1()
{
int i;
for(i = 0;i < 100;i++)
{
printf("The thread1 is create!\n");
sleep(1);
}
}
void Mythread2()
{
int i;
for(i = 0;i < 100;i++)
{
printf("The thread2 is create!\n");
sleep(1);
}
}
int main()
{
int ret;
pthread_t id1;
pthread_t id2;
ret = pthread_create(&id1,NULL,(void *)Mythread1,NULL);
if(ret < 0)
{
printf("Create pthread failed!\n");
return 1;
}
ret = pthread_create(&id2,NULL,(void *)Mythread2,NULL);
if(ret < 0)
{
printf("Create pthread failed!\n");
return 1;
}
pthread_join(id1,NULL);
pthread_join(id2,NULL);
return 0;
}
The thread1 is create!
The thread2 is create!
The thread1 is create!
The thread2 is create!
The thread1 is create!
The thread2 is create!
The thread1 is create!
The thread2 is create!
#include <stdio.h>
#include <string.h>
#include <pthread.h>
void *pass(void *arg)
{
char ptr[10] ={0};
strcpy(ptr,arg);
printf("pass %s is done!\n",ptr);
return NULL;
}
int main()
{
pthread_t id;
int ret;
char str[10] = {0};
printf("Please input the string:\n");
scanf("%s",str);
ret = pthread_create(&id,NULL,pass,str);
if(ret < 0)
{
printf("Create pthread failed!\n");
return 1;
}
sleep(1);
printf("Pthread is created...\n");
pthread_join(id,NULL);
return 0;
}
Please input the string:
hello
pass hello is done!
Pthread is created...
#include <stdio.h>
#include <pthread.h>
void *Pass(void *arg)
{
int *num;
num = (int *) arg;
printf("Num is passed!Num is %d\n",*num);
return NULL;
}
int main()
{
pthread_t id;
int ret;
int pass;
void *attr = &pass;
printf("Please input the pass num:\n");
scanf("%d",&pass);
ret = pthread_create(&id,NULL,Pass,attr);
if(ret < 0)
{
printf("Create pthread failed!\n");
return 1;
}
sleep(1);
printf("pthread is created...\n");
return 0;
}
调试结果:
Please input the pass num:
1
Num is passed!Num is 1
pthread is created...
4、线程参数传递,传递数据结构:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
struct test
{
int num;
char *str;
};
void *pass(void *arg)
{
struct test *tmp;
tmp = (struct test *)arg;
printf("Struct is passed!\nThe passsed num is %d,the passed char is %s\n",tmp->num,tmp->str);
return NULL;
}
int main()
{
pthread_t tidp;
int ret;
struct test *T1;
T1 = (struct test*)malloc(sizeof(struct test));
T1->num = 3;
T1->str = "daw";
ret = pthread_create(&tidp,NULL,pass,(void *)T1);
if(ret < 0)
{
printf("Create pthread failed!\n");
return 1;
}
sleep(1);
printf("Pthread is created...\n");
pthread_join(tidp,NULL);
return 0;
}
调试结果:
Struct is passed!
The passsed num is 3,the passed char is daw
Pthread is created...
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mutex;
void *fun1(void *arg)
{
int val;
val = pthread_mutex_lock(&mutex);
if(val != 0)
{
printf("Lock error!\n");
return 1;
}
printf("This fun1!\n");
pthread_mutex_unlock(&mutex);
return NULL;
}
void *fun2(void *arg)
{
int val;
val = pthread_mutex_lock(&mutex);
if(val != 0)
{
printf("Lock error!\n");
return 1;
}
printf("This fun2!\n");
pthread_mutex_unlock(&mutex);
return NULL;
}
int main()
{
pthread_t id1;
pthread_t id2;
if(pthread_mutex_init(&mutex,NULL) != 0)
{
printf("Init mutex failed!\n");
return 1;
}
if(pthread_create(&id1,NULL,fun1,NULL) < 0)
{
printf("Create pthread1 failed!\n");
return 1;
}
if(pthread_create(&id2,NULL,fun2,NULL) < 0)
{
printf("Create pthread2 failed!\n");
return 1;
}
sleep(1);
printf("Pthread is created!\n");
pthread_join(id1,NULL);
pthread_join(id2,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
调试结果:
This fun1!
This fun2!
Pthread is created!