Posted by Danni on December 5, 2018

Multithreading is a specialized form of multitasking which allows your computer to run two or more programs concurrently. In general, there are two types of multitasking: process-based and thread-based.

Process-based multitasking handles the concurrent execution of programs. Thread-based multitasking deals with the concurrent execution of pieces of the same program.

Perhaps one of the biggest change to the language since C++ 11 is the addition of multithreading support. Before C++11, it was possible to target multicore computers using OS facilities (pthreads on Unix like systems) or libraries like OpenMP and MPI.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <Windows.h>

using namespace std;

{
for (int i = 0; i < 5; i++)
{
cout << "Thread 01 is working ！" << endl;
Sleep(100);
}
}
{
for (int i = 0; i < 5; i++)
{
cout << "Thread 02 is working ！" << endl;
Sleep(200);
}
}

int main()
{

for (int i = 0; i < 5; i++)
{
cout << "Main thread is working ！" << endl;
Sleep(200);
}
system("pause");
}


However, the join function will block the main thread so that the main thread can only be processed after all child threads finish processing. To optimize such, we can use detach to separate child and main threads to facilitate execution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <Windows.h>

using namespace std;

{
for (int i = 0; i < 5; i++)
{
cout << "Thread 01 is working ！" << endl;
Sleep(100);
}
}
{
for (int i = 0; i < 5; i++)
{
cout << "Thread 02 is working ！" << endl;
Sleep(200);
}
}

int main()
{

for (int i = 0; i < 5; i++)
{
cout << "Main thread is working ！" << endl;
Sleep(200);
}
system("pause");
}


### Mutex

When multiple threads are making changes to the same variable, the data can be corrupted if not taking protective measures. For example, in the following code, thread01 and thread02 are both making changes to the totalNum, and its output is corrupted such that some desired values are not printed while some are duplicatedly printed.

The reason could be, while thread01 is making changes, thread02 is also making changes so the output of thread01 is the actual processed result of thread02. To deal with such problems, we can use mutex to update shared data. To use mutex, just **#include ** in the header.

The mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads. mutex offers exclusive, non-recursive ownership semantics: A calling thread owns a mutex from the time that it successfully calls either lock or try_lock until it calls unlock .

……9494, 9292, 9090, 8888, ……

Original code without mutex:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int totalNum = 100;

{
while (totalNum > 0)
{
cout << totalNum << endl;
totalNum--;
Sleep(100);
}
}
{
while (totalNum > 0)
{
cout << totalNum << endl;
totalNum--;
Sleep(100);
}
}

int main()
{
system("pause");
}


Updated code using Mutex:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <Windows.h>
#include <mutex>

using namespace std;

mutex mu;

int totalNum = 100;

{
while (totalNum > 0)
{
mu.lock();
cout << totalNum << endl;
totalNum--;
Sleep(100);
mu.unlock();
}
}
{
while (totalNum > 0)
{
mu.lock();
cout << totalNum << endl;
totalNum--;
Sleep(100);
mu.unlock();
}
}

int main()
{
system("pause");
}


Now the output is normal:

1
2
3
4
5
100

99

98