Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Bill M Newsgroups: comp.os.linux.development.apps Subject: Re: object file memory Date: Wed, 08 Feb 2012 20:42:25 -0600 Organization: A noiseless patient Spider Lines: 95 Message-ID: References: <20120207000059.76@kylheku.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 9 Feb 2012 02:42:32 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="lEF1sT+s6Siq+e4+OOaedA"; logging-data="31319"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/6klTF+nx1NiljxJBfCEBsZLhVFrOmpSE=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 In-Reply-To: Cancel-Lock: sha1:KsFctHN9onVJC1DxKBeFvBXeLXI= Xref: x330-a1.tempe.blueboxinc.net comp.os.linux.development.apps:425 Bill M wrote, On 2/7/2012 4:08 PM: > Ersek, Laszlo wrote, On 2/7/2012 3:09 PM: >> On Tue, 7 Feb 2012, Bill M wrote: >> >>> I have a global declared as follows: >>> >>> __thread OCX_COMM_STATE comm_state = OCX_COMM_START; >> >> That's not only a declaration, it's also an external definition. >> Therefore I think this line is from "foo.c", not "foo.h". >> >> >>> OCX_COMM_STATE is a typedef'd enum. >>> >>> So then, in a function called from the main program >>> >>> comm_state = OCX_COMM_SEND; >>> >>> But it seems not to change. >> >> Is that function in main.c? >> >> >>> Any thoughts? Did I miss something? >> >> You may have forgotten to update the declaration of the variable in >> "foo.h" (or main.c): >> >> extern __thread OCX_COMM_STATE comm_state; >> >> Without this declaration, the code in main.o has no idea about >> comm_state being in TLS. > > Thanks. I actually do have the extern declaration in the program that is > linking in foo, but failed to declare that fact in my post. > > Anyway, I see what my problem is: My main program is actually starting 2 > threads: the 1st is a client loop (a TCP client), and the 2nd is my > failed attempt to interface to it. So, in the client loop thread I have > one comm_state, and in the interface thread there is another; and of > course they have no idea about one another. I need to restructure this > program and design a better interface. > > I'm trying to encapsulate all of the TCP client interaction in one > object file, and then link that into various other applications that > want to use this client to talk to the server. I'm not a very good > programmer, so it's going to take me a couple of tries to make something > that is robust. > So I've re-written my client application to something I think is more manageable, but I'm still having some trouble. It seems that global variables declared with __thread are not keeping their values. I have something that looks like this: *** client.c *** static __thread COMM_STATE comm_state; static __thread int sockfd; static __thread struct sockaddr_in their_addr; static int connect(SERVER server) { if(comm_state == CONNECTED) return ALREADY_CONNECTED; ... sockfd = socket( ... ); ... connect(sockfd, ... } int client_write(SERVER server, char* buffer) { for(;;) { if((connect(server)) != ALREADY_CONNECTED) continue; if((comm_send(buffer)) != SUCCESS) continue; //now get a reply here ... } } *** client_app.c *** extern int client_write(SERVER server, char* buffer) ; ... So I think you get the idea, and my question is this: Should globals in client.c be persistent from one client_write call to the next for each thread in client_app.c that calls client_write? Or am I missing something? Thanks!!