C++两种线程模式:委托和继承(附示例)

继承方式实现简单,请参见:https://github.com/eyjian/mooon/blob/master/common_library/include/mooon/sys/thread.h 在C++11标准之前的实现较为复杂(可参见:https://github.com/eyjian/mooon/blob/master/common_library/include/mooon/sys/thread_engine.h): 委托版本使用非常简单:

 					
 static void hello(int m, int n)
  
 {
 
 				    printf("[%u] hello: %d/%d\n", pthread_self(), m, n);
 
 }
 
 
 
 class X
 
 {
 
 public:
 
 				    void world(int m, int n, int l)
 
 {
 
 				        printf("[%u] X::world: %d/%d/%d\n", pthread_self(), m, n, l);
 
 }
 
 };
 
 
 
 int main()
 
 {
 
      X* x2 = new X;
 
  
 // 带2个参数
 
 				    CThreadEngine engine9(bind(&hello, 2015, 5));
 
 
 
 // 带3个参数
 
 				    CThreadEngine engine16(bind(&X::world, x2, 2016, 5, 3));
 
 				    return 0;
 
 } 

委托版本实现:

 #ifndef MOOON_SYS_THREAD_ENGINE_H
  
 			#define MOOON_SYS_THREAD_ENGINE_H
 
 
 
 // 如果测试,请不需注释掉TEST_THREAD_ENGINE的定义
 
 //#define TEST_THREAD_ENGINE 1
 
 
 
 			#if !defined(TEST_THREAD_ENGINE)
 
 			#include "mooon/sys/config.h" // 如果需要脱离mooon运行,请注释掉这行
 
 			#endif // TEST_THREAD_ENGINE
 
 
 
 			#include <pthread.h>
 
 			#include <stdio.h>
 
 			#include <unistd.h>
 
 
 
 			#if defined(TEST_THREAD_ENGINE)
 
 			#define SYS_NAMESPACE_BEGIN
 
 			#define SYS_NAMESPACE_END
 
 			#endif // TEST_THREAD_ENGINE
 
 			SYS_NAMESPACE_BEGIN
 
 
 
 // 基类
 
 class Function
 
 {
 
 public:
 
 			    virtual ~Function() {}
 
 			    virtual void operator ()() = 0;
 
 };
 
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
 // 不带参数
 
 
 
 class FunctionWithoutParameter: public Function
 
 {
 
 public:
 
 // 不使用返回值,所以不需要返回值,否则将void替换成需要的类型
 
 			    typedef void (*FunctionPtr)();
 
 
 
 			    FunctionWithoutParameter(FunctionPtr function_ptr)
 
 : _function_ptr(function_ptr)
 
 {
 
 }
 
 
 
 			    virtual void operator ()()
 
 {
 
 (*_function_ptr)();
 
 }
 
 
 
 private:
 
 			    FunctionPtr _function_ptr;
 
 };
 
 
 
 			template <class ObjectType>
 
 class MemberFunctionWithoutParameter: public Function
 
 {
 
 public:
 
 			    typedef void (ObjectType::*MemberFunctionPtr)();
 
 			    MemberFunctionWithoutParameter(MemberFunctionPtr member_function_ptr, ObjectType* object)
 
 : _member_function_ptr(member_function_ptr), _object(object)
 
 {
 
 }
 
 
 
 			    virtual void operator ()()
 
 {
 
 (_object->*_member_function_ptr)();
 
 }
 
 
 
 private:
 
 			    MemberFunctionPtr _member_function_ptr;
 
 			    ObjectType* _object;
 
 };
 
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
 // 带1个参数
 
 
 
 			template <typename ParameterType>
 
 class FunctionWith1Parameter: public Function
 
 {
 
 public:
 
 			    typedef void (*FunctionPtr)(ParameterType);
 
 
 
 			    FunctionWith1Parameter(FunctionPtr function_ptr, ParameterType parameter)
 
 : _function_ptr(function_ptr), _parameter(parameter)
 
 {
 
 }
 
 
 
 			    virtual void operator ()()
 
 {
 
 (*_function_ptr)(_parameter);
 
 }
 
 
 
 private:
 
 			    FunctionPtr _function_ptr;
 
 			    ParameterType _parameter;
 
 };
 
 
 
 			template <class ObjectType, typename ParameterType>
 
 class MemberFunctionWith1Parameter: public Function
 
 {
 
 public:
 
 			    typedef void (ObjectType::*MemberFunctionPtr)(ParameterType);
 
 			    MemberFunctionWith1Parameter(MemberFunctionPtr member_function_ptr, ObjectType* object, ParameterType parameter)
 
 : _member_function_ptr(member_function_ptr), _object(object), _parameter(parameter)
 
 {
 
 }
 
 
 
 			    virtual void operator ()()
 
 {
 
 (_object->*_member_function_ptr)(_parameter);
 
 }
 
 
 
 private:
 
 			    MemberFunctionPtr _member_function_ptr;
 
 			    ObjectType* _object;
 
 			    ParameterType _parameter;
 
 };
 
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
 // 带2个参数
 
 
 
 			template <typename Parameter1Type, typename Parameter2Type>
 
 class FunctionWith2Parameter: public Function
 
 {
 
 public:
 
 			    typedef void (*FunctionPtr)(Parameter1Type, Parameter2Type);
 
 
 
 			    FunctionWith2Parameter(FunctionPtr function_ptr, Parameter1Type parameter1, Parameter2Type parameter2)
 
 : _function_ptr(function_ptr), _parameter1(parameter1), _parameter2(parameter2)
 
 {
 
 }
 
 
 
 			    virtual void operator ()()
 
 {
 
 (*_function_ptr)(_parameter1, _parameter2);
 
 }
 
 
 
 private:
 
 			    FunctionPtr _function_ptr;
 
 			    Parameter1Type _parameter1;
 
 			    Parameter2Type _parameter2;
 
 };
 
 
 
 			template <class ObjectType, typename Parameter1Type, typename Parameter2Type>
 
 class MemberFunctionWith2Parameter: public Function
 
 {
 
 public:
 
 			    typedef void (ObjectType::*MemberFunctionPtr)(Parameter1Type, Parameter2Type);
 
 			    MemberFunctionWith2Parameter(MemberFunctionPtr member_function_ptr, ObjectType* object, Parameter1Type parameter1, Parameter2Type parameter2)
 
 : _member_function_ptr(member_function_ptr), _object(object), _parameter1(parameter1), _parameter2(parameter2)
 
 {
 
 }
 
 
 
 			    virtual void operator ()()
 
 {
 
 (_object->*_member_function_ptr)(_parameter1, _parameter2);
 
 }
 
 
 
 private:
 
 			    MemberFunctionPtr _member_function_ptr;
 
 			    ObjectType* _object;
 
 			    Parameter1Type _parameter1;
 
 			    Parameter2Type _parameter2;
 
 };
 
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
 // 带3个参数
 
 
 
 			template <typename Parameter1Type, typename Parameter2Type, typename Parameter3Type>
 
 class FunctionWith3Parameter: public Function
 
 {
 
 public:
 
 			    typedef void (*FunctionPtr)(Parameter1Type, Parameter2Type, Parameter3Type);
 
 
 
 			    FunctionWith3Parameter(FunctionPtr function_ptr, Parameter1Type parameter1, Parameter2Type parameter2, Parameter3Type parameter3)
 
 : _function_ptr(function_ptr), _parameter1(parameter1), _parameter2(parameter2), _parameter3(parameter3)
 
 {
 
 }
 
 
 
 			    virtual void operator ()()
 
 {
 
 (*_function_ptr)(_parameter1, _parameter2, _parameter3);
 
 }
 
 
 
 private:
 
 			    FunctionPtr _function_ptr;
 
 			    Parameter1Type _parameter1;
 
 			    Parameter2Type _parameter2;
 
 			    Parameter3Type _parameter3;
 
 };
 
 
 
 			template <class ObjectType, typename Parameter1Type, typename Parameter2Type, typename Parameter3Type>
 
 class MemberFunctionWith3Parameter: public Function
 
 {
 
 public:
 
 			    typedef void (ObjectType::*MemberFunctionPtr)(Parameter1Type, Parameter2Type, Parameter3Type);
 
 			    MemberFunctionWith3Parameter(MemberFunctionPtr member_function_ptr, ObjectType* object, Parameter1Type parameter1, Parameter2Type parameter2, Parameter3Type parameter3)
 
 : _member_function_ptr(member_function_ptr), _object(object), _parameter1(parameter1), _parameter2(parameter2), _parameter3(parameter3)
 
 {
 
 }
 
 
 
 			    virtual void operator ()()
 
 {
 
 (_object->*_member_function_ptr)(_parameter1, _parameter2, _parameter3);
 
 }
 
 
 
 private:
 
 			    MemberFunctionPtr _member_function_ptr;
 
 			    ObjectType* _object;
 
 			    Parameter1Type _parameter1;
 
 			    Parameter2Type _parameter2;
 
 			    Parameter3Type _parameter3;
 
 };
 
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
 class Functor
 
 {
 
 public:
 
 			    Functor()
 
 : _function(NULL)
 
 {
 
 }
 
 
 
 			    Functor(const Functor& other)
 
 {
 
 			        _function = other._function;
 
 
 
 			        Functor& mutable_other = const_cast<Functor&>(other);
 
 			        mutable_other._function = NULL;
 
 }
 
 
 
 ~Functor()
 
 {
 
 			        delete _function;
 
 }
 
 
 
 			    Functor& operator =(const Functor& other)
 
 {
 
 			        _function = other._function;
 
 
 
 			        Functor& mutable_other = const_cast<Functor&>(other);
 
 			        mutable_other._function = NULL;
 
 
 
 			        return *this;
 
 }
 
 
 
 			    void operator ()()
 
 {
 
 (*_function)();
 
 }
 
 
 
 // 不带参数
 
 			    Functor(FunctionWithoutParameter::FunctionPtr function_ptr)
 
 {
 
 			        _function = new FunctionWithoutParameter(function_ptr);
 
 }
 
 
 
 			    template <class ObjectType>
 
 			    Functor(typename MemberFunctionWithoutParameter<ObjectType>::MemberFunctionPtr member_function_ptr, ObjectType* object)
 
 {
 
 			        _function = new MemberFunctionWithoutParameter<ObjectType>(member_function_ptr, object);
 
 }
 
 
 
 // 带1个参数
 
 			    template <typename ParameterType>
 
 			    Functor(typename FunctionWith1Parameter<ParameterType>::FunctionPtr function_ptr, ParameterType parameter)
 
 {
 
 			        _function = new FunctionWith1Parameter<ParameterType>(function_ptr, parameter);
 
 }
 
 
 
 			    template <class ObjectType, typename ParameterType>
 
 			    Functor(typename MemberFunctionWith1Parameter<ObjectType, ParameterType>::MemberFunctionPtr member_function_ptr, ObjectType* object, ParameterType parameter)
 
 {
 
 			        _function = new MemberFunctionWith1Parameter<ObjectType, ParameterType>(member_function_ptr, object, parameter);
 
 }
 
 
 
 // 带2个参数
 
 			    template <typename Parameter1Type, typename Parameter2Type>
 
 			    Functor(typename FunctionWith2Parameter<Parameter1Type, Parameter2Type>::FunctionPtr function_ptr, Parameter1Type parameter1, Parameter2Type parameter2)
 
 {
 
 			        _function = new FunctionWith2Parameter<Parameter1Type, Parameter2Type>(function_ptr, parameter1, parameter2);
 
 }
 
 
 
 			    template <class ObjectType, typename Parameter1Type, typename Parameter2Type>
 
 			    Functor(typename MemberFunctionWith2Parameter<ObjectType, Parameter1Type, Parameter2Type>::MemberFunctionPtr member_function_ptr, ObjectType* object, Parameter1Type parameter1, Parameter2Type parameter2)
 
 {
 
 			        _function = new MemberFunctionWith2Parameter<ObjectType, Parameter1Type, Parameter2Type>(member_function_ptr, object, parameter1, parameter2);
 
 }
 
 
 
 // 带3个参数
 
 			    template <typename Parameter1Type, typename Parameter2Type, typename Parameter3Type>
 
 			    Functor(typename FunctionWith3Parameter<Parameter1Type, Parameter2Type, Parameter3Type>::FunctionPtr function_ptr, Parameter1Type parameter1, Parameter2Type parameter2, Parameter3Type parameter3)
 
 {
 
 			        _function = new FunctionWith3Parameter<Parameter1Type, Parameter2Type, Parameter3Type>(function_ptr, parameter1, parameter2, parameter3);
 
 }
 
 
 
 			    template <class ObjectType, typename Parameter1Type, typename Parameter2Type, typename Parameter3Type>
 
 			    Functor(typename MemberFunctionWith3Parameter<ObjectType, Parameter1Type, Parameter2Type, Parameter3Type>::MemberFunctionPtr member_function_ptr, ObjectType* object, Parameter1Type parameter1, Parameter2Type parameter2, Parameter3Type parameter3)
 
 {
 
 			        _function = new MemberFunctionWith3Parameter<ObjectType, Parameter1Type, Parameter2Type, Parameter3Type>(member_function_ptr, object, parameter1, parameter2, parameter3);
 
 }
 
 
 
 public:
 
 Function* _function;
 
 };
 
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
 // 不带参数
 
 
 
 			Functor bind(FunctionWithoutParameter::FunctionPtr function_ptr)
 
 {
 
 			    return Functor(function_ptr); 
 
 }
 
 
 
 			template <class ObjectType>
 
 			Functor bind(typename MemberFunctionWithoutParameter<ObjectType>::MemberFunctionPtr member_function_ptr, ObjectType* object)
 
 {
 
 			    return Functor(member_function_ptr, object);
 
 }
 
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
 // 带1个参数
 
 
 
 			template <typename ParameterType>
 
 			Functor bind(typename FunctionWith1Parameter<ParameterType>::FunctionPtr function_ptr, ParameterType parameter)
 
 {
 
 			    return Functor(function_ptr, parameter); 
 
 }
 
 
 
 			template <class ObjectType, typename ParameterType>
 
 			Functor bind(typename MemberFunctionWith1Parameter<ObjectType, ParameterType>::MemberFunctionPtr member_function_ptr, ObjectType* object, ParameterType parameter)
 
 {
 
 			    return Functor(member_function_ptr, object, parameter);
 
 }
 
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
 // 带2个参数
 
 
 
 			template <typename Parameter1Type, typename Parameter2Type>
 
 			Functor bind(typename FunctionWith2Parameter<Parameter1Type, Parameter2Type>::FunctionPtr function_ptr, Parameter1Type parameter1, Parameter2Type parameter2)
 
 {
 
 			    return Functor(function_ptr, parameter1, parameter2);
 
 }
 
 
 
 			template <class ObjectType, typename Parameter1Type, typename Parameter2Type>
 
 			Functor bind(typename MemberFunctionWith2Parameter<ObjectType, Parameter1Type, Parameter2Type>::MemberFunctionPtr member_function_ptr, ObjectType* object, Parameter1Type parameter1, Parameter2Type parameter2)
 
 {
 
 			    return Functor(member_function_ptr, object, parameter1, parameter2);
 
 }
 
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
 // 带3个参数
 
 
 
 			template <typename Parameter1Type, typename Parameter2Type, typename Parameter3Type>
 
 			Functor bind(typename FunctionWith3Parameter<Parameter1Type, Parameter2Type, Parameter3Type>::FunctionPtr function_ptr, Parameter1Type parameter1, Parameter2Type parameter2, Parameter3Type parameter3)
 
 {
 
 			    return Functor(function_ptr, parameter1, parameter2, parameter3);
 
 }
 
 
 
 			template <class ObjectType, typename Parameter1Type, typename Parameter2Type, typename Parameter3Type>
 
 			Functor bind(typename MemberFunctionWith3Parameter<ObjectType, Parameter1Type, Parameter2Type, Parameter3Type>::MemberFunctionPtr member_function_ptr, ObjectType* object, Parameter1Type parameter1, Parameter2Type parameter2, Parameter3Type parameter3)
 
 {
 
 			    return Functor(member_function_ptr, object, parameter1, parameter2, parameter3);
 
 }
 
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
 			static void* thread_proc(void* parameter)
 
 {
 
 			    Functor* functor = (Functor*)parameter;
 
 (*functor)();
 
 
 
 			    delete functor; // 记得这里需要delete
 
 			    return NULL;
 
 }
 
 
 
 class CThreadEngine
 
 {
 
 public:
 
 			    CThreadEngine(const Functor& functor)
 
 {
 
 // bind()返回的是一个临时对象,
 
 // 故需要new一个,以便在thread_proc()过程中有效
 
 			        Functor* new_functor = new Functor(functor);
 
 
 
 int errcode = pthread_create(&_thread, NULL, thread_proc, new_functor);
 
 if (errcode != 0)
 
 {
 
 			            delete new_functor;
 
 }
 
 }
 
 
 
 ~CThreadEngine()
 
 {
 
 join();
 
 }
 
 
 
 			    void join()
 
 {
 
 if (_thread > 0)
 
 {
 
 			            pthread_join(_thread, NULL);
 
 			            _thread = 0;
 
 }
 
 }
 
 
 
 private:
 
 			    CThreadEngine();
 
 			    CThreadEngine(const CThreadEngine&);
 
 			    CThreadEngine& operator =(const CThreadEngine&);
 
 
 
 private:
 
 			    pthread_t _thread;
 
 };
 
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
 // 以下为测试代码
 
 			#if defined(TEST_THREAD_ENGINE)
 
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
 // 非类成员函数
 
 
 
 			static void hello()
 
 {
 
 			    printf("[%u] hello\n", pthread_self());
 
 }
 
 
 
 			static void world()
 
 {
 
 			    printf("[%u] world\n", pthread_self());
 
 }
 
 
 
 			static void hello(int m)
 
 {
 
 			    printf("[%u] hello: %d\n", pthread_self(), m);
 
 }
 
 
 
 			static void world(int m)
 
 {
 
 			    printf("[%u] world: %d\n", pthread_self(), m);
 
 }
 
 
 
 			static void hello(int m, int n)
 
 {
 
 			    printf("[%u] hello: %d/%d\n", pthread_self(), m, n);
 
 }
 
 
 
 			static void world(int m, int n)
 
 {
 
 			    printf("[%u] world: %d/%d\n", pthread_self(), m, n);
 
 }
 
 
 
 			static void hello(int m, int n, int l)
 
 {
 
 			    printf("[%u] hello: %d/%d/%d\n", pthread_self(), m, n, l);
 
 }
 
 
 
 			static void world(int m, int n, int l)
 
 {
 
 			    printf("[%u] world: %d/%d/%d\n", pthread_self(), m, n, l);
 
 }
 
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
 // 类成员函数
 
 
 
 class X
 
 {
 
 public:
 
 //
 
 // 不带参数
 
 //
 
 
 
 			    void hello()
 
 {
 
 			        printf("[%u] X::hello\n", pthread_self());
 
 }
 
 
 
 			    void world()
 
 {
 
 			        printf("[%u] X::world\n", pthread_self());
 
 }
 
 
 
 //
 
 // 带1个参数
 
 //
 
 
 
 			    void hello(int m)
 
 {
 
 			        printf("[%u] X::hello: %d\n", pthread_self(), m);
 
 }
 
 
 
 			    void world(int m)
 
 {
 
 			        printf("[%u] X::world: %d\n", pthread_self(), m);
 
 }
 
 
 
 //
 
 // 带2个参数
 
 //
 
 
 
 			    void hello(int m, int n)
 
 {
 
 			        printf("[%u] X::hello: %d/%d\n", pthread_self(), m, n);
 
 }
 
 
 
 			    void world(int m, int n)
 
 {
 
 			        printf("[%u] X::world: %d/%d\n", pthread_self(), m, n);
 
 }
 
 
 
 //
 
 // 带3个参数
 
 //
 
 
 
 			    void hello(int m, int n, int l)
 
 {
 
 			        printf("[%u] X::hello: %d/%d/%d\n", pthread_self(), m, n, l);
 
 }
 
 
 
 			    void world(int m, int n, int l)
 
 {
 
 			        printf("[%u] X::world: %d/%d/%d\n", pthread_self(), m, n, l);
 
 }
 
 };
 
 
 
 ////////////////////////////////////////////////////////////////////////////////
 
 int main()
 
 {
 
 // 不带参数
 
 			    CThreadEngine engine1(bind(&hello));
 
 			    CThreadEngine engine2(bind(&world));
 
 
 
 			    X* x1 = new X;
 
 			    X* x2 = new X;
 
 			    CThreadEngine engine3(bind(&X::hello, x1));
 
 			    CThreadEngine engine4(bind(&X::world, x2));
 
 
 
 // 带1个参数
 
 			    CThreadEngine engine5(bind(&hello, 2015));
 
 			    CThreadEngine engine6(bind(&world, 2016));
 
 
 
 			    CThreadEngine engine7(bind(&X::hello, x1, 2015));
 
 			    CThreadEngine engine8(bind(&X::world, x2, 2016)); 
 
 
 
 // 带2个参数
 
 			    CThreadEngine engine9(bind(&hello, 2015, 5));
 
 			    CThreadEngine engine10(bind(&world, 2016, 5));
 
 
 
 			    CThreadEngine engine11(bind(&X::hello, x1, 2015, 5));
 
 			    CThreadEngine engine12(bind(&X::world, x2, 2016, 5));
 
 
 
 // 带3个参数
 
 			    CThreadEngine engine13(bind(&hello, 2015, 5, 3));
 
 			    CThreadEngine engine14(bind(&world, 2016, 5, 3));
 
 
 
 			    CThreadEngine engine15(bind(&X::hello, x1, 2015, 5, 3));
 
 			    CThreadEngine engine16(bind(&X::world, x2, 2016, 5, 3));
 
 
 
 // 按任意键继续继续
 
 			    printf("Press ENTER to exit ...\n");
 
 			    getchar();
 
 
 
 			    engine1.join();
 
 			    engine2.join();
 
 			    engine3.join();
 
 			    engine4.join();
 
 			    engine5.join();
 
 			    engine6.join();
 
 			    engine7.join();
 
 			    engine8.join();
 
 
 
 			    engine9.join();
 
 			    engine10.join();
 
 			    engine11.join();
 
 			    engine12.join();
 
 			    engine13.join();
 
 			    engine14.join();
 
 			    engine15.join();
 
 			    engine16.join();
 
 
 
 			    delete x1;
 
 			    delete x2;
 
 
 
 // 按任意键继续退出
 
 			    printf("Press ENTER to exit ...\n");
 
 			    getchar();
 
 
 
 			    return 0;
 
 }
 
 
 
 			SYS_NAMESPACE_END
 
 			#endif // TEST_THREAD_ENGINE
 
 			#endif // MOOON_SYS_THREAD_ENGINE_H 		

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Spark学习技巧

Hive学习之Lateral View

Lateral view与UDTF函数如explode()一起使用,UDTF对每个输入行产生0或者多个输出行。Lateral view首先在基表的每个输入行应用...

41140
来自专栏Jackson0714

【T-SQL基础】01.单表查询-几道sql查询题

36390
来自专栏源哥的专栏

字符串分拆函数

在Oracle中,如果一个包含分隔符的字符串(比如说“55*62*77”,这个字符串是用*号做分隔符,可以拆分成三个数“55”,“62”和“77”),要将它们拆...

7320
来自专栏james大数据架构

你真的会玩SQL吗?无处不在的子查询

你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接、外连接 你真的会玩SQL吗?三范式、数据完整性 你真...

20070
来自专栏杨建荣的学习笔记

通过java程序抽取日志中的sql语句(r4笔记第4天)

今天在翻看以前的笔记时,发现自己在很早之前写过一个java程序,能够解析日志中的sql语句。 当时使用的环境是weblogic,日志目录下总是有几十上百个日志文...

31760
来自专栏文渊之博

参数化(二):执行查询的方式

前面一篇我介绍了执行计划缓存以及执行之前批处理经过的流程。这篇将用几个最普通的例子介绍查询的几种执行方式。 请看下面这个我使用的这个查询: SELECT Id...

18780
来自专栏章鱼的慢慢技术路

SQL数据查询之——嵌套查询

19130
来自专栏AhDung

【SQL】sql版Split函数。用于拆分字符串为单列表格

功能与.net版string.Split函数类似,只不过.net返回的是数组,这个返回的是一个单列表格,每个拆分出来的子串占一行。可选是否移除空格子串和重复项。...

41330
来自专栏程序员的SOD蜜

使用CTE解决复杂查询的问题

最近,同事需要从数个表中查询用户的业务和报告数据,写了一个SQL语句,查询比较慢: Select S.Name, S.AccountantCode, ( Se...

25460
来自专栏大数据学习笔记

Hadoop源码分析:Writable接口和WritableComparable接口

1、 Writable接口 由于Hadoop的MapReduce和HDFS都有通信的需求,需要对通信的对象进行序列化。Hadoop并没有采用Java的序列化,而...

22260

扫码关注云+社区

领取腾讯云代金券