0

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
3
Views
5 Years
Discussion Span
Last Post by mike_2000_17
1

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
Votes + Comments
cool! i love your example!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.