线程的基本操作

2019-06-17  本文已影响0人  suntwo

函数原型

#include <pthread.h>

int pthread_create(pthread_t *restrict thread,
    const pthread_attr_t *restrict attr,
    void *(*start_routine)(void*), void *restrict arg);

创建线程的函数有四个参数,第一个参数是一个pthread_t类型的指针。第二个参数通常设置为NULL,第三个参数是新创建的线程的函数,返回类型为(void *),第四个参数为传递的参数,类型为(void *)类型。

下面给出一个实例

#include<pthread.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<time.h>
void * xiancheng(void *p)
{
pid_t pid;
pthread_t tid;
pid=getpid();
tid=pthread_self();
printf("%s jinchengwei %u xianchengwei %u\n",(char *)p,(unsigned int)pid,(unsigned int)tid);
return (void *)0;
}
int main()
{
pthread_t id;
int err=pthread_create(&id,NULL,xiancheng,"hello");
if(err!=0)
{
printf("error\n");
return 0;
}
xiancheng("zhi");
sleep(1);
}

使用命令gcc pthreap.c - o pthreap -lpthreap进行编译
在这里我们需要注意到几个点

结束子线程

线程的结束有几种方式

下面进行一个实例

#include<pthread.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<time.h>
void * xiancheng(void *argv)
{
return (void *)0;
}
void * xiancheng1(void *argv)
{
pthread_exit((void *)1);
}
void * xiancheng2(void *argv)
{
while(1)
{
sleep(1);
printf("waitting 4s\n");
}

}

int main()
{
pthread_t id;
void * a;
pthread_create(&id,NULL,xiancheng,NULL);
pthread_join(id,&a);
printf("%d\n",(int)a);
pthread_create(&id,NULL,xiancheng1,NULL);
pthread_join(id,&a);
printf("%d\n",(int)a);

pthread_create(&id,NULL,xiancheng2,NULL);
sleep(3);
pthread_cancel(id);
pthread_join(id,&a);
printf("%d\n",(int)a);
}

结果:
root@ubuntu:/home/sun/project# ./aa
0
1
waitting 4s
waitting 4s
-1

线程间的同步

当有多个线程同时访问一个共享变量时便会出现问题,可能并不是我们预期的结果出现,接下来我们分析一下原因。

代码实例

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<sys/types.h>
int count=0;
void *pthread1(void * argc)
{
int i=0;
for(i=0;i<100000;++i)
count++;
return (void *)0;
}

void *pthread2(void * argc)
{

int i=0;
for(i=0;i<100000;++i)
count++;
return (void *)0;
}

int main()
{

pthread_t tid1,tid2;
pthread_create(&tid2,NULL,pthread1,NULL);
pthread_create(&tid2,NULL,pthread2,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
printf("%d",count);
return 0;
}

运行代码结果

在单核上运行结果
root@ubuntu:/home/sun/project# ./aa
200000root@ubuntu:/home/sun/project# ./aa
200000root@ubuntu:/home/sun/project# ./aa
200000root@ubuntu:/home/sun/project# ./aa
200000root@ubuntu:/home/sun/project# ./aa
200000root@ubuntu:/home/sun/project# ./aa
200000root@ubuntu:/home/sun/project# ./aa
200000root@ubuntu:/home/sun/project# ./aa
200000root@ubuntu:/home/sun/project# ./aa
200000root@ubuntu:/home/sun/project# ./aa
200000root@ubuntu:/home/sun/project# ./aa
200000root@ubuntu:/home/sun/project# ./aa
200000root@ubuntu:/home/sun/project# 


在多核上运行结果
root@ubuntu:/home/sun/project# ./aa
111388root@ubuntu:/home/sun/project# ./aa
127061root@ubuntu:/home/sun/project# ./aa
108070root@ubuntu:/home/sun/project# ./aa
101543root@ubuntu:/home/sun/project# ./aa
141164root@ubuntu:/home/sun/project# ./aa
200000root@ubuntu:/home/sun/project# ./aa
129528root@ubuntu:/home/sun/project# ./aa
148950root@ubuntu:/home/sun/project# ./aa
122621root@ubuntu:/home/sun/project# ./aa
109718root@ubuntu:/home/sun/project# ./aa
157505root@ubuntu:/home/sun/project# ./aa
183777root@ubuntu:/home/sun/project# 


分析

可以看到我们使用多线程访问一个共享变量时在多核上回出现错误,我们分析一下出现错误的原因。

正常的程序运行顺序

上一篇 下一篇

猜你喜欢

热点阅读