LinuxQuestions.org
Review your favorite Linux distribution.
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
Location: San Diego, California (92115), U.S.A.
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 201

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
Location: San Diego, California (92115), U.S.A.
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 201

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
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,239

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
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
Location: San Diego, California (92115), U.S.A.
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 201

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: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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
Location: San Diego, California (92115), U.S.A.
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 201

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
Location: San Diego, California (92115), U.S.A.
Distribution: Ubuntu 14.04 LTS (Trusty Tahr)
Posts: 201

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 TheIndependentAquarius 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


All times are GMT -5. The time now is 11:36 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration