0
  1. what is thread
  2. the difference between cases with using mutex and without using mutex
  3. difference between using join() method and without using join()
  4. which low-level functions is called when you create thread with std::thread class constructor and with using pthread.

I have read the material on the internet and still I am asking the question just for further strengthen my ideas. Thanks in advance

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
jim
  • 43
  • 1
  • I would also try and write the code, I find that reading only gets me so far, I find that writing the code reinforces a lot of what I've been reading. –  Dec 26 '15 at 14:28
  • Search the web! Also, if you claim to "have read the material on the internet", it would be helpful if you provided a link. Lastly, people here are willing to help you but your questions just look like homework and thus you look like someone that's too lazy to do their homework. – Ulrich Eckhardt Dec 26 '15 at 15:57

2 Answers2

1

1) A thread allows for parallel execution of your program. Using multiple threads in your program allows multiple processor cores to execute your code and thus (usually) speeding up the program.

2) Because threads allows parellel execution of code it can happen that thread #1 is reading data while thread #2 is modifying this data, this can result in some funky cases you don't want to happen. Mutexes stop this behaviour by making threads wait their turn in these particular critical sections.

3) using thread.join() makes the current thread wait for the completion of thread object that's been called join() upon.

4) This is really OS specific. For example, Unix based systems use pthread as the underlying thread class when creating a std::thread. The compiler vendor implements this.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

If you would like to learn multi threading using with C++ standard library then please refer C++ concurrency IN Action(Author Williams). It is very good book also referred on The Definitive C++ Book Guide and List

  1. what is thread

Thread is an execution unit which consists of its own program counter, a stack, and a set of registers. Thread are implemented in application to improve the performance and effective use of CPU's The CPU switches rapidly back and forth among the threads giving illusion that the threads are running in parallel. Refer https://en.wikipedia.org/wiki/Thread_%28computing%29

  1. the difference between cases with using mutex and without using mutex

Imagine for a moment that you’re sharing an apartment with a friend. There’s only one kitchen and only one bathroom. Unless you’re particularly friendly, you can’t both use the bathroom at the same time, and if your roommate occupies the bathroom for a long time, it can be frustrating if you need to use it. Likewise, though it might be possible to both cook meals at the same time, if you have a combined oven and grill, it’s just not going to end well if one of you tries to grill some sausages at the same time as the other is baking a cake. Furthermore, we all know the frustration of sharing a space and getting halfway through a task only to find that someone has borrowed something we need or changed something from the way we left it.

It’s the same with threads. If you’re sharing data between threads, you need to have rules for which thread can access which bit of data when, and how any updates

When you have a multi-threaded application, the different threads sometimes share a common resource, such as a global variables, file handler or any. Mutex can be used with single resource for synchronization. Other synchronization methods(like semaphore) available to synchronize between multiple threads and process.

The concept is called "mutual exclusion" (short Mutex), and is a way to ensure that only one thread is allowed inside critical area, using that resource etc.

  1. difference between using join() method and without using join()

calling thread waits for the thread specified by thread to terminate. If that thread has already terminated, then join() returns immediately. The thread specified by thread must be joinable. By default thread are joinable if you not changed its attribute.

  1. which low-level functions is called when you create thread with std::thread class constructor and with using pthread.

pthread_create is called in case of linux. std thread library is platform independent. So it call different thread API specific to the underlying operating system.

Community
  • 1
  • 1
Ajay yadav
  • 4,141
  • 4
  • 31
  • 40
  • 2
    Just in response to the the answer to the third question, there is no parent-child relation between threads! You could also do the join in the opposite direction, i.e. join the thread that started another from the thread that was started. – Ulrich Eckhardt Dec 26 '15 at 16:00