models: Optional or as Indispensible as trade capture@@

(A blog post. No need to reply)
Trade booking is the most essential component of any trading system. If a small trader has no computer system, he would make do with a poor man’s trade booking tool like Excel or physical paper to record the trades he did and the positions he holds.

Now compare quant models. I feel models are optional. It depends on the asset class spectrum. On one extreme end are listed equity options. The other extreme might be vanilla government bonds.

Vanilla Government bonds (not futures/options) involve no option-pricing, and are risk-free (notwithstanding the recent Greek/Irish defaults). I would assume there’s no need for quantitative model in pricing/risk systems.

Treasury futures is a different story. There are models to predict price distribution of underlying asset. Since the price is in the future, there are probabilities to work out — a branch of statistical mathematics.

Muni cash bonds need some model? not much. There’s perhaps some credit risk mathematics. The models generate a bid/offer price pair for each bond (based on the position held by the trader), but a trader offering the same bond can choose to use that price (plus a spread) or ignore it. It’s optional to a lot of muni traders.

On the extreme end of the spectrum, listed equity options are priced and quoted on exchanges using widely agreed models (lognormal stuff). Are models necessary to a trader in this market? Can an investor simply follow her instinct to decide for herself a bid/offer price?

transform/copy with push_back on yourself (i.e. a container)

    // abbreviations is a container.
    transform(abbreviations.begin(), abbreviations.end(),
        back_inserter(abbreviations), suffixAdder(c));

Broken, because abbreviations.end() dereferences to a special sentinel “value”. When you push_back on abbreviations, that sentinel “value” shifts to the right.

For example, If initially end() points past the 5th and last node, then after 1st push_back, the 6th position is populated, and the sentinel “item” is now after the 6th node. Initially you wanted to call insert() (and suffixAdder()) 5 times, but now you need to call it 6 times! In fact, it's a infinite loop as we keep shifting the finishing line.

Solution — use front_inserter instead of back_inserter.

c++ private subclass to increase visibility@inherited member

class X : private B {
  public:    B::c;      // c was private, now is public
}
void someFunc(X x){
  cout<<x.c;
}
All my c++ books fail to mention this C++ language feature[1]! The above c++ code (miraculously) works but took me 20 minutes of search —

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devwin32/baseandderivedclassaccess_xml.html
http://www.learncpp.com/cpp-tutorial/116-adding-changing-and-hiding-members-in-a-derived-class/

Both sites explain the rationale —
* private subclass X inherit as private all of parent’s Public members like B::c2. Therefore if main() has a subclass instance, main() can’t access the now-private field
* This language feature allows the subclass to increase accessibility of the inherited member.

[1] I guess this feature is seldom used, but it’s tested in IKM.

GS workload — squeeze-in

Here's another reason GS developers get _2_to_3_ times the work done per man-month compared to other companies on Wall Street.

Say at beginning of Jan I start a 4-week project in GS. Need to release by end of month. Then scope expands by 30% — fine we can absorb it in the same timeframe. Then some unrelated production issue pops up. No one else knows it better, so I let it squeeze into my workload. It is taking many hours, but manager won't extend the deadline. Then other mini-projects squeeze into my workload. I end up spending only half my time on the main project. However, deadline stays the same. I'm now expected to double up my effort to swallow all the new stuff squeezed in.

You may think it's unreasonable, but the hard, cold fact is, the other team members could cope, so must I too. Sometimes they look just like sponge with unlimited absorption.

Mind you, the initial 4-week estimate is not fake — It's a reasonable estimate by all accounts. But GS managers know they can squeeze in more stuff later, so they do.

technically challenging role on the WS job market

Imagine a tech lead role that requires substantial architecture + implementation capabilities.

Q1: are there such roles on the market?
A1: yes I have seen some people playing such roles.

In our discussion, we often say “this job spec seems to be tough, but once hired, just about anyone can do the job.” or “Other people in the team are probably mediocre so the new hire need not be as good as the job spec says.”

In SG, a trading sys developer is paid higher than an Oracle DBA, or a manager at HP, or a system architect at some government companies (I know such friends), so it’s easy to think that trading dev skill set requirement must be significantly higher than the average java developer. But I believe just about any smart java developer can do the job.

Now back to my answer A1 above. Some positions on the market are indeed that challenging. How do we tell which one is? If I know which one is, I could either avoid such a “hot” job, or grab such a “hot” job.

who to command a trading IT team

Among these 4 alternatives below, which one can best command a typical fast-paced, elite trading engine developer team?

Assumption – battle-tested trading system architects are rare, therefore in some cases, manager must select someone slightly less qualified. Needless to say, in reality one person often wears multiple hats.

– BA? Personally I feel No but not sure in practice. Good local domain knowledge, business communications … is half the story. However, some BA’s have recent development experience.
– PM? Often less technical and less hands-on. Perhaps insufficient. Many practical problems require implementation insight
– Architect-on-paper? Must become hands-on to command. Too many uncertainties on the ground. You must be ready to roll up your sleeves. Planning vs execution — The latter dominates.
– Lead developer without architect credentials? Usually good enough. Practical problem solving. I’m biased towards the hands-on developers.

Now for my long term survival to age 65, I feel the hands-on developer enjoys more flexibility and career choices. More geographical choices too.

tcp/udp client+server briefly #connect()UDP

(Note no unix-domain sockets covered here.)

As elaborated in http://bigblog.tanbin.com/2010/12/socket-bind-listen-accept.html

TCP server is socket()-bind-listen-accept
TCP client is socket()-connect

UDP server is socket()-bind
UDP client is socket()-bind { optional }

(UDP both sides similar. See the nice diagram and sample code in http://www.tenouk.com/Module41a.html)

However, after the set-up, to move the data, UDP supports several choices.

hands-off manager@@ According to Lego of PWM

Q: most app teams have no _dedicated_ DB guy?? (though I know an ex-DBA in a trading floor RTB team) Database logic change is usually part of a BTB developer’s job. Database investigation is both RTB/BTB work
A: in a _data_service_ or reporting application team, there IS someone dedicated. But you also need some auxiliary skills like ETL or reporting _products_.

Q: many hands-on developers endeavor to move UP to a hands-off manager role?
A: if you manage a team of 5+ developers, then it’s hard to be hands-on.
A: if you are hands-off and just manage subordinates/users/upstream/downstream…, then you are vulnerable. Bottom line is “deliver results to business”. Example – system slowdown, users complaining. Hands-off manager must know how to command his troop to resolve the issue, quickly. If a manager has no system knowledge, he can’t command. It all depends on the expectation on the leadership role.

%%A: I’d say the hands-off manager (a “him” for example) needs to show off knowledge of his system internals, to gain credibility and user confidence. What if a subordinate (a “her”) openly exposes the manager’s lack of system knowledge? If the knowledge is expected of him, then she’s hated. If he can easily shrug it off as “low-level” details he doesn’t usually deal with, then he’s safe. In a production outage conference call, she should listen to his cue.

Q: so a hands-off manager just needs the BA’s skill – coordinate and clarify with users and other systems?
A: manager needs that skill but also need to do more than a BA.
%%A: a lot of GS vice presidents are really Biz Analysts. I feel in PWM Tech, technical is less valued than business or mgmt trec. More than once, senior managers openly say that if they retain a key manager, then the entire dev team can be rebuilt from 0.

Q: so to keep business users productive, every app manager (incl. hands-off ones) needs some high-level system knowledge? He needs no low-level knowledge as required of developers?
A: right

product control job function, briefly

PC is all about validating daily pnl. Traders own their trading accounts, so they decide on the mark-to-market, but validation needed by a third party – comes PC.

Fincon is a different job function — posting pnl to GL.

3 common levels of securities —

Level 1: system generated valuation. Includes exchanged traded securities.
Level 2: model-validated valuation. Covers Most OTC derivatives.
Level 3: no suitable model. Covers CDS

I feel realized PnL is straightforward. Unrealized PnL needs mark-to-market. In 1997 I invested into commodities futures. Every day, I saw unrealized profit, then it turned to unrealized loss. That was Level 1.

sybase – number of user connection

1> sp_configure ‘number of user connection’

2> go

 Parameter Name                 Default     Memory Used Config Value Run Value   Unit                 Type      

 —————————— ———– ———– ———— ———– ——————– ———-

 number of user connections              25     1990886        1700         1700 number               dynamic   

 

 

1> sp_monitorconfig ‘number of user connection’

2> go

Usage information at date and time: Dec  2 2010 10:35AM.

 

 Name                      Num_free    Num_active  Pct_act Max_Used    Num_Reuse  

 ————————- ———– ———– ——- ———– ———–

 number of user connection         376        1324  77.88         1363           0

 

auto_ptr vs scoped_ptr, in essence

(See also post on auto_ptr vs shared_ptr.)

Boost scoped_ptr ++forbids++ copy and assignment[2] whereas aptr permits these 2 accidents to happen.

scptr gives up ownership carefully, via reset, where as aptr does so accidentally and freely.

A const auto_ptr (i am not saying “auto_ptr to const”) is very similar to a scoped_ptr. There’s a small difference — P 13 [[boost]]

[2] Note some text explicitly says a scoped_ptr can’t be the LHS of assignment due to compilation error.

SSocket→ BBind→ LListen→ AAccept#details

My focus is internet-socket (not UnixDomain-socket) and server-side TCP. UDP and client-side will be addressed later.

“Bind before Listen” — http://www.linuxhowtos.org/C_C++/socket.htm also shows the same flow.

1) [SC] socket() system_call calls into the kernel to _creates_ a new socket and returns a socket FILE descriptor integer

2) [S] bind() specifies the local end point. The bind() is the choke point to specify (socket() doesn’t) the local end point. It can bind to “any” one address available to the hosting OS, but must bind to a fixed port(??). I know bind() can use INADDR_ANY to bind to multiple addresses. Some say each socket can bind to one address at a time but I don’t think so and I don’t care. To an api user like me, bind() can indeed connect me to multiple addresses.

In a multicast receiver (no “server” per se), bind() specifies the group port, not the local port.

3) [S] listen() is the choke point to specify the _LLLLLLength of the queue. Any incoming connection exceeding the queue capacity will hit “server busy”

4) [S] accept() is the only blocking call in the family —
** If no pending connections are present on the queue, and the socket is NOT marked as non-blocking, accept() blocks the caller until a connection is present. If the socket is marked non-blocking and no pending connections are present on the queue, accept() fails immediately with the error EAGAIN.

The name accept() means accept CONNECTIONs, so it’s used for connection-oriented TCP only.

programmatically use a c# field name as a string -Expression trick

Say I have a literal string whose value is the name of a field/method/property. As I frequently rename these class members, I want the string value be update automatically either at runtime or at refactor-time.
Here’s the “runtime” solution (more cool than refactor-time, IMO)

///

/// Usage:
/// string fieldName = GetMemberName(() => MyClass.anyMember);
///

public static string GetMemberName(Expression<Func> expr)
{
  var body = ((MemberExpression)expr.Body);
  return body.Member.Name;
}

option valuation graph – hovers above hockey stick

The vanilla European Call (or Put’s) valuation graph against spot price is a _smooth_ curve slightly above the kinky hockey stick. I like to remind myself the curve depicts a “range of possibilities”. The curve is a Snapshot of a bunch of what-if possibilities such as “If underlier becomes $S my call’s theoretical value is $C”.


Q: since S and C axes are both in dollars, is this graph drawn in scale? Can I take a point on the smooth curve and read actual (x=100,y=20) coordinate values and interpret them as S=$100, C=$20?
A: yes you can. In fact the slope of the hockey stick handle is  45 degrees (representing a delta of 100% when sigma is 0.)

Let’s assume strike is $100.

For OTM, C might be $7 when S is $50 (below strike). C is a small fraction of S. In other words S/C represents a high leverage ratio for an OTM call (or put). C is purely time-value, since intrinsic value is $0. Let me repeat, for OTM call (or put), leverage ratio is high because valuation is purely time-value.

The magnitude of the leverage ratio (and the valuation of the call/put) depends fundamentally on the contest between ln(S/K) vs σ√ t , as described elsewhere in this blog.

For ITM, leverage ratio is much lower and probably less meaningful. When S is $180, C is dominated by intrinsic value of S-K = $80. In other words, the smooth curve hovers slightly above hockey stick handle

when Must RHS be a Lvalue expression

Advanced!

Q: when must the RHS be a L-value expression?
%%A: it must if LHS is a non-const reference variable, as the var must bind a Location. This applies to function arguments, too.

C++ true-blue call-by-reference requires the argument (i.e. RHS) be an L-value. RHS can’t be a literal “3” or a math expression. An L-value has an address so can be passed by reference.

Q: can RHS be a new-expression? Does it qualify as a RHS L-value?
A: YES tested — qq/ *(new int(8)) /, presumably because this object has an address

Q: what if the LHS is a ref-to-const?
A: then RHS can be literals like “3”. Tested.

Q: how about a function call returning an object? Can this be an RHS L-value?
A: YES if the function returns by reference. Tested. Such a function-call IS a true-blue L-value-expression.
A: NO if the function returns by value. Such a function-call ISN’T an L-value-expression.

hasa^holdsa — in c++ ^ java

C++ offers an alternative to HASA

– hasa means Umbrella class holds a nonref field of the Component class
– holdsa means Umbrella class holds a ptr/ref field, though reference field is less common.

As a contrast, in java,
+ hasa is the only option for primitive fields
+ holdsa is the only option for non-primitive fields

Back to c++,

– holdsa adds exactly 4 bytes to sizeof(Umbrella)
– hasa adds exactly sizeof(Component) bytes to sizeof(Umbrella)

^ hasa adds compile-time dependency. Since compiler needs to size up umbrella class, umbrella class definition must include full composition (field listing) of the component class. This leads to compile time dependency.
^ holdsa is pimpl. reduces compile time dependency. FCD required.

pointer≠address #with exceptions

See also https://bintanvictor.wordpress.com/2012/07/02/3-meanings-of-pointer-tip-on-delete-this/

Some people say “A pointer is an address”. Misleading! I’d say “pointers can sometimes be used as addresses.”

When people talk about a pointer, they mean either an address or a pointer-variable . A variable is a source code entity. After compilation it can be either an object or optimized away.

An address of the white-house is a stable, immutable text you can write on paper. But a pointer-var can be reseated, so it’s more like a “variable holding an address”.

(A non-reseatable pointer is pretty much an address. Notable examples include the “this” pointer and the vptr, both hidden fields. Update — now i think both can be reseated — in ctor and in multiple-inheritance.)

int myInt = 3;

Look at the above illustration. The variable myInt is NOT the same thing as the value “3”. Unless optimized away, myInt is both a variable and an object (see post on Mutable,initialize etc), has an address and occupies 32 bits and can change its content. Likewise, a pointer var occupies 32 bits of memory and can change Content.

Note nonref variables like myInt is permanently seated at a address. It gets allocated 32bits once and for all, and never re-binds to another object (same in java/c/c#). Similar to reference variables. (In contrast, python simple myInt variable behaves differently. See other posts.)

— I don’t think there’s another c++ construct that is as monolithic as address-of-a-house —

* qq( &myObject ) is the closest thing — It’s an address + type information. Whoever using qq( &myObject ) need the address and need to know its type.
** qq( &myObject ) is an address + type and is subject to variable scope control. You can get this address only if you are in its scope. Compiler controls scope(?).
* A const pointer is also like an address-of-a-house.
* A reference doesn’t feel like an address. Granted, a reference is permanently seated to an address, but when you read the reference you don’t see that target address — You must take the address of the reference.

c++^perl zbs insights: vastly different values

Looking at my blog posts on c++ vs perl, the value of my insights in these 2 languages are very different.

* c/c++ is the foundation underneath java, c#, OS, DB and most infrastructure software. perl is something most systems can make do without
* c++ performance is still the envy of most languages in the context of many high-value systems
* compared to c++, perl and php are niche languages, even if ranked in the top 10 languages
* there’s a large following and corporate endorsement for any popular language like ruby, groovy, pike, LISP, Tcl, but it doesn’t mean these are as important as c# or c++. Be critical. Be comparative. Be objective. Take a perspective.
* If you look at wall street salary, some perl or mainframe jobs could pay $100/hour, but be very critical and look beyond short term.

##front office technical know-how

–Most imp to hiring and productivity–

swing, wpf — more complex than all other tools
pricing math
pnl rollup, MTD, unrealized.. is imp to traders
fire fighting
c++ …rv
mastery over db query 

FIX
general low latency techniques
gemfire — imp to both risk and real time pre-trade. Let’s xx2set up
trade booking, cancel, MOD
java OOM — simple techniques
position mgmt is important to traders
trade blotter basics

2011 BGC c++IV #socket #done

Q3: what can go wrong during a write to a socket??? (LG)
Q3b: if buffer is full, will the writing thread block???
%A: by default it blocks, but it’s probably possible to configure it to return an error code or even thrown an exception

Q: blocking vs non-blocking socket?

Q: socket programming – what’s select ?

Q: Should base class dtor always be virtual?
%%A: I would say yes. If a dotr is really not virtual, then it’s not supposed to be a base class. However, SOF mentions: “If you want to prevent the deletion of an instance through a base class pointer, you can make the base class destructor protected and non-virtual; by doing so, the compiler won’t let you call deleteon a base class pointer.”

Q: how many ways to share data between 2 processes? How about shared memory

Q: synchronize 2 unix processes accessing a shared file?
A: named semaphore

RE: Dealing with duplicate records in table which has no primary key

—–Original Message—–

Sent: Tuesday, December 07, 2010 11:47 AM

Subject: Dealing with duplicate records in table which has no primary key

 

How to remove duplicate records if table has no primary key

1. rename FOO to FOO_BAK, create FOO as select distinct * from FOO_BAK

 

[Bin] Did you test?

[Bin] It does work. Nice! How about  

 

— Solution 2:

 

Select distinct * into NEW_FOO from FOO

 

Revoke all access from FOO and NEW_FOO. Then create a public view to either point to FOO or NEW_FOO, so we can easily point applications to either of the 2 tables.

 

This preserves the original FOO table as is.

 

— Solution 3:

 

Select * into FOO_BAK from FOO

Truncate table FOO

Insert FOO select distinct * from FOO_BAK

alter table FOO add ident int identity — adding identity column so no more identical rows from now on

 

This avoids sp_rename, a troublesome Sybase system procedure to avoid.

GTD on-time: GS time booking

A developer position is created by business, and funded by business. Why wall street pays sky high for developers? Because business generates high profit and business wants more IT guys to help them shorten “time to market”.

In my GS system, code quality is low priority[1]. But many GS people say productivity is higher than other banks. I believe “productivity” is measured by the number of many-hours actually spent and _booked_ to the project. But how is GS time booking different from other banks?

For a developer, the number of man-hour in a year is a fixed 1920 man-hours. I typically book into 20 – 40 projects each year. I feel at least 6 levels of managers above me (up to MD and partner) look at my time booking data to manage their budget and expenditure. Unlike other banks, GS line managers look bad in front of very senior managers if developers overbook time. Developer is a scarce resource in GS and many levels of managers watch their burn-rate. Other banks are less burn-rate-conscious.

Suppose a project is budgeted 40 man-hours but is expanding to 60 due to scope increase, then other projects must shift to the next year. Business don’t like their project postponed, so they push IT managers, hard. IT managers want to please the business, so they push developers. Other banks are less determined.

In GS, there’s real resistance to postponing projects to next year. Most employees and consultants endeavor to deliver in time even though requirement often change. In other banks, IT guys say “we aren’t machines or supermen”, but in GS many developers are expected to be 80% more productive than other banks. This is achievable if a developer knows her system inside out.

If I say “My project is booking 50% more time because requirements changed drastically”. Manager would come down to scrutinize and ask “Every project has changing requirements, but they don’t exceed budget this much. Why are you slower?” I feel other banks don’t say this.

I feel GS is more productive because business can really push IT. Both business and IT are motivated by promotion and bonus

[1] “Finish your work on time, with reasonable quality”

#1 evidence managers seek when keeping/terminating a consultant

update — Shuo (and others) suggests that boss relationship overrides all other performance metrics including productivity, but I feel Wall St is slightly different in terms of Salary and time-to-market.  If you get things done on time it really helps boss’s promotion.

I think boss relationship means the non-objective, non-measurable, non-quantitative evaluations of a consultant.
—-
Q1: What do managers look at when keeping/terminating a consultant?
A: Note Some consultants are hired on prod support budget, but majority of consultants are hired on project budgets, so let’s focus on the latter. The key question in this case is

Q2: Can this guy implement required *functionalities* on time, so business can use them on time? Note it’s all about *business-value* [1]. Requires a, b, g but not c, d, f below

a) Familiar with current codebase in the system. You need to spend enough time in a given system. Every site is different. No portable knowledge.
b) practical java/sql/unix know-how. also eclipse, javascript, cvs, html practical know-how.
c) code quality — code smells like “copy-paste”. Managers don’t care about this.
d) test coverage
f) advanced knowledge of java/sql/threading/GC/collections …. — completely irrelevant to Q2 above. Managers don’t care about this.
g) code readability, if team lead worries about taking it over personally

[1] When managers ask (or present) progress updates, they prefers high level terms, so they could estimate 40% (or whatever) of the required “business value” are now achieved. The higher you hit the management rank, the more you need to communicate in terms of business value, time left, time spent, additional manpower needed …

When a critical project needs to be done on time, many managers prefer consultants because consultants don’t take fake sick leaves or “family leaves”, don’t go to trainings, and are generally experienced.

Managers generally feel FTE are here to stay. Consultants and FTE are equally mobile job hoppers, but consultants are more subject to budget cuts. This factor has a profound influence. I have seen or experienced budget cuts affecting consultants in Verizon, GS, and other companies.

customize new/malloc, briefly

For a given class C with a derived class D,

C::operator new(……); // is inheritable by D; implicit static; allocates raw memory
C::operator delete(…); // is inheritable by D; implicit static; deallocates raw memory i.e. return to the “pool”

C(……..); // never inherited; never static; turns allocated raw memory into object
~C(void); // never inherited; never static; turns object into raw memory, to be deallocated.

All of them can be private.

As explained in another blog post, the new expression like “new D()” can’t be overloaded. This expression invokes the operator new and the ctor.

[[effC++]] has a lengthy chapter on how to customize new/delete.

P139 [[understanding and using c pointers]] has a short chapter on how to avoid the overhead of frequent malloc/free operations. Note this is NOT replacing malloc with our own malloc.

P70 [[understanding and using c pointers]] has a one-pager super-simple wrapper over free()

y many java architects avoid static methods #threading

Hi XR,

Not sure why my architect doesn’t like static methods. I have some stateless method. It takes an object, pass to a static JNI method, then gets an output object, and returns it. I created a simple wrapper class around this single method. I feel making the method non-static requires callers to instantiate the class first. It would give the illusion that there’s state in the instance. It can also lead to unnecessary object proliferation.

My colleague mentioned threading as a reason. He’s not a newbie on threading, but I feel a stateless method is safest when it’s static. It is possible that he worries that this static method may one day access some static field? In that case, making it non-static and requiring every caller to have its own instance can help a bit.

Another colleague (elsewhere) also felt static methods are bad in threading. He’s also experienced in threading.

In threading, My habit is to keep things simple. If a method is stateless, i keep it static and have zero static field in that class. When people look at the code they immediately see it’s stateless, just like Math.abs().

::operator new() customization — ARM

ARM Jargon warning —
“the new-operator will call the function operator-new()” actually means
The new expression will call the function operator-new()

P60 ARM makes it clear that function operator-new can be overloaded — new-expression is not something you overload. Ultimately, op-new is really just a function and as such can be overloaded. The global qq{::operator new()} can be overloaded for class T, using T::operator new() [2]. Quite common and safe. See Scott Meyers and [[c++primer]]

Q: But can you overload the global  ::operator new()  ?
A: yes. P60 ARM Bullet point 2
A: yes. “Unlike class-specific new, global new is usually overloaded for debugging purposes. In some cases, however, you might want to overload global new and delete permanently, because you have a better allocation strategy or because you want more control over it.” according to [[C++InAction]] http://www.relisoft.com/book/tech/9new.html

[[c++TimesavingTechniques]] P128 has a rather simple yet practical replacement for global  ::operator new()

[2] the static method syntax. Class-specific Op-new() is implicitly static since you never call op-new on an existing instance to grab memory for a new instance.

dissect`parametrized functor declaration-cum-construction

typedef void (*WorkerFunPtr)(const std::string&);

template<typename FunT,   // The type of the function being called
         typename ParamT> // The type of its parameter

struct Adapter {
   Adapter(FunT f, ParamT& p) : // Construct this adapter and set the
      f_(f), p_(&p) {}          // members to the function and its arg

   void operator( )( ) { // This just calls the function with its arg
      f_(*p_);        
   }
private:
   FunT    f_;
   ParamT* p_;  // Use the parameter’s address to avoid extra copying
};
void worker(const std::string& s) {} // a free function
int main( ) {
   boost::thread thr1(Adapter(worker, someStrVar)); // this line should be decomposed as
 
   Adapter // specialized type
        myAdapter(worker, someStrVar); // constructor

   boost::thread thr1(myAdapter);

Specializing a class template and then constructing an object is always a confusing process. Unfortunately we often have to do it in one line.
* specializing the Adapter template is simple enough, if we treat WorkerFunPtr as a simple type rather than a complicated typedef.
* constructing an object is simple enough
* However, the constructor is parametrized (since it’s in a class template), therefore the arguments have to match the actual types specified in the template specialization.
** worker is a function but implicitly converted to a function pointer. Its type does match the actual type WorkerFunPtr
** someStrVar does match the actual type std::string
==) Therefore this line constructs an object of the specialized type, using the 2 arguments — a func ptr and a string

Last lines passes the newly constructed myAdapter OBJECT to the thread constructor. Implicitly converted to qq/myAdapter.operator()()/ i.e. a no-arg function.
 

wait() locking/unlocking #boost

For both boost-thread, java 1.4 wait() and java Condition variables, here’s the sequence of events
————————————–
1) before wait(), application programmer must lock explicitly.
——– going into wait() ——-
2) unlock implicitly

3) lock implicitly
——– return from wait() ——

Q: look at (3) — will an abnormal “exceptional” return from wait() still grab the lock?

A: yes for boost. This is a choke point — before the thread emerges from “under water” and awakes from “the avatar dream”, it must grab the lock — grabbing indefinitely if it’s not available.

A: yes for java 1.4. i guess the wait() method never throws any checked or unchecked exception except those 2 listed in API. The standard wait() call is wrapped in synchronized block, so it’s obvious that exceptional return from wait() must grab the lock.

A: ditto for Condition.java. See example in javadoc

user->kernel thread mapping]Solaris

P91 Hughes and Hughes diagram portraits the most general scheme, where 1 or more user threads (LWPs) map to a kernel thread. This 1-or-m:1 mapping depends critically on an unnoticed layer of thread library, which

* receives thread creation requests
* handles thread scheduling

Therefore, in general we have to assume threads are managed by the thread library, and threads we create may be invisible to kernel, if they exists as USER-threads.

The pthread thread-id TID doesn’t always map to a kernel thread id. Pthreads do not need to be implemented with kernel threads, so some thread libraries are entirely user threads or mixed. P92 Hughes&Hughes confirms Pthreads is a hybrid model.

java offers green and native threads.
– green — [[weblogic definitive guide]] and [[java threads]] clarify that multiple user-threads map to a single kernel thread under the green thread model. Therefore thread creation and scheduling is invisible to kernel.
– win32 native threads — one java thread maps to a kernel thread
– solaris and linux generally support native threads better. one-to-one mapping