LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-12-2013, 01:29 PM   #1
pratham29
LQ Newbie
 
Registered: Oct 2013
Posts: 8

Rep: Reputation: Disabled
Multithreading


Hi ,
I am new to Linux, I had difficulty understanding the following code :

Code:
#include <pthread.h> %p thread library
#include <stdio.h>

void* test(void* s) {
  sleep(2);
  printf("%s\n",(char*)s);
  return NULL;
}
int main (void) %declare 2 variables
 {
  pthread_t t1,t2;
  pthread_create(&t1,NULL,&test,(void*)"thread1");
  pthread_create(&t2,NULL,&test,(void*)"thread2");
  pthread_join(t1,NULL); %join :wait for another thread to exit
  pthread_join(t2,NULL);
  return 0;
}
Output :
Thread 2
Thread 1

I could not understand the logic of this code. Also , will i get different output if executed on different o/s.

Thanks for help in advance. Awaiting response
 
Old 10-13-2013, 09:52 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,260

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
What's there not to understand?

main() is executed when you run the program, the first thing it does is create two threads. It spawns t1 and passes the void* argument "thread1", then it spawns t2 and passes the void* argument "thread2". It then waits for t1 to exit, waits for t2 to exit, and then returns.

Each thread is spawned with the "test" routine, which simply sleeps for two seconds and then prints out the void* (recast as a char*) argument that it was called with before exiting.

The output will always be either:
thread 1
thread 2

or

thread 2
thread 1

Since the two threads are in a race condition for output after their 2 second delay.

Where exactly are you confused? I think this is probably the simplest example of multithreading imaginable. How familiar are you with C, and programming in general? This question has nothing to do with Linux, FYI. Execution will be the same on any OS.

Last edited by suicidaleggroll; 10-13-2013 at 09:58 PM.
 
1 members found this post helpful.
Old 10-14-2013, 05:32 PM   #3
pratham29
LQ Newbie
 
Registered: Oct 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
What's there not to understand?

main() is executed when you run the program, the first thing it does is create two threads. It spawns t1 and passes the void* argument "thread1", then it spawns t2 and passes the void* argument "thread2". It then waits for t1 to exit, waits for t2 to exit, and then returns.

Each thread is spawned with the "test" routine, which simply sleeps for two seconds and then prints out the void* (recast as a char*) argument that it was called with before exiting.

The output will always be either:
thread 1
thread 2

or

thread 2
thread 1

Since the two threads are in a race condition for output after their 2 second delay.

Where exactly are you confused? I think this is probably the simplest example of multithreading imaginable. How familiar are you with C, and programming in general? This question has nothing to do with Linux, FYI. Execution will be the same on any OS.
Thanks, was confused in the calling function part but now cleared with this doubt.No background in programming , hence the question.
 
Old 10-15-2013, 08:55 AM   #4
zeebra
Member
 
Registered: Dec 2011
Distribution: Mageia 3, Debian Wheezy, Maemo, Linux Mint 14.
Posts: 200

Rep: Reputation: Disabled
You should read <pthread.h>
 
  


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
multithreading ffmpeg spezticle Linux - Software 4 05-22-2012 02:04 AM
Multithreading yes or no? mishomor Linux - General 11 06-14-2011 02:38 PM
Multithreading capability bavondrake Linux - Software 9 09-13-2007 12:57 AM
qt multithreading? suicidle_p0ptart Linux - Software 5 08-28-2007 02:54 AM
Multithreading jayashrik Programming 1 07-20-2005 05:13 AM


All times are GMT -5. The time now is 11:03 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration