Introduction to Multi-Threaded Programming
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Reply to comment | Linux Journal
1 hour 36 min ago - Reply to comment | Linux Journal
1 hour 52 min ago - Favorite (and easily brute-forced) pw's
3 hours 43 min ago - Have you tried Boxen? It's a
9 hours 35 min ago - seo services in india
14 hours 7 min ago - For KDE install kio-mtp
14 hours 7 min ago - Evernote is much more...
16 hours 8 min ago - Reply to comment | Linux Journal
1 day 53 min ago - Dynamic DNS
1 day 1 hour ago - Reply to comment | Linux Journal
1 day 2 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
there are errors when linking
shindow@shindow-laptop:~/code$ gcc thread.c -o thread
/tmp/ccXY7Wqj.o: In function `main':
thread.c:(.text+0x40): undefined reference to `pthread_create'
thread.c:(.text+0x6e): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
i check it at :http://stackoverflow.com/questions/1662909/undefined-reference-to-pthread-create-in-linux-c-programming
so gcc -pthread thread.c -o thread is correct
Excellent
Hi,
First of all thanks for this tutorial. Its simple and very clear.
All mistakes in the sample example were already mentioned by others :)
Even I used
while(1) {
pthread_mutex_lock(&count_mutex);
if(x == 4000) {
pthread_mutex_unlock(&count_mutex);
pthread_exit(NULL);
return;
}
x++;
printf("Thread ID%ld: X is now %d.\n",
pthread_self(), x);
pthread_mutex_unlock(&count_mutex);
}
which will ensure x value to be 4000. See I got it from your tutorial :D.
Thanks again.
Just Add
The follow link is a interesting guide:
https://computing.llnl.gov/tutorials/pthreads/
Tcp Client and Server
How would you create a tcp server and client, whereby the server wants to be able to see any move made by the client and vice versa?[c-sharp]
Did the first commenter
Did the first commenter seriously use goto? GTFO and learn to program.
Accessing the variable "x"
Accessing the variable "x" outside the mutexed area is not really a good idea.
I would rather have used something like this:
void *mythread(void *data) { start: pthread_mutex_lock(&count_mutex); if (x < 4000) { x++; printf("Thread ID%ld: X is now %d.\n", pthread_self(), x); pthread_mutex_unlock(&count_mutex); goto start; } else { pthread_mutex_unlock(&count_mutex); } pthread_exit(NULL); }This is one of the rare cases where a "goto" is useful ;-)
Thanks for the tutorial.
Restructuring the increment loop
How about this?
bool run=true;
while(run) {
pthread_mutex_lock(&count_mutex);
if(run=(x<4000) x++;
printf("Thread ID%ld: X is now %d.\n",
pthread_self(), x);
pthread_mutex_unlock(&count_mutex);
}
I think changing the
I think changing the increment line to
if (x < 4000) {x++};
would be better, otherwise the count goes higher than 4000 as the while check is outside the mutex. Otherwise, thanks for the example, it was very clear.
code analyzer for multi threaded programming
Errors in multi threaded application programming like Thread block, Dead lock and Race conditions can also be solved using static tools. Companies like Symbian , Juniper networks ,Research in Motion(Blackberry),Cisco are using Coverity Prevent, a Static analysis code inspection tool for analyzing source code for fixing defects. Coverity Prevent is also used by the Department of Homeland security to scan many open source projects. you can get more info at at http://www.Coverity.com
LinuxThreads and NPTL
Perhaps you would like to revisit
http://pauillac.inria.fr/~xleroy/linuxthreads/
It's stated that "LinuxThreads is now obsolete and is being replaced by NPTL."
Thank you for the introduction.
pthreads is not obsolete.
pthreads is not obsolete. it's just the implemention in glibc and kernel.
Excellent
This is my first MT program, this tutorial gave me a clear idea..wonderful..!!
ThanX a lot..!!
Yes
The article helped me to understand.
Thanks...
Compile error with the source code
I think this line :
pthread_t tid[10];
Should be changed into :
pthread_t tids[10];
Because code uses "tids" instead of "tid" ... a small syntax error.
Thank you for the tutorial it helped a lot.
The sample code is buggy
Thanks for the tutorial,
I tried the sample code provided in this article. After a small change of the variable name tid to tids, I compiled the program with no errors. However, the result was not what I was expecting .Actually the variable X goes up to 4009 not the expected value 4000. I think it might be because the lock is inside the while loop..further stop condition should be implemented after the thread got the mutex.
thanks
Re: Introduction to Multi-Threaded Programming
Probably should have covered condition variables.
Re: Introduction to Multi-Threaded Programming
i need some more examples programmes for threads.could u send to my id?.my id is " panusuyadevi@yahoo.co.in "
thanku
Re: Introduction to Multi-Threaded Programming
IT is a wonderfull sample...
I would like to know more about pthreads..
could pls assist me where i could get more information and samples --Jai
Re: Introduction to Multi-Threaded Programming
IT is a wonderfull sample...
I would like to know more about pthreads..
could pls assist me where i could get more information and samples
Re: Introduction to Multi-Threaded Programming
Wonderfully written. Convered all the basic issues with MT programming in just a page.
Re: Introduction to Multi-Threaded Programming
Very well written. Simple yet precise.