Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pthread problem

Status
Not open for further replies.

rotis23

Programmer
Aug 29, 2002
121
0
0
GB
hello,

i've got two c code files.

the first is the controller that uses a thread pool of pthreads. each thread then calls a function in the second c code file that does the work.

during testing i'm just spawning two instances of the second c code file.

the question! - will it cause problems if i don't use #define statements for variables? i think that there is some kind of cross calling of variables between the two spawned instances.

thanks for any help,

rotis23
 
Could you clarify the description regarding which files constitute which thread.

A thread cannot call a function in another thread,it can only pass a message to the other thread telling it to start doing something.

As for variable sharing it is always safer to #define or define as const variables that are not going to change. If any variable could be modified by multiple threads then it should be protected under a proper locking mechanism. e.g. pthread_mutex_lock()

cheers
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
I don't think you are very precise about which code is spawned and which file ... but it probably doesn't matter.

#define won't cause more problems here than else and it won't probably solve any either.

What you need to be very careful about is 2 or more threads accessing the variable (same location in memory) at the 'same time'.
If one thread is updatating a variable and another thread reads the variable BEFORE the first thread has finished updating ... then you are in deep ....
If the above scenario is possible you should protect the variable with a 'mutual exclusive thing' (Mutex).



Does this help ?

/JOlesen



 
thanks for the response.

to clarify, what i am doing is including a c code file with various functions. i suppose this is exactly the same as including the functions within one whole program or code file. in this case it is obvious that global variables are going to be shared between two or more threads and therefore need synchronising or mutexing.

what happens then when library functions (like stdlib.h) are being called by many c processes. are all global variables mutexed?
 
ok,

i've just debugged the sproblem and indeed the global variables are being acccesed by both threads. it seems pretty obvious now though.

i'm more used to OO programming where instances provide mutual exclusion. is there an alternative way to using mutexes? again, how do c libraries achieve mutual exclusion?

thanks,

rotis23
 
I guess what you are asking is how do library functions achieveve mutual exclusion when being called by multiple THREADS.

Each library functions global data is protected by a pthread_mutex. These mutexes are invoked by every library function which has shared data. The libraries which have resolved these issues are called thread safe libraries.

Sorry there are only three ways to chaive mutual exclusion in C
1) Use one of the syncronisation primitives e.g. pthread_mutex,pthread_cond_wait()
2) Write your own synconization primitives(be very very carefull on multiprcessor machines)
3) Eliminate all your global variables.

cheers
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
thanks again for the responses,

after thinking this through. the problem i have is not providing mutual exclusion for global vairables but having exclusive global variables per thread. counters, for example.

now this is a bit of a nightmare. so i'm eliminating the global variables used by each thread (crazy_indian suggestion - 3). this is also a bit of a headache - passing many variables as function parameters.

the joy of multithreaded functionality!

cheers,

rotis23
 
just read some interesting info.

doesn't _REENTRANT do this? i mean, keep global variables of a library local per instance.

anyone used this?
 

simple said:

objects that may be changed by different threads have to be protected against concurrent access. this can be done i.e by a mutex.
if you don't use threads than you don't have to use this protection and you can increase the perfomance by disable this.
compiling your program with -D_REENTRANT means you would use the threadsafe versions of functions where a threadsafe and a threadunsafe version exists.
using threads you have to compile with -D_REENTRANT or a shortcut for a few important options like -pthread on linux.

noka
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top