Fedora Linux Support Community & Resources Center
Old 17th April 2012, 11:00 AM
spskhokhar Offline
Registered User
Join Date: Nov 2011
Location: Punjab, India
Posts: 23
pthread with c++ (multithreading)

* want multi-threading in c++ code, tried boost, working perfectly.
* But want to use pthread with c++, just for fun and it works
* Following is the sample code:

#include <iostream>
#include <string>
#include <cstdio>

extern "C"{
#include <pthread.h>
#include <errno.h>

using namespace std;

class Tester
int value;

Tester() {}
~Tester() {}
void setData(int i) {value = i;}
int getData() {return value;}
void printData()
int i;
for (i=value; i < (value + 10); i++) cout << "value: " << i << "\n";

void *runner(void *ptr)
Tester *test = static_cast<Tester*>(ptr);
return NULL;

int main(int argc, char *argv[])
Tester test;
pthread_t th;

if (pthread_create(&th, NULL, runner, static_cast<void*>(&test)) != 0)
return -1;
pthread_join(th, NULL);
return 0;
Reply With Quote
Old 22nd April 2012, 01:15 AM
marko Offline
Registered User
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 7,097
Re: pthread with c++ (multithreading)

To compile that you'd need something like (assuming the cc file is pthread_try.cc )

g++ -lpthread -o pthread_try pthread_try.cc
then run the pthread_try executable,
you can drop the -o pthread_try option and just run "a.out" if you don't care about the resulting file name
Reply With Quote
Old 29th April 2012, 05:47 PM
spskhokhar Offline
Registered User
Join Date: Nov 2011
Location: Punjab, India
Posts: 23
Re: pthread with c++ (multithreading)

thanx marko
you are right...
Reply With Quote

c++, cplusplus, multithreading, pthread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Display the pthread names? zi_au Using Fedora 2 12th March 2008 08:17 AM
Pthread issues TerjeG Using Fedora 1 18th February 2007 08:45 PM
pthread aggravation... Pazuzu Using Fedora 0 21st December 2006 06:21 PM
pthread id and process id ckmun81 Using Fedora 0 20th November 2006 03:32 AM
static linking pthread lib afeick Programming & Packaging 10 4th November 2005 02:18 PM

Current GMT-time: 17:27 (Wednesday, 23-08-2017)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat