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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sharing a structure between 2 kernel threads........ 1

Status
Not open for further replies.

Statement

ISP
Oct 12, 2001
6
IN
Hi, can any one pls tell me as to how i can share a structure or an array among two diferent threads.....I need to acces the same structure thru the different threads....how and where do I declare it.......
Thanx for any help in advance....
 
Statement,

The beauty of threads is that you do not have to worry about how to do things like this. You acccess them the same way you would if you only had one thread. Please read the follwoing pseudo code.

Code:
float myArray[1000] ;
struct {
  float f ;
  char *str ;
} myStruct ;

main() {
  pthread_create(&id, threadMain....) ;

  myArray[ 23 ] = 3.14159 ;
  myStruct.f = 2.71828189 ;
  
}

threadMain() {
  myArray[ 24 ] = 3.14159/2.0 ;  /* I can access them here too */
  myStruct.f += 1.0 ;
}

Ofcourse you need to protect the data using mutex, semaphore, locks, etc... if you are manipulating the data in both threads.

Brudnakm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top