c++ - PThread Question -


i trying make small thread example. want have variable , each thread try increment , stop once gets point. whenever variable locked, want sort of message printed out "thread x trying lock, cannot" know it's working correctly. first day coding threads feel free point out unnecessary in code here -

#include <iostream> #include <pthread.h> #include <stdio.h> #include <stdlib.h> using namespace std;  #define num_threads 2 pthread_t threads[num_threads]; pthread_mutex_t mutexsum;  int number = 0; void* increasebyhundred(void* threadid) {      if(pthread_mutex_lock(&mutexsum))         cout<<"\nthread "<<(int)threadid<<" trying lock cannot";      else {         for(int i=0;i<100;i++) {             number++;             cout<<"\nnumber: "<<number;         }         pthread_mutex_unlock(&mutexsum);         pthread_exit((void*)0);     } }   int main(int argc, char** argv) {      int rc;     int rc1;     void* status;      pthread_attr_t attr;      pthread_mutex_init(&mutexsum, null);     pthread_attr_init(&attr);     pthread_attr_setdetachstate(&attr, pthread_create_joinable);      rc = pthread_create(&threads[0], &attr, increasebyhundred, (void*)0);     rc1 = pthread_create(&threads[1], &attr, increasebyhundred, (void*)1);      pthread_attr_destroy(&attr);      while(number < 400)         pthread_join(threads[0], &status);       pthread_mutex_destroy(&mutexsum);     pthread_exit(null);  } 

i following tutorial found here https://computing.llnl.gov/tutorials...reatingthreads , tried adapt mutex example idea. code increments 199 , stops. i'm guessing because threads doing routine once. there way make them routine other when create them say

while routine ?

i have pthread_join there because similar tutorial had on theirs. don't though. i'm pretty sure line problem...i don't know how fix it. appreciated.

whenever variable locked, want sort of message printed out "thread x trying lock, cannot" know it's working correctly.

why want that? learning threads. learn basics first. don't go diving off deep end pthread_mutex_trylock or mutexes configured error checking. need learn walk before can learn how run.

the basics involves mutex initialized use default settings , using pthread_mutex_lock grab lock. default settings, pthread_mutex_lock return non-zero if there big, big problems. there 2 problems can occur here: deadlock, , bad mutex pointer. there no recovery either; real solution fix code. thing can here throw exception don't catch, call exit() or abort(), etc.

that other thread has locked mutex not big problem. not problem @ all. pthread_mutex_lock block (e.g., go sleep) until lock becomes available. 0 return pthread_mutex_lock means calling thread has lock. make sure release lock when done working protected memory.

edit
here's suggestion let see threading mechanism working advertised.

  1. upon entry increasebyhundred print time-stamped message indicating entry function. want use c printf here rather c++ i/o. printf() , related functions thread-safe. c++ 2003 i/o not.
  2. after successful return pthread_mutex_lock print time-stamped message indicating successful lock.
  3. sleep() few seconds , print yet time-stamped message prior calling pthread_mutex_unlock().
  4. do same before calling pthread_exit().

one last comment: checking error return pthread_mutex_lock. completeness, , because every programmer paranoid out, should check return status pthread_mutex_unlock.

what pthread_exit? doesn't have return status. could print message after calling pthread_exit, reach statement if using non-compliant version of threads library. function pthread_exit() cannot return calling function. period. worrying happens when pthreads_exit() returns tinfoil hat exercise. while programmers should paranoid beyond out, should not paranoid schizophrenic.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -