answersLogoWhite

0


Best Answer

The thread class (std::thread) was introduced with the C++11 standard. Indeed, C++11 introduced several classes to make it easier to implement concurrency.

Consider the following example:

#include

#include

#include

void foo (char ch, size_t count, std::mutex* cout_mutex)

{

if (!cout_mutex) return;

for (size_t i=0; i!=count; ++i)

{

cout_mutex->lock();

std::cout << ch;

cout_mutex->unlock();

}

}

void do_it()

{

std::mutex my_mutex;

std::thread t1 (foo, 'A', 30, &my_mutex); // print 30 A's

std::thread t2 (foo, 'B', 25, &my_mutex); // print 25 B's

std::thread t3 (foo, 'C', 20, &my_mutex); // print 20 C's

std::thread t4 (foo, 'D', 15, &my_mutex); // print 15 D's

// synchronize threads:

t1.join();

t2.join();

t3.join();

t4.join();

}

int main()

{

for (size_t i=0; i!=10; ++i)

{

do_it();

std::cout << std::endl;

}

std::cout << "\nComplete!\n" << std::endl;

}

Example output:

AAAAAAAAABABABABABACBACBACBACBDACBDACBDACBDACBDACBDACBDACBDACBDACBDACBDACBDACBDACBDCBDCBCB

AAAAAAAAAAAABABABABCABCABCABDCABDCABDCABDCABDCABDCABDCABDCABDCABDCABDCABDCABDCBDCBDCBCBCBB

AAAAAAAAABABABABABABABABCABCABCABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDBCDBCDBCDCC

AAAAAAAAAABABABABABCABCABCABCABCADBCADBCADBCADBCADBCADBCADBCADBCADBCADBCADBCADBCDBCDBCDBCB

AAAAAAAAAAAABABABABABACBACBACBACDBACDBACDBACDBACDBACDBACDBACDBACDBACDBACDBCDBCDBCDBCDBCBCB

AAAAAAAAAAABABABABACBACBACBADCBADCBADCBADCBADCBADCBADCBADCBADCBADCBADCBADCBADCBDCBDCBCBCBB

AAAAAAAAABABABABABACBACBACBACDBACDBACDBACDBACDBACDBACDBACDBACDBACDBACDBACDBACDBACDBCDBCBCB

AAAAAAAAAABABABABABACBACBACBACBADCBADCBADCBADCBADCBADCBADCBADCBADCBADCBADCBADCBDCBDCBDCBCB

AAAAAAAAABABABABABABABCABCABCADBCADBCADBCADBCADBCADBCADBCADBCADBCADBCADBCADBCADBCDBCDBCBCC

AAAAAAAAAAABABABABABABACBACBACBACBDACBDACBDACBDACBDACBDACBDACBDACBDACBDACBDCBDCBDCBDCBDCBC

Complete!

The main function invokes the do_it() function 10 times. On each invocation, four threads of execution are instantiated. The first thread prints 30 A's, the second prints 25 B's, the third prints 20 C's and the last prints 15 D's. All threads begin executing as soon as they are instantiated and run concurrently with the do_it function which is part of the main thread (the thread in which main executes). Thus there are five threads in total.

The mutex is required to ensure that only one thread has access to the std::cout stream at a time. In this case the mutex is actually redundant since each thread prints just one character at a time. However, it is included to demonstrate a simple locking technique when two threads attempt to access the same resource.

All four threads must be joined to the main thread's do_it function. If we fail to do this, the do_it function would return before the four threads had completed their tasks. This would cause the mutex (which is local to the do_it function) to fall from scope which would then invalidate the mutex pointers in the four threads; they'd be pointing to a resource that no longer existed. By joining the threads to the main thread, the do_it function will wait for all threads to complete their tasks.

You will note from the example output that the execution order isn't exactly the same for each iteration of the main loop. This is normal with concurrent tasks and will ultimately depend on whatever background activity is going on at the time.

In the example output, the first thread starts printing A's while the second thread is being instantiated, then they alternate between A and B. However, on each iteration of the main loop, the B's start at different points. After a few iterations the C's begin printing and then the D's. Note that the order isn't the same each time and that there's no guarantee that D will stop before A, B or C. This shows that each thread runs independently of all others.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Discuss the class involved in implementation of thread in visual c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is there a way to program speech recognition in c plus plus with the express edition not the visual studio version?

The express edition's C++ implementation is no different to the visual studio implementation. It is the exact same language.


Which vitamin is involved in the formation of visual pigments?

vitamin a


Where can you download c plus plus?

C++ is a language specification rather than a specific implementation of the language. Visual C++ is a specific implementation as is GCC. Specific implementations can be downloaded from the appropriate implementer's websites. E.g., Visual C++ is available from Microsoft while GCC is available from the GNU Compiler Collection website.


What is basic language at least discuss?

BASIC, as such is dead now, but its derivatives like Visual Basic still exist.


Is the left cerebral hemisphere mainly involved with visual spatial skills?

False


What side of the brain is most involved in visual-spatial activities?

The right side of the brain is most involved in visual-spatial activities. It is responsible for tasks such as perceiving spatial relationships, recognizing patterns, and understanding shapes and forms.


Which element of speech delivery is involved when a speaker maintains good eye contact?

Visual aid


What are the steps to make a telescope?

An online visual guide can help you understand what is involved in building a telescope. www.howtelescopeswork.com


What is Tab control in Visual Basic NET?

In Visual Basic .NET the tab control is a built in way to add tabs to your application. A tab is a "window" of controls that can be interchanged by clicking on the automatically created buttons. The most common implementation of tabs is in web browsers, such as Google Chrome or Mozilla Firefox.


Where are the cortical areas involved with hearing located?

The temporal occipital lobe is the cortical area involved in auditioning. It is the visual processing center of the mammalian brain.


What is the best c plus plus IDE?

On Windows, Visual Studio is by far the best IDE, but the C++ implementation is not standards-compliant. For a more generic/portable solution, consider using gcc instead.


What is the difference between cerebral parietal lobe and occipital lobe?

The parietal is mainly involved in spatial reasoning and sense, when you have to mentally rotate an object you are using this area. The occipital is involved in visual processing