Concurrent thinking

Back when I was a kid, learning about programming in college, I was left with the impression that parallel computing/multi-threading is not easy. 1. We learned about race conditions, deadlocks, semaphores and all that. Once I started working, the idea that multitasking is hard was reinforced by the absence of it in our CAD/EDA tools which surely would have benefited from the effort. 2

In recent years, my views on concurrency have finally changed a bit, first via the simplified threading model that Perl provides and more recently through my Android development adventures.

A couple years ago, I supported a feature that required nightly crawling of a fairly large directory structure, a three deep tree with ~10,000 files each of which needed to be read and parsed. Normally, it would be ok for stuff to run for an hour or two, but in this case, I had to stop the service that depended on the tree until processing was finished. So I needed to parallelize.

When I read a bit more about Perl’s threading model, I was surprised to learn that it’s mostly just forking, except it’s easier to pass data structures between threads/forks. 3. So I created 10 threads, each of which read it’s own subtree and passed back the results of parsing 4

The crawler utility wasn’t rocket science, but it did open my mind’s way of thinking a bit. Multi-threading doesn’t always have to be fraught with danger if you just limit the amount of shared data. Threading may be intimidating, but forking is easy. There are many tasks that lie somewhere in-between.

With Android development, I’ve been forced to open up a bit more. Android doesn’t allow network calls from the main UI thread. You have to do it in a separate thread. Android doesn’t allow UI updates from anywhere other than the main thread, so I can’t just do everything in my side thread. 5

As I’ve worked on my SMPTE app, I’ve found concurrency is a nice way to divide the problems I’m trying to solve:

  • the main UI thread. Update the display, respond to buttons
  • a thread that other phones can talk to for syncing. Two+ actually. One waiting for socket requests and one for each accepted connection
  • a thread that reaches out and syncs to a master phone.
  • a thread that computes the current timecode.
  • I imagine I’ll have a thread for generating the SMPTE audio signal.

There’s very little that needs to be communicated between these. It’s really just the stored time adjustment value. In a way, even though I’ll have a bunch of threads, since there’s only one long that is shared, it’s really a bunch of almost independent processes.


  1. did people use the term multi-threading back then?

  2. I do remember that my friend Trey tried to add it to our signal router with mixed success. I don’t believe that effort ever made it into production

  3. forking usually requires you to setup a socket and serialize all your own data. This isn’t hard but I never took the time to write a series of helper routines.

  4. Most of the old slowness was due to the loop: ask for a file, wait for the fileserver, process quickly, ask for another file, wait some more

  5. I also found it interesting that garbage collection in Java happens in its own thread.

I have a better idea of the time

In a previous post, I talked about getting accurate time syncs between Android phones. My main objective has been to learn Android app development and I’ve found the best approach for me (for everyone?) is to implement a project. A SMPTE timecode generator is what I chose.

 

To develop a mobile app, you have to get confortable with things like:

  • creating a GUI layout based on XML and learning the available controls.
  • understand the main event loop related mechanisms, Activity, in the case of Android.
  • know how to change colors, fonts and sizes
  • how to use icons. The android ecosystem has many icons available but then you have to copy several image files for each icon you want to use to account for the different sized devices.
  • you have to be cognizant of when different system APIs were introduced. I want my app to be runnable on old phones 1

To talk a bit about my SMPTE app, I am almost comfortable with my ability to sync between phones. I considered something like syncing using some sort of audio signal. One master phone could emit a series of blips and the slave phone could emit its own blips and listen to the combination of the two. As suggested like my friend Vanya, I could treat it like a PLL. The child phone would look at the sound it’s hearing and move its own signal forward or backward. I will probably try this as well but I haven’t yet played with Android audio stuff.

The approach I’ve taken so far is to use a socket connection between phones. The slave phone sends a kind of ping to the master which responds with its current time value. If the slave does 10 of these in quick succession, one of them is usually within 10ms round trip. I figure/hope that anyone wanting to use an app like mine would have an old wifi router laying around.

The display gives the date and time followed by a hundredth counter. So in this picture the four devices are within 5/100 sec or 50ms.

IMG_5064

And here’s another with lucky timing. 2/100 sec.

IMG_5065

I imagine that some/much of the the differences are due to timing accuracy of the Android system. Updating the display a kind of round about. I have a scheduling thread that calls by callback every 10ms. That callback decides what to display and sends that along to the main UI thread.

SMPTE timecode is only as fast as 30 frames per second, so 5ms or 10ms deviation is probably fine.

I’m curious to see how/whether things change with the audio output. In that case, I don’t need a timer. I will simply feed numbers into an audio buffer and add more as the buffer empties. Is it harder to get consistency with audio or display timing?


  1. I really like the idea of finding new uses for old phones we’ve just thrown in a drawer somewhere

How do you know you’re straight?

This post has a bunch of footnotes. If you’re reading this via my email newsletter, I strongly recommend you click the title of this post to read it in a web browser. You’ll be able to click each footnote for a popup. It makes for an easier read.

 

Last week, the US Supreme Court decided in favor of marriage equality for Gays and Lesbians. This issue has been thought provoking to me in a number of ways, so perhaps I’ll write several posts on the overall topic. The thought that sticks out to me the most is “How do I know I’m straight/no gay?” Many opponents to marriage equality try to argue that homosexuality is a lifestyle choice; that one can be “born again hetero”. This post shares some of my life experiences that have led to my opinion that this is a BS argument.

When I think of my own exposure to the idea of homosexuality, there are three people in my life who have shaped my opinion more than others. The first of these is a guy in my fraternity pledge class at MIT. I’ll call him TD, but anyone familiar the members of that era will know who I’m talking about.

In the early days of knowing him, TD was pretty brash. He said all kinds of crazy and perhaps even offensive stuff. Borderline racist, and not so borderline homophobic 1. As pledges during our freshman year, 2 we didn’t have any say on which rooms we’d live in, 3 and TD and I shared a space for a while. We didn’t get along particularly well. We didn’t fight, but it wasn’t pleasant either.

Sometime during sophomore year (maybe junior) TD announced to the house that he’s gay, first by having a brother from a different house spend the night and then bringing it up in a chapter meeting. The house seemed split 50/50 between acceptance/non-acceptance 4. The house had communal showers on each floor. I remember thinking of myself as progressive by telling TD in the shower, when we happened to be in there, that his personal life is his own.

After the dust settled, TD was a different person. More relaxed, more aware, happy, fun. As seniors, we were roommates again, this time by choice, and we had great rapport. I wouldn’t want anyone to continue such a conflicted life. There was one conversation from that time that really sticks with me. TD had gone on dates with two different guys and stopped by the house with each. I got to meet his dates and chat a bit. Some days later, I asked TD, “so you gonna go out with X or Y again?” We talked a bit about the dates. One guy was too forward, the other is nice they’ll probably see each other again. What really struck me is that I could have had exactly the same conversation about women. Just change the pronouns.

What I learned from TD, is that being gay is not a lifestyle choice. He was a different person after coming out. Furhermore, unless you want to fantasize about your friends’ partners, who your friends date doesn’t have any negative impact on you.  TD has been with the same man for a bunch of years now. TD was active in the fight for marriage equality, but I’m not sure they want to marry.

The second person who shaped my views on homosexuality was also in my fraternity. I’ll be a bit more vague on his identity and the reasons why will be obvious in a bit. I’ll refer to him as Bob (since I can’t think of any others from my fraternity with that name)

Bob was really fun. Everyone liked him. He dated women. One night we were returning from a night of partying at another fraternity 5 when he mentions that he’s bisexual.  I didn’t think much of it. A couple days later, Bob and I are chatting and the topic comes up again. He tells me he’d got a crush on someone else in the house and he’s not sure how to approach it. We chat about that a bit when I make a side comment, “I’m assuming, in this conversation, that you’re not talking about me”. A couple minutes later, he confesses that I am, indeed, the object of his crush. hmmm. Now what do I say? I’d never before had a guy tell me he was into me.

Many of us may think we know how we’d respond in such a situation, but until you’re actually faced with it, you don’t really know. I indicated that I wasn’t interested, but over the coming weeks, he persisted. He was fairly specific and explicit, assuring me there’d be no obligations. These were the kind of advances, that many guys would not turn down if they were coming from a woman.

It made me think.

In college, I wasn’t really very lucky with the ladies. Like anyone else, a bit of intimacy or human connection would have been really welcome. I seriously considered it. College is supposed to be about exploration and finding oneself. Trying new things. I thought, “why not? I’m an open minded guy”. The problem was Bob doesn’t have the softness that I find appealing in women. Bob has body hair. 6 Bob doesn’t have boobies. Had Bob been more effeminate, I probably would have taken him up on his offers. This was also what solidified in me my confidence about my own orientation.

You don’t really know who you are until you find yourself on the edge of some sort of cliff. I am thankful to Bob. Like TD, Bob has also been with the same man for many years. I imagine they will marry if they haven’t already.

The third person who has deeply shaped my perspective on homosexuality is a woman I dated while living in Israel. I’ll refer to her as MF. MF and I met on a trip with Intel’s mountain biking club about two or three weeks after my arrival in the country. She’d largely grown up in Connecticut so she was a welcome familiar personality. I gave her a ride home from that trip and we agreed I’d give her a lift in future weeks 7. MF and I quickly realized we got along really well and I soon considered her to be my girlfriend. I met her parents and regularly had Friday dinners with them at their house 8. I got along really well with her parents. Her mom loved me. Not long into our relationship, MF asked me if I’d ever been with a guy. I replied no. She pushed a bit more, “you’ve really never been with a guy?”. I recounted to her my experience with Bob and thought it was odd that she was so persistent about the topic. She then told me that she had been with women in the past and told me of previous girlfriends. I was fine with that. Lots of women have made connections with other women. She tells me “so I’ve dated women and I figured that’s the path I’m on until I met you.”

You can’t change who you are, but I didn’t really understand that at the time. I really liked MF. 9. We agreed that we’d give it a shot and see how it goes but I’d need to be patient. Several months of frustration ensued. If not for this obstacle, I would have married her. We got along really well. We had great chemistry. I fit into her family really well. We enjoyed the same activities.

Being a compelling partner is not enough to convert someone. You’re gay/lesbian, straight, or something in between, but there’s no changing it. Soon after we broke up, MF started dating a woman with whom she still shares a life today. MF has carried four children that they are raising together. 10

I am thankful to TD, Bob and MF for the ways in which they’ve touched my life. I am happy for them that they’ve come to terms with who they are. I am happy to see that society is placing less pressure on folks to be someone they’re not. Marriage equality in all 50 states is another step in that direction. It’s not a lifestyle choice.

 

I have thoughts on other aspects on this topic that I’ll likely write about.


  1. A common thing for Mac users to do back then was to redefine system sounds. One of the guys in the house replaced the error sound (for example trying to type when a modal dialog was waiting for ok) with a sound clip of TD saying, “what do you think? I’m some kind of faggot?”

  2. this was in an era when MIT fraternity rush happened two or three days after new freshmen show up on campus. Pledges immediately moved into the house. MIT doesn’t do this anymore and I’m not aware of any other school having such a system. I think it worked well because you could rush without predetermined biases for or against the reputations of each house.

  3. During the first semester, we were each required to rotate between three different rooms, thereby being exposed to more many older brothers

  4. One of the brothers went so far as to say he could no longer use the chapter room to spend the night with his girlfriend. It’d been tainted. The same guy is somewhat religious and I found it humorous that he was using that room to screw the woman he wasn’t married to. In his defense, they did marry and are still together today.

  5. another thing that’s different about MIT (even today?) is that most fraternity parties are open to any MIT student, even members of another house. I believe most other schools (all?) have it such that you only go to parties thrown by your own house.

  6. I’ll note that my dad once referred to me as looking like a wolf 🙂

  7. In Israel, cars were quite expensive to own. Most people that did have cars, got them as a work benefit. Because of this, I got to know Haifa really well. Better than any other city I’ve lived in

  8. In Israel, Friday dinners is something everyone does, every week. I should do a post just on that.

  9. in the years since I’ve returned to the US, I’ve visited Israel for work a bunch of times. In those visits I probably spent more time with her than my other Israeli friends combined. I also spent most of my first sabbatical with her on a month and a half trip to Kenya

  10. One of my trips to Israel was when she was pregnant with her first son. I’d never touched a pregnant belly before. A couple years later, I visited shortly after the birth of her twins. Sometime during that trip she took a picture of me holding one of them. At the time, I had a thought that made me really sad “this child should have been mine”