Linux高级编程——线程(2)

结束线程方法:

  • 线程返回

  • 调用 pthread_exit 函数(自杀)

  • 调用 pthread_cancel 函数(他杀)

    当所杀的线程内调用了pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);

    则调用pthread_cancel 函数没有作用

    pthread_setcancelstate意为对Cancel信号的反应(disable表忽略)

  • 调用一个全局变量

注意:在 main 函数中,return 语句和 pthread_exit 函数具有不同的效果,return 语句执行后进程也会结束,进程内部的所有线程自然强制结束,但 pthread_exit 函数只会结束主线程,并不会结束其他线程。

在普通线程的线程函数中,return 语句和 pthread_exit 函数基本差不多,只不过 pthread_exit 函数还会调用一下之前已经注册的线程清理函数。

volitile:C 语言关键字,作用就是告诉编译器取消对变量的优化,每次访问都从内存中存取变量。尤其是在多个线程都需要频繁访问一个全局变量时,强烈建议使用该关键字修饰变量。

  • 编译器优化常用的方法有:将内存变量缓存到寄存器;调整指令顺序充分利用CPU指令流水线,常见的是重新排序读写指令。对常规内存进行优化的时候,这些优化是透明的,而且效率很好。

  • volitile表明某个变量的值可能在外部被改变,因此对这些变量的存取不能缓存到寄存器,每次使用时需要重新存取。

register:C 语言关键字,建议编译器将变量设置为寄存器变量。

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

void* thr_fun1(void* arg);

int main()
{
    pthread_t tid;
    pthread_create(&tid, NULL, thr_fun1, NULL);

    sleep(3);

    pthread_cancel(tid);

    pthread_join(tid,NULL);

    return 0;
}

void *thr_fun1(void* arg)
{
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);//忽略他杀请求
    while(1)
    {
        printf("hello\n");
        sleep(1);
    }

    return NULL;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生产者-消费者问题是操作系统中的一个经典问题,也是多线程编程中常见的问题。在该问题中,有两类线程:生产者和消费者。生产者线程负责生产产品,将产品放入一个缓冲区中;消费者线程负责消费产品,从缓冲区中取出产品并进行消费。而缓冲区则是两个线程共享的资源。 下面是一个Linux下的多线程生产者-消费者示例: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define BUFFER_SIZE 10 int buffer[BUFFER_SIZE]; // 缓冲区 int in = 0; // 生产者将产品放入缓冲区的位置 int out = 0; // 消费者从缓冲区取出产品的位置 int count = 0; // 缓冲区中产品的数量 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 互斥锁 pthread_cond_t not_full = PTHREAD_COND_INITIALIZER; // 条件变量,缓冲区不满 pthread_cond_t not_empty = PTHREAD_COND_INITIALIZER; // 条件变量,缓冲区不空 // 生产者线程函数 void *producer(void *arg) { int i; for (i = 0; i < BUFFER_SIZE * 2; i++) { // 生产BUFFER_SIZE * 2个产品 pthread_mutex_lock(&mutex); // 加锁 while (count == BUFFER_SIZE) { // 缓冲区已满,等待条件变量not_full pthread_cond_wait(&not_full, &mutex); } buffer[in] = i; // 生产产品 printf("producer: produced item %d at %d\n", buffer[in], in); in = (in + 1) % BUFFER_SIZE; // 更新in指针 count++; // 更新产品数量 pthread_cond_signal(&not_empty); // 发送条件变量not_empty pthread_mutex_unlock(&mutex); // 解锁 } return NULL; } // 消费者线程函数 void *consumer(void *arg) { int i; for (i = 0; i < BUFFER_SIZE * 2; i++) { // 消费BUFFER_SIZE * 2个产品 pthread_mutex_lock(&mutex); // 加锁 while (count == 0) { // 缓冲区为空,等待条件变量not_empty pthread_cond_wait(&not_empty, &mutex); } printf("consumer: consumed item %d from %d\n", buffer[out], out); out = (out + 1) % BUFFER_SIZE; // 更新out指针 count--; // 更新产品数量 pthread_cond_signal(&not_full); // 发送条件变量not_full pthread_mutex_unlock(&mutex); // 解锁 } return NULL; } int main() { pthread_t producer_thread, consumer_thread; // 创建生产者线程和消费者线程 pthread_create(&producer_thread, NULL, producer, NULL); pthread_create(&consumer_thread, NULL, consumer, NULL); // 等待两个线程结束 pthread_join(producer_thread, NULL); pthread_join(consumer_thread, NULL); return 0; } ``` 在该示例中,使用了一个互斥锁和两个条件变量来保证线程之间的同步。生产者线程按照顺序生成产品并将产品放入缓冲区,当缓冲区已满时,生产者线程等待条件变量not_full;消费者线程按照顺序从缓冲区中取出产品并进行消费,当缓冲区为空时,消费者线程等待条件变量not_empty。通过条件变量的使用,可以避免线程的忙等待,提高了程序的效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值