LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 02-20-2013, 06:25 PM   #1
methodtwo
Member
 
Registered: May 2007
Posts: 146

Rep: Reputation: 18
pthreads questions


Hi i called this pthreads questions even though ultimately it might not come down to just pthreads but something more general. I dunno.
In my C program i have the main function call a function that creates some pthreads. The program is a TCP/IP daemon that serves some basic text-based games like tic-tac-toe(yes this is just a programming exercise). Thus it is pre-threaded and each thread serves a game. So the main loop of the program happens in the code of each thread. However because the main loop of these basic games is now called in the thread code, instead of in main where it originally was, functions external to the thread have to get called from within the thread. The games state is a [3][3] char array which was global originally. So now because it's supposed to be a network daemon, i have to use "thread-specific-data" for this [3][3] char array. Here is the main loop from each thread:
Code:
void *thread_main()
{
   char done  = ' ';
   init_matrix();
 do {
     disp_matrix();
     get_player_move();
     done = check();
     if(done != ' ') break;
     get_computer_move();
     done = check();
} while(done == ' ');

  if(done == 'X') printf("You Won\n");
  else printf("I won\n");

  disp_matrix();
  freeaddrinfo(res);
  return(0);
}
And here is the "thread-specific-data" for the games state([3][3] char array)created in "get_player_move"
Code:
pthread_once(&ts_once, pt_once);
   if((tsd = pthread_getspecific(tskey)) == NULL) {
	tsd = calloc(1, sizeof(Thread));
        pthread_setspecific(tskey, tsd);
   }
So now because the [3][3] char array for the games "state" is in the Thread struct pointed to by tsd then "struct Thread *tsd" has to be global as in the code example above! or has to be passed as an argument to each function. Can you have a pointer to thread specific data(that gets returned from pthread_get_specific) declared as a global? And if not then how do you pass it to all the functions that the threads call?But if i pass "struct Thread *tsd" to the functions the compiler kicks out like 12 errors. The main one is "expected expression" with reference to functions like init_matrix. the call conforms to the prototype namely "init_matrix(struct Thread *tsd)".
I don't know how to fix this. I'm really sorry that i haven't written about this in a general and concise way. I don't know if i understand C enough yet to do that. Any help would be great. Thank you very much for reading

Last edited by methodtwo; 02-20-2013 at 10:33 PM.
 
Old 02-20-2013, 11:00 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.

http://www.linuxquestions.org/questi...4/#post4896188
 
  


Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pthreads questions methodtwo Programming 9 02-21-2013 12:51 AM
[SOLVED] [Pthreads]Usage of Conditions Variables with Pthreads Aquarius_Girl Programming 2 01-14-2013 05:40 AM
Pthreads a12ctic Linux - Software 1 06-09-2005 10:05 PM
pthreads Keith Hampton Linux - Software 1 10-23-2003 01:46 PM
pthreads h/w Programming 5 10-09-2003 11:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration