Introduction to Multi-Threaded Programming
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
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
| 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 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- RSS Feeds
- Trying to Tame the Tablet
- New Products
- What's the tweeting protocol?
- Dart: a New Web Programming Experience
- Reply to comment | Linux Journal
1 hour 38 min ago - Drupal is an Awesome CMS and a Crappy development framework
6 hours 17 min ago - IT industry leaders
8 hours 40 min ago - Reply to comment | Linux Journal
1 day 1 hour ago - Reply to comment | Linux Journal
1 day 4 hours ago - Reply to comment | Linux Journal
1 day 5 hours ago - great post
1 day 5 hours ago - Google Docs
1 day 6 hours ago - Reply to comment | Linux Journal
1 day 11 hours ago - Reply to comment | Linux Journal
1 day 11 hours ago
Enter to Win an Adafruit Prototyping Pi Plate 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 Prototyping Pi Plate 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
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




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.