16,线程

线程介绍
线程是一种轻量级的进程
线程不含有资源

线程操作
创建线程:pthread_create()函数
线程终止:pthread_cancel()函数
等待线程结束:pthread_join()函数
线程间的同步和互斥


实例源码1:pthread_create.c
//pthread_create.c
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

pthread_t ntid;

//公有函数,打印进程,线程ID
void printids(const char *s)
{

pid_t pid;

pthread_t tid;


pid = getpid();

tid = pthread_self();


printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,

(unsigned int)tid, (unsigned int)tid);

}

//新线程函数
void *thr_fn(void *arg)
{

printids(arg);


return NULL;

}

int main(void)
{

int err;

void *ThreadReault;


//创建新线程

err = pthread_create(&ntid, NULL, thr_fn, "New thread: ");


if(err != 0)

{

fprintf(stderr, "can't create thread! \n\n", strerror(err));

wait(1);

}


printids("main thread: ");


if(0 != pthread_join(ntid, &ThreadReault))//等待新线程结束

printf("wait thread fail!\n");


return 0;

}

运行结果:
终端输入:
#gcc pthread_create.c -lpthread
#./a.out
显示结果:
main thread:  pid 11265 tid 3078026944 (0xb776f6c0)
New thread:  pid 11265 tid 3078024048 (0xb776eb70)

注意: pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以 在编译中要加 -lpthread参数,否则,编译时出现如下错误:
undefined reference to 'pthread_create'
undefined reference to 'pthread_join'


实例源码2:pthread_cancle.c
//pthread_cancel.c
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

//新线程1函数
void *thr_fn1(void *arg)
{

printf("thread 1 returning\n");


return (void *)1;

}

//新线程2函数
void *thr_fn2(void *arg)
{

printf("thread 2 exiting\n");


pthread_exit((void *)2);

}

//新线程3函数
void *thr_fn3(void *arg)
{

//死循环,每隔1s,打印一次

while(1)

{

printf("thread 3 writing\n");

sleep(1);

}

}

int main(void)
{

pthread_t tid;

void *tret;


//创建新线程1

pthread_create(&tid, NULL, thr_fn1, NULL);

pthread_join(tid, &tret);

printf("thread 1 exit code %d!\n", (int)tret);


//创建新线程2

pthread_create(&tid, NULL, thr_fn2, NULL);

pthread_join(tid, &tret);

printf("thread 2 exit code %d!\n", (int)tret);


//创建新线程3

pthread_create(&tid, NULL, thr_fn3, NULL);

sleep(3);//休眠3s,便于观察线程3执行过程

pthread_cancel(tid);//终止线程

pthread_join(tid, &tret);

printf("thread 3 exit code %d!\n", (int)tret);


return 0;

}


运行结果:
终端输入:
#gcc pthread_cancel.c -lpthread
#./a.out
显示结果:
thread 1 returning
thread 1 exit code 1!
thread 2 exiting
thread 2 exit code 2!
thread 3 writing
thread 3 writing
thread 3 writing
thread 3 exit code -1!


实例源码3:pthread_mutex.c
//pthread_mutex.c

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

int counter;
pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;

void *update(void *vptr)
{

int val;


while(1)

{

pthread_mutex_lock(&counter_mutex);

val = counter;

printf("%x: %d\n", (unsigned int)pthread_self(), val + 1);

counter = val + 1;

pthread_mutex_unlock(&counter_mutex);

sleep(1);

}



return  NULL;

}


int main(void)
{

pthread_t tha, thb;



pthread_create(&tha, NULL, update, NULL);

pthread_create(&thb, NULL, update, NULL);

pthread_join(tha, NULL);

pthread_join(tha, NULL);


return 0;

}


运行结果:
终端输入:
#gcc pthread_mutex.c -lpthread
#./a.out
显示结果:
b70d1b70: 1
b78d2b70: 2
b70d1b70: 3
b78d2b70: 4
b70d1b70: 5
b78d2b70: 6
...















































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值