Return-Path: william@bourbon.usc.edu Delivery-Date: Sun Oct 12 08:46:19 2008 X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on merlot.usc.edu X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.3 Received: from bourbon.usc.edu (bourbon.usc.edu [128.125.9.75]) by merlot.usc.edu (8.14.1/8.14.1) with ESMTP id m9CFkJI4021717 for ; Sun, 12 Oct 2008 08:46:19 -0700 Received: from bourbon.usc.edu (localhost.localdomain [127.0.0.1]) by bourbon.usc.edu (8.14.2/8.14.1) with ESMTP id m9CFrYiq014601 for ; Sun, 12 Oct 2008 08:53:34 -0700 Message-Id: <200810121553.m9CFrYiq014601@bourbon.usc.edu> To: cs551@merlot.usc.edu Subject: Re: cs551 Ruminations on global variables Date: Sun, 12 Oct 2008 08:53:34 -0700 From: Bill Cheng Someone wrote: > I have heard, learned and practiced to avoid global variables for a > decade :-) . However, I found myself using a lot of global variables > in warm-up 2. Likewise, my partner and I are expecting more global > variables in the final project. > > Although I could pass many arguments to the thread entry function by > passing them as a pointer to a structure, I don't think it's very > convenient. > > Should I feel guilty about using many global variables? How about just one global variable? You can always build a structure to contain all your global variables and have only one instance of this structure! This way you have only one global variable without changing much! I guess my point is that you don't really have to feel too bad about having many global variables because you can always consolidate them into one. You do have to worry about locking global variables! If you have a major data structure that many threads would need access to, you need a mutex for accessing it. If it's become part of a larger data structure, you can still use mutex to lock part of the data structure. So, it doesn't really present any problem. -- Bill Cheng // bill.cheng@usc.edu