A problem about time()

W

Wei Jones

Hi, all
I got a problem as subject title. I create a pthread which like :

static time_t *Now;
void get_time(void *tmp)
{
while(1) {
time(&Now);
disp_time_somewhere();
sleep(1);
}
}

In other task, I use time(&Before),too. And there is a small chance,
get_time() update (Before)'s value.

I have already fixed my code.
But I dont wanna guess the really answer, Can you help me ?

Thans a lot.
 
E

Eric Sosman

Wei said:
Hi, all
I got a problem as subject title. I create a pthread which like :

static time_t *Now;
void get_time(void *tmp)
{
while(1) {
time(&Now);
disp_time_somewhere();
sleep(1);
}
}

In other task, I use time(&Before),too. And there is a small chance,
get_time() update (Before)'s value.

I have already fixed my code.
But I dont wanna guess the really answer, Can you help me ?

The answer to what? The only question you have asked is
"Can you help me?" What problem are you having?

By the way, the C language has no support for multi-
threading and has no function named sleep(), so if your
question concerns them you should ask it in some other
newsgroup. comp.programming.threads or comp.unix.programmer
are likely candidates -- but it all depends on what your
question actually is.
 
R

Richard Heathfield

Wei said:
Hi, all
I got a problem as subject title. I create a pthread which like :

static time_t *Now;

This defines a pointer to a time_t object. Since it's static and not
initialised explicitly by you, it takes the value NULL.
void get_time(void *tmp)
{
while(1) {
time(&Now);

The time() function takes the address of a time_t object, but you are
passing the address of a /pointer/ to a time_t object. So the code is
broken.

Here is a correct call to time:

time_t Now;
time(&Now);
 
K

Keith Thompson

Richard Heathfield said:
Here is a correct call to time:

time_t Now;
time(&Now);

And here's another:

time_t now;
Now = time(NULL);

(assuming proper #include directives, of course).

The time() function returns its result both by returning it and by
storing it in the location pointed to by its argument. If the
argument is a null pointer, it just returns the result without trying
to store it as well. There are probably obscure historical reasons
for this odd behavior.

(I *think* that "Now = time(&Now);" will work, but it's redundant and
ugly. And redundant. And did I mention it's ugly?)
 
P

Peter Pichler

Keith Thompson said:
And here's another:

time_t now;
Now = time(NULL);

Syntax error :)
(I *think* that "Now = time(&Now);" will work, but it's redundant and
ugly. And redundant. And did I mention it's ugly?)

Provided that Now is declared (and having the right type), I cannot see
why it shouldn't work. No UB involved. But it would be ugly. And somehow
redundant.
 
K

Keith Thompson

Peter Pichler said:
Syntax error :)

Well, not a syntax error as such, but certainly an error.
(Yeah, I meant "time_t Now;". Oops.)
Provided that Now is declared (and having the right type), I cannot see
why it shouldn't work. No UB involved. But it would be ugly. And somehow
redundant.

And ugly, too. Don't forget ugly.
 
W

Wei Jones

Thanks a lot, for all your help.
Now I know what is wrong in my code.

Thanks a lot, especially Keith Thompson.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,093
Messages
2,570,614
Members
47,230
Latest member
RenaldoDut

Latest Threads

Top