Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Welcome Guest | Log In | Register | Benefits
Channels ▼
RSS

C/C++

C++11's async Template



Related Reading


More Insights




konst

Thank you for the article! I think Bjarne's message that async must not be used for IO makes sense. There are better ways of doing IO, e.g. via concurrency::create_task.

Christopher Yeleighton

Microsoft Visual C++ 11 is available to all licensees of recent versions of Microsoft Windows for free.

Gopalakrishna Palem

As others felt, it would be more appropriate to make the backlog data structure read-only. The threads should only read the data 'designated' to them, not touching the others - opening the scaleout possibilities.

One good way of doing it is, instead of 'dequeue', using some 'indexable' data-structure, such as vector or map for the backlog.

Then each thread will operate only on data related to it. For example, for N threads, thread1 will read all (x%N==0) indices, thread2 will read all (x%N==1) indices, thread3 will read all (x%N==2) indices etc... this way no two threads can access same data.
My personal favorite would be Mapped vector datastructure for the backlog - index the strings with their 1st char in the map and create 26/52 threads (as many as the number of items in the map) and let each thread operate on each set of strings for that char. (If the map is sparse, this may be inefficient). Ofcourse both these two techniques can be combined (since map uses vector or similar indexable datastructure to store its set of strings anyway)

http://gopalakrishna.palem.in

j12x

Mark,

I agree that your course is a good introduction to C++11 threads. However your WordSearch.cpp program is about as confusing as it gets for beginners.

It makes no sense to change the backlog deque. Using a vector to return the results and making the original deque a read-only vector is definitely the way to go. And it will be much faster when the number of threads increases.

I am afraid your students will be misinformed on what threading is all about after seeing examples like this one. I side Bjarne on this one: WordSearch.cpp is definitely an antipattern.

slimshader

Threads should operate on different parts (ranges) of immutable input. There is absolutely no reason for threads to mutate input deque. No mutation = no race conditions = no need for locks.