LinuxQuestions.org
Help answer threads with 0 replies.
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


Reply
  Search this Thread
Old 02-20-2013, 01:48 PM   #1
methodtwo
Member
 
Registered: May 2007
Posts: 146

Rep: Reputation: 18
pthreads questions


Hi there
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. Having global doesn't make any sense because it's thread specific data right? 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 06:23 PM.
 
Old 02-20-2013, 05:52 PM   #2
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Rep: Reputation: 26
Quote:
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.
Hi there! I would suggest you take this to the Programming forum.

I'm also curious, why would you go with mt programming, if you are a beginner C programmer?

 
Old 02-20-2013, 06:21 PM   #3
methodtwo
Member
 
Registered: May 2007
Posts: 146

Original Poster
Rep: Reputation: 18
Thanks for the reply. Is mt programming multi thread? In that case i wanted to learn more about C and i wanted to make the tic-tac-toe program that i wrote the socket code for into one that was a multi threaded daemon because i enjoy learning about UNIX and i'm also reading the classic w.rich stevens book APUE.
 
Old 02-20-2013, 06:23 PM   #4
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Rep: Reputation: 26
Quote:
Originally Posted by methodtwo View Post
Is mt programming multi thread?
Yes.

Quote:
Originally Posted by methodtwo View Post
i'm also reading the classic w.rich stevens book APUE.
I do not know that one!
 
Old 02-20-2013, 06:40 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I do agree that asking the mods (via the Report button) to move this to the programming forum is a good idea. Please don't start a duplicate question over there.
 
Old 02-20-2013, 07:13 PM   #6
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Rep: Reputation: 26
Quote:
Originally Posted by chrism01 View Post
I do agree that asking the mods (via the Report button) to move this to the programming forum is a good idea. Please don't start a duplicate question over there.
Done.
 
Old 02-20-2013, 09:22 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 02-20-2013, 09:23 PM   #8
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Rep: Reputation: 26
Thanks Tinkster...
 
Old 02-21-2013, 12:46 AM   #9
methodtwo
Member
 
Registered: May 2007
Posts: 146

Original Poster
Rep: Reputation: 18
This was closed in the programming forum. Sorry i didn't know that exact duplicates get closed. APUE is advanced programming in the unix environment, to answer an earlier question
 
Old 02-21-2013, 12:51 AM   #10
ejspeiro
Member
 
Registered: Feb 2011
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 203

Rep: Reputation: 26
Have you checked this one out?

The C Programming Language (2nd Edition) by Brian W. Kernighan and Dennis M. Ritchie.

That's the one I learned with, and the one I would suggest for C. Know it deeply enough and your background will be very solid.
 
  


Reply



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
[SOLVED] [Pthreads]Usage of Conditions Variables with Pthreads Aquarius_Girl Programming 2 01-14-2013 05:40 AM
pthreads manaila Linux - Newbie 1 07-01-2011 11:59 AM
Pthreads christheavatar Programming 4 03-05-2005 09:30 PM
pthreads ftgow Linux - Software 0 07-08-2004 03:55 AM
pthreads socket9001 Programming 2 12-29-2003 12:23 AM

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

All times are GMT -5. The time now is 06:12 AM.

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