Multi-threading is a special form of multitasking, which allows the computer to run two or more programs at the same time. In general, there are two types of multitasking: process-based and thread-based.
Process-based multitasking is the concurrent execution of programs.
Thread-based multitasking is the concurrent execution of fragments of the same program.
Multi-threaded programs contain two or more parts that can run simultaneously. Each part of such a program is called a thread, and each thread defines a separate execution path.
C++ does not contain any built-in support for multi-threaded applications. Instead, it relies entirely on the operating system to provide this functionality.
This tutorial assumes that you are using a Linux operating system and we are going to use POSIX to write a multi-threaded C++ program. POSIX Threads or Pthreads provide an API available on a variety of Unix-like POSIX systems, such as FreeBSD, NetBSD, GNU/Linux, Mac OS X, and Solaris.
Create thread
The following program, we can use it to create a POSIX thread:
#include <iostream>#include <cstdlib>#include <pthread.h>#include <unistd.h>using namespace std;#define NUM_THREADS 5void *wait(void *t){
int i;
long tid;
tid = (long)t;
sleep(1);
cout << "Sleeping in thread " << endl;
cout << "Thread with id : " << tid << " ...exiting " << endl;
pthread_exit(NULL);}int main (){
int rc;
int i;
pthread_t threads[NUM_THREADS];
pthread_attr_t attr;
void *status;
// 初始化并设置线程为可连接的(joinable)
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for( i=0; i < NUM_THREADS; i++ ){
cout << "main() : creating thread, " << i << endl;
rc = pthread_create(&threads[i], NULL, wait, (void *)i );
if (rc){
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
// 删除属性,并等待其他线程
pthread_attr_destroy(&attr);
for( i=0; i < NUM_THREADS; i++ ){
rc = pthread_join(threads[i], &status);
if (rc){
cout << "Error:unable to join," << rc << endl;
exit(-1);
}
cout << "Main: completed thread id :" << i ;
cout << " exiting with status :" << status << endl;
}
cout << "Main: program exiting." << endl;
pthread_exit(NULL);}
Copy after login
当上面的代码被编译和执行时,它会产生下列结果:
main() : creating thread, 0main() :
creating thread, 1main() : creating thread, 2main() : creating thread, 3main() : creating thread, 4Sleeping in thread
Thread with id : 4 ...exiting
Sleeping in thread
Thread with id : 3 ...exiting
Sleeping in thread
Thread with id : 2 ...exiting
Sleeping in thread
Thread with id : 1 ...exiting
Sleeping in thread
Thread with id : 0 ...exiting
Main: completed thread id :0
exiting with status :0Main: completed thread id :1
exiting with status :0Main: completed thread id :2
exiting with status :0Main: completed thread id :3
exiting with status :0Main: completed thread id :4
exiting with status :0Main: program exiting.
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn