c/c++ __thread

1 篇文章 0 订阅

1. __thread 关键字表示每一个线程有一份独立的实体,每一个线程都不会干扰。

2. __thread 只能修饰POD变量,简单的来说可以是如下几种变量

(1) 基本类型 (int , float 等等)

(2)指针类型

(3) 不带自定义构造函数和析构函数的类,如果希望修饰带自定义构造和析构函数的类,需要用到指针。

3. 实验代码

(1)__thread 可以修饰stu_t, 因为stu_t没有自定义的构造和析构函数

(2)__thread 可以修饰teacher_t*,因为__thread可以修饰指针

(3)实验结果表示在不同的线程中,变量不会相互影响。

#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <string>

class stu_t {
public:
   int num;
   int age;
};

class teacher_t {
public:
   int num;
   int age;
   teacher_t (int num, int age) {
      this->num = num;
      this->age = age;
   }
};


static __thread int count;
static __thread stu_t stu;
static __thread teacher_t* teacher;

void print(std::string description, int num, stu_t stu, teacher_t* teacher) {
    std::cout << description << ", before======>" << "pid:" << getpid() << ", pthread id:" << pthread_self() << ", count:" << count << std::endl;
    std::cout << description << ", before======>" << "pid:" << getpid() << ", pthread id:" << pthread_self() << ", snum:" << stu.num << ", sage:"<< stu.age << std::endl;
    std::cout << description << ", before======>" << "pid:" << getpid() << ", pthread id:" << pthread_self() << ", snum:" << teacher->num << ", tage:"<< teacher->age << std::endl;
}


void *function1(void *argc)
{
    count = 2;
    stu.num = 2;
    stu.age = 2;
    teacher = new teacher_t(2,2);
    sleep(1);
    print("thread1", count, stu, teacher);
    return 0;
}

void *function2(void *argc)
{
    stu.num = 3;
    stu.age = 3;
    count = 3;
    teacher = new teacher_t(3,3);
    sleep(2);
    print("thread2", count, stu, teacher);
    return 0;
}

int main()
{
    pthread_t  thread_id[2];
    int ret;

    stu.num = 1;
    stu.age = 1;
    count = 1;
    teacher = new teacher_t(1,1);

    print("main", count, stu, teacher);
    pthread_create(thread_id, NULL, function1, NULL);
    pthread_create(thread_id + 1, NULL, function2, NULL);
    pthread_join(thread_id[0], NULL);
    pthread_join(thread_id[1], NULL);
    print("main", count, stu, teacher);

    return 0;
}

​

4. 实验结果

main, before======>pid:697, pthread id:0x7fff91ca0380, count:1
main, before======>pid:697, pthread id:0x7fff91ca0380, snum:1, sage:1
main, before======>pid:697, pthread id:0x7fff91ca0380, snum:1, tage:1
thread1, before======>pid:697, pthread id:0x700002525000, count:2
thread1, before======>pid:697, pthread id:0x700002525000, snum:2, sage:2
thread1, before======>pid:697, pthread id:0x700002525000, snum:2, tage:2
thread2, before======>pid:697, pthread id:0x7000025a8000, count:3
thread2, before======>pid:697, pthread id:0x7000025a8000, snum:3, sage:3
thread2, before======>pid:697, pthread id:0x7000025a8000, snum:3, tage:3
main, before======>pid:697, pthread id:0x7fff91ca0380, count:1
main, before======>pid:697, pthread id:0x7fff91ca0380, snum:1, sage:1
main, before======>pid:697, pthread id:0x7fff91ca0380, snum:1, tage:1

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#0 Homer::RtpSessionState::deprovisionAtDestruction (this=0x7f1fe00e7650) at ../components/mos/state/session_state/src/rtp_session_state.cpp:886 886 ../components/mos/state/session_state/src/rtp_session_state.cpp: No such file or directory. [Current thread is 1 (Thread 0x7f1ffffff700 (LWP 372))] (gdb) bt #0 Homer::RtpSessionState::deprovisionAtDestruction (this=0x7f1fe00e7650) at ../components/mos/state/session_state/src/rtp_session_state.cpp:886 #1 0x00005653759a8e33 in Homer::DeviceState::~DeviceState (this=0x7f1fe00ecf50) at ../components/mos/state/device_state/src/device_state.cpp:87 #2 0x0000565375c4229e in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x7f1fe00ecf40) at /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/shared_ptr_base.h:155 #3 std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0x7f1fe00bbb08) at /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/shared_ptr_base.h:730 #4 std::__shared_ptr<Homer::DeviceStateBase, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0x7f1fe00bbb00) at /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/shared_ptr_base.h:1169 #5 Homer::Device::~Device (this=0x7f1fe00bb070) at ../components/mos/device/src/device.cpp:94 #6 0x0000565375c3c08a in Homer::WebsocketDevice::~WebsocketDevice (this=0x7f1fe00bb070) at ../components/mos/device/src/websocket_device.h:31 #7 Homer::WebsocketClientDevice::~WebsocketClientDevice (this=0x7f1fe00bb070) at ../components/mos/device/src/websocket_client_device.cpp:20
06-13
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值