We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,520 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

what a thread is...?

so to create a thread all i need is to call this function? and the codes in the "void *(*start_routine)" function is a thread?

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
    void *(*start_routine)(void*), void *arg);
3
Contributors
2
Replies
6 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
wildplace
Junior Poster in Training
72 posts since Dec 2009
Reputation Points: 10
Solved Threads: 4
Skill Endorsements: 0

start_routine is the entrance point of the thread create with pthread_create.

gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0

Yeah, that's pretty much it. Try this for instance:

#include <pthread.h>
#include <iostream>

void* f(void*) {
  for(int i = 0; i < 1000; ++i) {
    std::cout << "Print from the Thread!" << std::endl;
    usleep(1000);
  };
  return NULL;
};

int main() {
  pthread_t threadID;
  pthread_create(&threadID, NULL, f, NULL);
  for(int i = 0; i < 1000; ++i) {
    std::cout << "Print from the Main Function!" << std::endl;
    usleep(1000);
  };
  pthread_join(threadID, NULL);
  return 0;
};

Compile with:

$ g++ pthread_example.cpp -o pthread_example -lpthread
mike_2000_17
21st Century Viking
Moderator
3,145 posts since Jul 2010
Reputation Points: 2,062
Solved Threads: 626
Skill Endorsements: 41

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0642 seconds using 2.67MB