Nothing Special   »   [go: up one dir, main page]

New Posts: Like Comment Share Send

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

163 1 comment · 2 shares

Home My Network Job


Like Comment Share Send
New posts

#india • following
2d • Post from Amrita Singha

WORK FROM HOME

TIME: Flexible Timings

INCOME: 30,000/- to 60,000/- as Per Month


…see more
8 4 comments

Like Comment Share Send

New post in C++ professionals group

Demitri Swan • 3rd+


Software Engineer | Go | C++ | C | Python | Bash
1w • Edited •

Hello friends. I'm dipping my toes into C++ concurrency. Coming from Go, the common
patterns seem a tinge different and more reminiscent of the native concurrency
primitives. I've solved this LeetCode concurrency problem but would like to get some
additional feedback if possible.

Here is the problem statement:

Suppose we have a class Foo (with methods first(), second(), third()). The same instance
of Foo will be passed to three different threads. Thread A will call first(), thread B will call
second(), and thread C will call third().

Design a mechanism and modify the program to ensure that second() is executed after
first(), and third() is executed after second().

Note: I should mention that you're given a class with method signatures without
implementations.

Thanks in advance!

#concurrency

-- Edit --

Here is this gist: https://lnkd.in/gcj_Y-Rf


Home My Network Job

New posts

63 51 comments

Like Comment

Add a comment…

Most recent

Demitri Swan Author 1w

Software Engineer | Go | C++ | C | Python | Bash

You guys are awesome. Thanks for the feedback folks.

Like Reply

Arun Kapale •
3rd+ 1w (edited)
14+ years of extensive experience in Linux, C++98, Python, C++11, C++14, C,
Design Patterns, Multithreading, IPC, STL on Linux
a nice example of conditional variable and mutex lock, some tweek as unlock
not require, but its working like round-robin too, Appreciate it!!
Home My Network Job

Like · 2 Reply · 1 Reply


New posts
Guetcho G. •
3rd+ 1w
C++ engineer and android security researcher at ZLabs.

I wouldn't call that a nice example. The author has to learn a lot.

Like · 1 Reply

Demitri Swan Author 1w

Software Engineer | Go | C++ | C | Python | Bash

Just solved a few more concurrency problems on LeetCode in C++. Pretty


soon I'll have solved all of them. Wondering if LeetCode detects race
conditions (possibly with Valgrind) though 🤔

Like Reply

Varun Bhatt •
3rd+ 1w
Principal Software Engineer at Nuance Communications

No need to unlock explicitly. When predicate becomes true in wait, it releases


the associated lock and that is the reason why we use unique_lock over
lock_guard with condition variable wait. Also, unlocking at the last line of the
method is anyways redundant.

Like · 2 Reply · 7 Replies

Load previous replies

Demitri Swan Author 1w (edited)

Software Engineer | Go | C++ | C | Python | Bash

Douglas Menegon Cordeiro I do think it's valuable advice to use block


scope effectively for RAII-based objects that'll destroy based on said
scope...so thanks.

Like Reply

Suhail Khan •
2nd 1w
C++ Software Engineer | Low latency C++ | HFT | Former SDE Intern at CarWale

A better way would be to utilize acquire-release semantics of atomic


variables.

After finishing executing first(), thread A would set an atomic boolean


variable x.

Thread B will loop while x is not true. Once it is true it executes second() and
sets another atomic boolean variable y.

Similarly, Thread C will loop while y is not true. Once it is true it executes
third().

This ensures thread A runs before B which in turn runs before C.

Like · 3 Reply · 8 Replies

Load previous replies


Brian Budge •
3rd+ 5d
Research Software Tech Lead/Manager at Reality Labs Research (we'reHome My Network Job
hiring!)

New
Gergely Máté probably, but posts
again it depends on expected wait time.

Like Reply

Ophir Ovadia •
3rd+ 1w
Chief System Engineer / Software System Engineer

You may use 2 binary semaphores to let 'second' wait for 'first' and 'third'
wait for second.

But thinking of a more generalized solution for the orderly execution of N


functions, you can use an array of binary semaphores.

In that case you only need one function which receives a function to execute
and a number, the number determines the order and is used as an index to
access the respective semaphores in the array (you wait on semaphore
(index-1) and you notify semaphore (index)).

This way you also avoid "notifying all", which is not elegant nor efficient when
you have many threads.

Like · 3 Reply · 1 Reply

Demitri Swan Author 1w

Software Engineer | Go | C++ | C | Python | Bash

Great feedback. Thanks.

Like · 1 Reply

Suman Kumar Madamsetty •


3rd+ 1w
Software Developer, Tests-Automator, DevOps Engineer, Scrum, ISTQB, PRINCE2
certified.

Have a function pointer array or a queue.. sequenece first(), second() and


third() functions in needed order in array/queue and pass it to a thread
runner… use locks for any critical code regions.

Like · 1 Reply · 2 Replies

Load previous replies

Suman Kumar Madamsetty •


3rd+ 1w (edited)
Software Developer, Tests-Automator, DevOps Engineer, Scrum, ISTQB,
PRINCE2 certified.

Having first(), second() and third() synchronized for thread safety.


Execution sequencing with function pointers and a worker queue avoids
locks and improves maintainability. The idea is to keep the functions
atomic and independent of state whereever possible.

…see more

Like Reply

Lu Huang •
3rd+ 1w
Senior Software Engineer @ Microsoft
Not as amusing as sleeping for a monotonically increasing number of
seconds before printing :) technically lock-free!
Home My Network Job

Like · 1 Reply
New posts
Guetcho G. •
3rd+ 1w
C++ engineer and android security researcher at ZLabs.

Stop thinking in Go. Start thinking in C++. Your code is too complex. And you
have to think about joining the threads. Stop trying. Just do it. In C++.

Like · 2 Reply · 3 Replies

Load previous replies

Demitri Swan Author 6d

Software Engineer | Go | C++ | C | Python | Bash

Hemant Makhijani Take a look at the problem specification.

Like Reply

Brian Budge •
3rd+ 1w
Research Software Tech Lead/Manager at Reality Labs Research (we're hiring!)

For C++ concurrency, I'll shamelessly plug my library. Check out


https://github.com/facebookincubator/dispenso for some task parallel
primitives like parallel for loops, Futures, pipeline, etc

Like · 1 Reply · 4 Replies

Load previous replies

Demitri Swan Author 1w

Software Engineer | Go | C++ | C | Python | Bash

Do you have an opinion on Seastar? I’ve heard about it before I got into
C++ due to ScyllaDB

Like Reply

Brian Budge •
3rd+ 1w
Research Software Tech Lead/Manager at Reality Labs Research (we're
hiring!)

Demitri Swan thanks for asking. I'm not very familiar with it, but it
seems interesting for service communication. dispenso is primarily
intended for high performance compute-bound concurrency, and
doesn't currently have much tailored for I/O

Like · 1 Reply

AMIT KUMAR GUPTA •


2nd 1w (edited)
Technical Lead at Infinite Convergence

Thanks for posting, This can also be solved using semaphore. :)

Like · 3 Reply

Stefano Fiorentino •
3rd+ 1w
Modern C++ · Yocto Project · Wi-Fi · BLE · Co-Founder at Italian Embedded ·
Organizer at C++ User Group Lugano
Home My Network Job
I think this is a good example where you can use future/promise as a
signalling system. I guess the problem can anyway be solved with a finite
New posts
state machine with almost zero locking costs

Like · 3 Reply

Daniel Soroko Donvito •


3rd+ 1w
Lead Engineer UC & Interoperability

There are some advanced techniques based on atomic instructions over


pointers that allow you to get better performance without using critical
zones, and locks. There is a very good book that I read years ago on that
subject: "Concurrency in action...".

Kind regards Daniel

\\DS

Like · 1 Reply · 2 Replies

Load previous replies

Demitri Swan Author 1w

Software Engineer | Go | C++ | C | Python | Bash

I haven’t gotten into it yet but now the time is ripe for some page
turning.

Like Reply

Diego Escala •
3rd+ 1w
Empowering creators @ TikTok

No reason for the first one to wait ;)

Like · 1 Reply · 5 Replies

Load previous replies

Demitri Swan Author 1w

Software Engineer | Go | C++ | C | Python | Bash

Great feedback. Thanks for checking it out.

Like · 1 Reply

Bilal EL-KIRI •
3rd+ 1w
Project Manager at HPS

I'll keep this in mindqqq1

Like · 1 Reply

Prabhakaran Rajamanickam •
3rd+ 1w (edited)
Sr. C++/C Developer | Unix | Linux | Aix | Databases| Telecom | Banking |
Analytics | Scripting |

Paste the code here and tell your problem statement clearly with output, let
me also try, i am into C++11,C++17 recently. Also full code with
implementation, i believe now any one can print right? I mean two can print
first, then three, then one, or it can be depends on the PC pointing
Home My Network Job
…seetomore
intructions, all three will print but it can be of any order right. You want

Like · 1 Reply · 2 Replies New posts

Demitri Swan Author 1w

Software Engineer | Go | C++ | C | Python | Bash

Cool. I've updated the post with a link to the code for the problem.

Like · 1 Reply

Prabhakaran Rajamanickam •
3rd+ 1w
Sr. C++/C Developer | Unix | Linux | Aix | Databases| Telecom | Banking |
Analytics | Scripting |

Demitri Swan Thanks. will try.

Like Reply

Follow Demitri to see more posts like this


Following Demitri will show more content like this in your feed. Follow

Virat Patil commented on this

Ashwini Patil • 2nd


Test Lead at Accenture| Murex FO | Scrum Master | ISTQB
1mo • Edited •

Thank you Accenture and Vinay Pandey for giving me the opportunity to be the part
of this fabulous journey :)

#TechStar2022

You might also like