Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.development.apps > #425

Re: object file memory

From Bill M <wpmccormick@just_about_everywhere.com>
Newsgroups comp.os.linux.development.apps
Subject Re: object file memory
Date 2012-02-08 20:42 -0600
Organization A noiseless patient Spider
Message-ID <jgvbqo$uin$1@dont-email.me> (permalink)
References (1 earlier) <20120207000059.76@kylheku.com> <jgrc5t$5f0$1@dont-email.me> <jgrmj8$6pc$1@dont-email.me> <alpine.DEB.2.00.1202072202080.25401@login01.caesar.elte.hu> <jgs7dk$ng5$1@dont-email.me>

Show all headers | View raw


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!!

Back to comp.os.linux.development.apps | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

object file memory Bill M <wpmccormick@just_about_everywhere.com> - 2012-02-06 16:41 -0600
  Re: object file memory Kaz Kylheku <kaz@kylheku.com> - 2012-02-06 23:07 +0000
    Re: object file memory Bill M <wpmccormick@just_about_everywhere.com> - 2012-02-07 08:23 -0600
      Re: object file memory Bill M <wpmccormick@just_about_everywhere.com> - 2012-02-07 08:40 -0600
        Re: object file memory Bill M <wpmccormick@just_about_everywhere.com> - 2012-02-07 08:58 -0600
      Re: object file memory Bill M <wpmccormick@just_about_everywhere.com> - 2012-02-07 11:21 -0600
        Re: object file memory "Ersek, Laszlo" <lacos@caesar.elte.hu> - 2012-02-07 22:09 +0100
          Re: object file memory Bill M <wpmccormick@just_about_everywhere.com> - 2012-02-07 16:08 -0600
            Re: object file memory Bill M <wpmccormick@just_about_everywhere.com> - 2012-02-08 20:42 -0600
              Re: object file memory Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-02-09 19:00 +0000
            Re: object file memory Joe Beanfish <joe@nospam.duh> - 2012-02-09 11:15 -0500

csiph-web