Fedora Linux Support Community & Resources Center
  #1  
Old 17th April 2012, 11:00 AM
spskhokhar Offline
Registered User
 
Join Date: Nov 2011
Location: Punjab, India
Posts: 23
linuxfirefox
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
{
private:
int value;

public:
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);
test->printData();
return NULL;
}

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

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

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

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
  #3  
Old 29th April 2012, 05:47 PM
spskhokhar Offline
Registered User
 
Join Date: Nov 2011
Location: Punjab, India
Posts: 23
linuxubuntufirefox
Re: pthread with c++ (multithreading)

thanx marko
you are right...
Reply With Quote
Reply

Tags
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 07:45 PM
pthread aggravation... Pazuzu Using Fedora 0 21st December 2006 05:21 PM
pthread id and process id ckmun81 Using Fedora 0 20th November 2006 02:32 AM
static linking pthread lib afeick Programming & Packaging 10 4th November 2005 01:18 PM


Current GMT-time: 14:45 (Tuesday, 29-07-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

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