TZ variable set within thread


 
Thread Tools Search this Thread
Top Forums Programming TZ variable set within thread
# 1  
Old 11-07-2003
TZ variable set within thread

Hello all,
I'm going to be using some of the date functions from time.h to do some time stamping. I will be getting a time and date from the header of a TIFF file. I will need to be able create a time for each time zone in the U.S. The source of the time stamp will be in GMT. What I'd like to do is to take the GMT time and date, load it up into a tm structure as GMT and set the TZ variable and then use localtime() to convert to the time zone I want. I need to be sure that this will only impact the thread I'm currently in. Since there will be many threads doing this at any given time I need to be sure that the manipulation of this variable will be local to the thread.
Thanks in advance!
-Bryan
# 2  
Old 11-07-2003
Bryan, this is not going to fly.

First, localtime() is not MT-safe. So you will need to use localtime_r().

But you can't use TZ like that. There is only one environment per process. Different threads can call putenv() without stepping on each other. But the last thread to change TZ wins. This is spelled out in the putenv man page: "These functions examine and modify the environment list, which is shared by all threads in a program."

The effect of TZ is to set some variables by indirectly invoking tzset(). I thought about bypassing tzset(), but it says here: "The ctime_r(), localtime_r(), and tzset() functions are MT-Safe in multithread applications, as long as no user-defined function directly modifies one of the following variables: timezone, altzone, daylight, and tzname. These four variables are not MT-Safe to access."

Maybe you can arrange for one process per time zone? Or write your version of localtime() that uses no per-process data?

I suppose that this sequence would work:
pthread_mutex_lock()
putenv()
tzset()
localtime_r()
pthread_mutex_unlock()
But I don't have enough experience with threads to know if that is as lame as it looks.

Personally, I would go with writing my own timezone routines. I know.... it's a lot of work. But it's the only path I see to a perfect result.
# 3  
Old 11-07-2003
Thx! I'll work on it a little more and check out another approach. If I come up w/anything cool I'll post it here.
-B
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Use read to set a variable.

Hi all, I used to set variable by read from keyboard read -p 'Input new value for variable :' var Now I want to pipe from ls and set to var a.txt b.txt c.txt ls | grep a.txt | read var why this cannot set the $var. What is the different between them....:wall: (4 Replies)
Discussion started by: mainsun
4 Replies

2. Shell Programming and Scripting

do you have a better way to set this variable?

greetings, i have a variable $input that i want to use to set $output. $input is /dir/filename.mph and $input is passed to my script that i manipulate it as follows: input=`basename $input`i want the $output to be filename_solved.mph, basically stuffing "_solved" in the filename. here's how i... (2 Replies)
Discussion started by: crimso
2 Replies

3. Shell Programming and Scripting

How to know who and where a variable is set ?

hi, i'm not a root user and i want to know which user and in which file is loaded a variable seen in the "env" display ? I will use this variable but i want to be sure that it will be a permanent variable ! i don't see it in my files (.profile , kshrc...) and neither in /etc/profile. ... (3 Replies)
Discussion started by: Nicol
3 Replies

4. HP-UX

What is the use of command set -- and set - variable?

Hi, I am using hp unix i want to know the use of the following commands set -- set - variable thanks (4 Replies)
Discussion started by: gomathi
4 Replies

5. Programming

how to create variable for all thread in c++

hello I have this code #include <sys/types.h> #include <unistd.h> #include <iostream> #include <pthread.h> #include<cstring> using namespace std; int var1=0; void doSomething() { var1 = 5; cout<<"Do S :"<<var1<<endl; sleep(1); var1 =7; (4 Replies)
Discussion started by: vip_a1
4 Replies

6. Shell Programming and Scripting

set variable with another variable? c shell

okay, this shouldn't be difficult but I can't figure it out. How can I set a variable with another variable. I have the following: foreach pe ($dir $sp) set tpe = `echo $pe | grep M` if ($tpe == M) then set ${$pe} = M <--- This doesn't work else endif end In this case what... (2 Replies)
Discussion started by: wxornot
2 Replies

7. Shell Programming and Scripting

set variable command

Hi all, I want to set a variable in ksh shell (prompt) and echo the value. $ set x=5 $echo $x But it is returning null. Can any one please help. Thanks in advance (1 Reply)
Discussion started by: ammu
1 Replies

8. Solaris

set enviroment variable..

Hello... I was wondering can anyone explain me how to set up enviroment variable to be permanent... I tryed with setenv but my solaris does not have this command... then I did: export ORACLE_SID=base1 export ORACLE_BASE=/home export ORACLE_HOME=$ORACLE_BASE/oracle/8.1.6 and by... (5 Replies)
Discussion started by: amon
5 Replies

9. UNIX for Dummies Questions & Answers

set variable PATH

Hi, i know that this topic discussed for many times but although i had researched them i couldnt succeed in my problem. i am following a step-by-step instruction guide and must do the following: ------------- To ensure access, set the path PATH $ORACLE_HOME/perl/bin:$PATH and set the Perl... (2 Replies)
Discussion started by: merope
2 Replies

10. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies
Login or Register to Ask a Question