I am a complete begineer in this field multithreading.
I have searched the internet; came across these POSIX and BOOST things. I dont know how to start. Can anuone guide me..I had downloaded the BOOST library but I couldnt find anything regarding the POSIX(and ? is the pthread). Which to choose or where to start off and run a Hello World...
PLZZZZZ Help Meee!!!!

POSIX is essentially for Linux or Unix systems. POSIX is just a specification for the API that the OS needs to expose and Linux, Unix and many others comply to those specifications. Windows does not, so windows has its own things, for example, via its Win32 API.

Boost Thread library is cross-platform in the sense that it's implemented to work with POSIX and Win32 (maybe others too, I don't know). In the coming C++0x standard, all operating systems will be required to implement a common multithreading library (std::thread), which will be almost identical to Boost Thread, because virtually all the new C++ standard libraries come from Boost.

So, I can give you a simple example, working with Boost Thread. First of all, the thread functions are in the form of function objects (or function pointers, but that's not very nice to use). A function object for a thread has to have a () operator. So here is a simple example that prints the number of times a thread loop has executed, with a "quit" option:

#include <boost/thread/thread.hpp>
#include <iostream>

//function object class that will be used to run the thread.
struct PrintThreadLoopCount {
  bool* Running; //declare a flag to tell whether the thread should still be running.
  int LoopCount; //count the amount of times the loop has run.

  //constructor
  PrintThreadLoopCount(bool* aRunning) : Running(aRunning), LoopCount(0) { };
  //copy-constructor (required if default is not good enough)
  PrintThreadLoopCount(const PrintThreadLoopCount& aObj) : Running(aObj.Running), LoopCount(aObj.LoopCount) { };

  //Declare a () operator method to make this class "Callable"
  void operator()() {
    while(Running[0]) {
      LoopCount++;
      std::cout << "\rThe thread has run: " << LoopCount << " times so far.              ";
      std::cout.flush();
      boost::this_thread::yield(); //The this_thread namespace lets you operate on the current thread. 
                                   //Yield() means the thread yields its execution time.
    };
  };

};

int main() {

  std::cout << "Press ('q' + enter) to quit..." << std::endl;

  bool RunningSignal = true;
  boost::thread* printing_thread = new boost::thread(PrintThreadLoopCount(&RunningSignal)); //This will launch the new thread of execution.

  std::string input;
  getline(std::cin,input); //the main thread just waits for the user to press q and enter.
  while(input != "q") {
    getline(std::cin,input);
  };

  RunningSignal = false; //signal the thread to stop.

  printing_thread->join(); //this will wait for the thread to be finished (i.e. join the two threads into one thread).

  std::cout << "\nThe application has terminated correctly! Have a nice day!" << std::endl;

  delete printing_thread;
  return 0;
};

Edited 6 Years Ago by mike_2000_17: n/a

Comments
Always posting such great stuff :)

i tried the code but it said :
1>LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc90-mt-gd-1_44.lib'

what is wrong with my library installation. I downloaded a setup file and installed boost using it, but at last sth went wrong(i.e few things were not unzipped successfully, it said). The installation finished anyway. Is this the problem?
Can you direct me the to boost library download or the installation whatever..

After installing boost I think you have to compile it with your compiler to generate the lib and dlls.

Did you follow these instructions ? If you are using Window 7 do not attempt to unzip the files in c:\ root directory. PkZip does not have permissions to unzp them there. Put them somewhere else and move the folder to c:\ if you want to using Windows Explorer.

Edited 6 Years Ago by Ancient Dragon: n/a

I don`t know what your goals are, but since you are just starting with multithreading, I`d strongly recommend OpenMP. It is very easy to use - just a couple of #pragmas and your code is running multithreaded.

It is safe to assume you won`t even get to situation where you`d be limited by OpenMP. Those last few percents of performance aren`t worth the time investment of other APIs, unless the business has a specific goal that must be fulfilled.

@wft4096: You gave a very interesting suggestion, openMP seems very nice. However, relating to the OP's question, Multi-threading and Parallel computing are two different things. The former is for flexibility and (real-time) multi-tasking, while the latter is for increasing performance of large scale algorithms on multi-core processors or super-computers or other parallel / distributed platforms of the like.

I just finished downloading and unzipping all the files for version 1.44.0 and did not have any problems. Make sure your hd has plenty of free space becaue boost is 308 mg and over 31,000 files.

Thanks for the suggestions. But what instructions i have followed should have made boost working, but it still is not.
@wtf4096 my goal is to install BOOST library.

what i did is downloaded boost_144_0
extracted it in e:\program files
invoked bootstrap that was in the boost_144_0 directory
then a bjam exe was created
i ran it.
what it did is compiled libraries and palced in different directories as eg.
E:\turmoil\boost_1_44_0\bin.v2\libs\thread\build\msvc-9.0express\debug\link-static\threading-multi\...lib
other libs were placed in different locations.
and now in my vc++ where to give the additional library directory in my vc++ IDE.
it wd b great if i got things right..
thanks!!

Hey Arthas - have you tried logging on to the Boost IRC channel? They have quite a few people who are always logged on. That's where I found most of my help.

Edited 6 Years Ago by Duki: n/a

OpenMP is really very nice. it takes a single line of preprocessor code and your loop runs on 4 cores.

i don`t think there is anything else that beats OpenMP in simplicity and speed of implementation.

I mean - in the morning you go and download the library and check docs and before going home from work, you`ve got it long parallelized.

Thanks people. I now have my boost running. Thanks for all your help..

This article has been dead for over six months. Start a new discussion instead.