Click here to Skip to main content

Pablo Aliskevicius - Professional Profile

@PabloAliskevicius

Summary

14,063
Author
4,731
Authority
896
Debator
62
Editor
39
Enquirer
678
Organiser
1,852
Participant
Pablo writes code for a living, in C++, C#, and SQL.
 
To make all that work easier, he uses some C++ libraries: STL, ATL & WTL (to write Windows applications), and code generation.
 
Pablo was born in 1963, got married in 1998, and is the proud father of two wonderful girls.
 
Favorite quotes:
"Accident: An inevitable occurrence due to the action of immutable natural laws." (Ambrose Bierce, "The Devil's Dictionary", published in several newspapers between 1881 and 1906).
"You are to act in the light of experience as guided by intelligence" (Rex Stout, "In the Best Families", 1950).
Member since Tuesday, March 25, 2003 (11 years, 4 months)
  • 18 Oct 2013: Best C++ article of September 2013

 

Contributions

Articles 7 (Legend)
Tech Blogs 0
Messages 246 (Regular)
Q&A Questions 0
Q&A Answers 165
Tips/Tricks 2
Comments 131

Links

Reputation

For more information on Reputation please see the FAQ.

Privileges

Members need to achieve at least one of the given member levels in the given reputation categories in order to perform a given action. For example, to store personal files in your account area you will need to achieve Platinum level in either the Author or Authority category. The "If Owner" column means that owners of an item automatically have the privilege, and the given member types also gain the privilege regardless of their reputation level.

ActionAuthorAuthorityDebatorEditorEnquirerOrganiserParticipantIf OwnerMember Types
Have no restrictions on voting frequencysilversilversilversilverAdmin
Store personal files in your account areaplatinumplatinumSitebuilder, Subeditor, Supporter, Editor, Staff
Have live hyperlinks in your biographybronzebronzebronzebronzebronzebronzesilverSubeditor, Protector, Editor, Staff, Admin
Edit a Question in Q&AsilversilversilversilverYesSubeditor, Protector, Editor, Admin
Edit an Answer in Q&AsilversilversilversilverYesSubeditor, Protector, Editor, Admin
Delete a Question in Q&AYesSubeditor, Protector, Editor, Admin
Delete an Answer in Q&AYesSubeditor, Protector, Editor, Admin
Report an ArticlesilversilversilversilverSubeditor, Mentor, Protector, Editor, Staff, Admin
Approve/Disapprove a pending ArticlegoldgoldgoldgoldSubeditor, Mentor, Protector, Editor, Staff, Admin
Edit other members' articlesSubeditor, Protector, Editor, Admin
Create an article without requiring moderationplatinumSubeditor, Mentor, Protector, Editor, Staff, Admin
Report a forum messagesilversilverbronzeProtector, Editor, Admin
Create a new tagsilversilversilversilverAdmin
Modify a tagsilversilversilversilverAdmin

Actions with a green tick can be performed by this member.


 
GeneralOn global pointers. [modified] PinmemberPablo Aliskevicius4-Apr-12 19:42 
GeneralThe programmer as an artist PinmemberPablo Aliskevicius12-Sep-11 23:09 
GeneralRe: The programmer as an artist PinmemberAniruddha Loya14-Nov-11 21:29 
GeneralThere is no such thing as quick and dirty. PinmemberPablo Aliskevicius30-Mar-09 21:30 
GeneralRe: There is no such thing as quick and dirty. Pinmember ThatsAlok 18-Jul-11 22:57 
GeneralRe: There is no such thing as quick and dirty. PinmemberPablo Aliskevicius19-Jul-11 2:39 
GeneralRe: There is no such thing as quick and dirty. Pinmember ThatsAlok 20-Jul-11 1:07 
GeneralShame on me: a race condition.... PinmemberPablo Aliskevicius28-May-08 8:39 
GeneralWhen multithreading is not an option... PinmemberPablo Aliskevicius19-Mar-07 10:18 
GeneralMore on thread procedures... PinmemberPablo Aliskevicius13-Oct-06 3:36 
You can find a lot of thread classes and patterns on the Web, but you won't find one which fits all your needs.
That, at least, is what happened to me.
Learnt a lot along the way, though. Here goes some of it.

The main issue is synchronization. There are many shapes and flavors, but the general idea is, the least you serialize access to stuff, the better off you are, both in respect to performance and to the risk of deadlocks.
Then again, there are several kinds of multithreading models.

The simplest one, is the 'shoot and forget' kind: print spooling is an example. You start a process running, and it will be done when it's done. Synchronization here is easy: just don't.

Then there are all the foreman/workers models, when one object (the foreman) lives in a thread, and sends jobs to one or more 'worker' objects, which execute those jobs on separate threads. Here, you can use any kind of mutex (in the broad sense of the term, which includes critical sections and the PostThreadMessage() function), and the appropiate one depends on how long each job will take, and how long the program will be 'on the air'. A Windows service must be more robust than a batch process that runs just for a few minutes.
You avoid deadlocks by locking only the message queue between the foreman and each worker (each worker has one), and for the shortest period of time. Particularly, the worker thread should lock the queue only while it's popping a job, and NOT while it's executing the job. In this design, there are no shared resources among threads but the message queue between a worker and its foreman. The Windows API PostThreadMessage() works like that.
An alternative to this model holds only one message queue for all workers. This one is practical if jobs are rather lengthy: the key question is, how often will a worker wait on the shared message queue while another worker is popping a job.
Some other resources might be shared among threads: think about an error log, if you use a text file for error logging. In that case, you need a OS-level lock (such as a named mutex, in Windows) while you're working with that file. Since errors should be relatively rare, you can get away with this; but, if you're logging, let's say, results, a more robust approach shall be needed. A database, for example.

Designing a multithreaded job

It all begins with a piece of paper, divided in several columns:

  1. User: Represents the user thread.
  2. Foreman: Gets a 'start working' message from the user thread, generates 'jobs' and divides them among the workers (or posts them to a common queue, and each available worker will take them from there). Might report progress to the user's thread.
  3. Workers: This column is usually divided in three: A, B, and Murphy. A and B take turns trying to work, Murphy will try to lock an object whenever it's needed by another thread. As you might have guessed, at design time Murphy is my closest collaborator.

    The workers will usually not report progress, but set some state which can be queried by the foreman.

I've been using threads whenever suitable for a few years now, and haven't met a deadlock yet (I fell into a database deadlock once, but that's a totally different story). Performance is usually satisfactory, despite the cost of thread switches: those can be helped by using a LIFO approach for worker threads, if you've got several of them. Debugging is not that much of an issue, since I unit-test as much as possible (not everything, to be honest), and I keep the bussiness logic apart from the threading code: I also like traces better than breakpoints, anyway.

The bottom line: I've heard people say that multithreading in C++ is hard, error-prone and difficult. Well, those reports, in my humble opinion, are exaggerated.

It's just a tool; use it the right way for the right job, and you'll get the right result.

 

Pablo_A

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 22 Aug 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid