DB2workload Development Based on Embedded SQL and C Multithreading

Source: Internet
Author: User
This article introduces the development process of DB2workload Based on Embedded SQL and C multi-thread, and provides a detailed list of program examples. This article provides an in-depth introduction to the multi-threaded C language and an analysis of the embedded SQL context environment that supports multi-threaded access. You can quickly master the development method of DB2workload and create a high

This article introduces the development process of DB2 workload based on embedded SQL and multi-thread C language, and provides a detailed program example list. This article provides an in-depth introduction to C-language multithreading and an analysis of the embedded SQL context environment that supports multi-threaded access. Readers can quickly master the development method of this DB2 workload and create a high

Because the code in the sample program is highly reusable, it can greatly improve the efficiency of the software automation testers who need to use this DB2 workload, and the software developers who use C-language multithreading to access DB2.

As we all know, in the actual production environment of DB2, we will encounter a variety of applications, such as based on different development languages, based on different architecture or based on different connection methods. For these different workloads, the software personnel engaged in testing the DB2 performance monitoring tool must develop some corresponding workloads to simulate these production environments in order to obtain reasonable test results, discover more potential problems with the DB2 performance monitoring tool.

According to the introduction of workload running on DB2, we found that the number of workloads based on embedded SQL and C-language multithreading is relatively small. In this case, this article introduces the development process of DB2 workload based on embedded SQL and C-language multithreading On the basis of analyzing the multi-thread technology in C language and building an embedded SQL context environment that supports multi-thread access, finally, a specific code instance is used to demonstrate the development process in detail.

Introduction to multiple threads in C Language

When a program starts to run, it is a process, including the memory and system resources used by the running programs and programs. A process is composed of multiple threads. A thread is an execution stream in a program. Each thread has its own proprietary register (Stack pointer, program counter, etc.), but the code zone is shared, that is, different threads can execute the same function and code segment. Multithreading means that a program contains multiple execution streams, that is, a program can run multiple different threads to execute different tasks at the same time, that is to say, a single program is allowed to create multiple parallel threads to complete their respective tasks.

C language initially did not design a multi-thread mechanism. With the development of software and hardware and the expansion of demand, C language developed a thread library to support multi-threaded operations and applications. This article mainly introduces the multi-thread C language in Linux. The multi-thread C language in Linux follows the POSIX thread interface, which is called pthread. In Linux, pthread is implemented by calling clone. Clone () is a Linux-specific system call. To compile a multi-threaded C program in Linux, you must use the header file "pthread. h". The library libpthread. a must be used for connection. Therefore, the-lpthread option must be added to the compilation; otherwise, the system prompts that related multi-threaded functions such as pthread_create () cannot be found. The following describes in detail some important APIs called by C-language multi-threaded programs in Linux.

Thread creation:

int pthread_create(pthread_t*restrict tidp,const pthread_attr_t *restrict attr, void *(*start_rtn)(void),void *restrict arg);
  • When the thread is successfully created, the function returns 0. If the value is not 0, the thread creation fails. The common error codes returned are EAGAIN and EINVAL. The former indicates that the system restricts the creation of new threads. For example, the number of threads is too large. The latter indicates that the second parameter indicates that the thread attribute value is invalid.
  • The first parameter is the pointer to the thread identifier. The second parameter is used to set the thread attribute. The third parameter is the starting address of the thread running function, and the last parameter is the parameter of the running function.

Thread suspension and exit:

int pthread_join(pthread_t thread, void **value_ptr);
  • This function suspends the current thread and continues execution only after the other thread returns. That is to say, when the program runs to this place, the program will stop and wait until the thread whose id is thread returns, then the program will be executed intermittently.
  • The first parameter is the identifier of the waiting thread, and the second parameter is a user-defined pointer, which can be used to store the return value of the waiting thread.
 void pthread_exit(void *rval_ptr);
  • There are two ways to end a thread. One is to end a function and the thread that calls it ends. The other is to call the function pthread_exit.
  • The only parameter is the return code of the function.

Mutex lock between threads:

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutex_attr_t* attr);
  • Mutex lock initialization: the first parameter is the mutex lock variable pointer, and the second parameter is the mutex lock attribute. The default attribute is used to pass in NULL.
int pthread_mutex_lock(pthread_mutex_t *mutex);
  • Mutex lock: the unique parameter is the mutex lock variable pointer. If the mutex lock has been locked, the current thread will be blocked until other threads unlock the mutex lock.
int pthread_mutex_unlock(pthread_mutex_t *mutex);
  • Mutex lock unlock: the unique parameter is the mutex lock variable pointer. If the current thread has the mutex lock specified by the mutex parameter, the call will unlock the mutex lock.

The execution process of multi-threaded C Programs in Linux is as follows:

Figure 1. multi-threaded Program Execution Process


Applications using C-language multithreading technology can make better use of system resources. The main advantage is to make full use of the free time slice of the CPU, you can respond to user requirements with as little time as possible, which not only greatly improves the overall running efficiency of the process, but also enhances the flexibility of the application.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.